Skip to content

Instantly share code, notes, and snippets.

@enesser
Last active September 22, 2019 01:04
Show Gist options
  • Select an option

  • Save enesser/7dee0105f1bdba82c06df3aeaeb56d74 to your computer and use it in GitHub Desktop.

Select an option

Save enesser/7dee0105f1bdba82c06df3aeaeb56d74 to your computer and use it in GitHub Desktop.
3D Math Primer
/****************************************************************
* Find the distance between two points
* given: (x1, y1), (x2, y2)
* vector2
*/
// d = √((x1-x2)² + (y1 - y2)²)
/****************************************************************
* Find the distance between two points in 3d space
* given: (x1, y1, z1), (x2, y2, z2)
* vector3
*/
// d = √((x1-x2)² + (y1 - y2)² + (z1 - z2)²)
/****************************************************************
* Convert between degrees and radians and vice versa
*/
// 180° = 3.14rad = π
// radians = degrees * π / 180
// radians = 90 * (π / 180) = π / 2
// degrees = radians * 180 / π
// degrees = (π / 3) * (180 / π) = °60
// right angle = 90° = (π / 2)
// straight angle = 180° = π
// full rotation angle = 360° = 2π
/****************************************************************
* Normalize 2D coordinates to coordinates in 3D space
*/
// (x / width) * 2 - 1
// -(y / height) * 2 + 1
// example:
document.addEventListener('click', (e) => {
mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(e.clientY / window.innerHeight) * 2 + 1;
mouse.z = 1;
rayCast.setFromCamera(mouse, camera);
}, false);
/**
* Return a pseudorandom number in between range of min and max.
*
* @param {Number} minimum number to return
* @param {Number} maximum number to return
* @return {Number} pseudorandom number in range of min and max
*/
let randomInRange = (min, max) =>
(Math.random() * (max-min)) + min;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment