Math3D

LittleJS 3D Math Plugin

  • Vector3 and Matrix4 for 3D games and plugins
  • Right handed, Y up, angles in radians
  • Used by the Render3D plugin, but has no rendering dependencies

Classes

Matrix4
Ray3D
Vector3

Methods

(static) buildMatrix(posopt, rotationopt, scaleopt) → {Matrix4}

Build a transform for an object from its position, rotation and scale

  • A point is scaled first, then rotated, then moved, which is what you want for a game object
Parameters:
NameTypeAttributesDescription
posVector3<optional>
rotationVector3<optional>

vec3(pitch, yaw, roll) in radians

scaleVector3<optional>
Returns:
Type: 
Matrix4

(static) collideBoxBox3D(posA, sizeA, posB, sizeB) → {Vector3|undefined}

Returns the vector to move box A by so it no longer overlaps box B, the shortest way out, or undefined

  • The 3D twin of collideBoxBox
Parameters:
NameTypeDescription
posAVector3
sizeAVector3

Full size of box A

posBVector3
sizeBVector3

Full size of box B

Returns:
Type: 
Vector3 | undefined

(static) collideSphereBox(pos, radius, boxPos, boxSize) → {Vector3|undefined}

Returns the vector to move a sphere out of an axis aligned box, or undefined

Parameters:
NameTypeDescription
posVector3

Sphere center

radiusnumber
boxPosVector3
boxSizeVector3

Full size of the box

Returns:
Type: 
Vector3 | undefined

(static) collideSphereCylinder(pos, radius, cylinderPos, cylinderRadius, cylinderHeight) → {Vector3|undefined}

Returns the vector to move a sphere out of a vertical cylinder, or undefined

Parameters:
NameTypeDescription
posVector3

Sphere center

radiusnumber
cylinderPosVector3
cylinderRadiusnumber
cylinderHeightnumber

Full height along Y

Returns:
Type: 
Vector3 | undefined

(static) collideSphereInBox(pos, radius, boxPos, boxSize) → {Vector3|undefined}

Returns the vector to move a sphere back inside an axis aligned box, or undefined when it is all inside

  • The inside out twin of collideSphereBox, for keeping things in a room or an arena
  • A sphere too big for the box on some axis is held at the middle of it on that axis
Parameters:
NameTypeDescription
posVector3

Sphere center

radiusnumber
boxPosVector3
boxSizeVector3

Full size of the box

Returns:
Type: 
Vector3 | undefined

(static) collideSphereSphere(posA, radiusA, posB, radiusB) → {Vector3|undefined}

Returns the vector to move sphere A by so it no longer overlaps sphere B, or undefined

Parameters:
NameTypeDescription
posAVector3
radiusAnumber
posBVector3
radiusBnumber
Returns:
Type: 
Vector3 | undefined

(static) isOverlapping3D(posA, sizeA, posB, sizeBopt) → {boolean}

Check if two axis aligned boxes are overlapping, touching edges do not overlap

Parameters:
NameTypeAttributesDescription
posAVector3
sizeAVector3

Full size of box A

posBVector3
sizeBVector3<optional>

Full size of box B, zero for a point

Returns:
Type: 
boolean

(static) isPointInBox3D(point, pos, size) → {boolean}

Check if a point is inside an axis aligned box, boundary is inclusive

Parameters:
NameTypeDescription
pointVector3
posVector3

Center of the box

sizeVector3

Full size of the box

Returns:
Type: 
boolean

(static) isVector3(v) → {boolean}

Check if the object is a valid Vector3

Parameters:
NameTypeDescription
vany
Returns:
Type: 
boolean

(static) randInSphere(radiusopt, minRadiusopt) → {Vector3}

Returns a random Vector3 inside a sphere, spread evenly through its volume, the 3D twin of randInCircle

Parameters:
NameTypeAttributesDefaultDescription
radiusnumber<optional>
1
minRadiusnumber<optional>
0

Leave a hollow middle this big

Returns:
Type: 
Vector3

(static) randVector3(lengthopt, coneAngleopt) → {Vector3}

Returns a random Vector3 of a given length, pointing any direction evenly, or within a cone around +Y

Parameters:
NameTypeAttributesDefaultDescription
lengthnumber<optional>
1
coneAnglenumber<optional>

Half angle of the cone around +Y in radians, PI is every direction

Returns:
Type: 
Vector3

(static) raycastBox(ray, pos, size) → {number|undefined}

Returns the distance along the ray to the first intersection with an axis aligned box, or undefined

  • The hit is ray.getPosition(distance), a direction that is not unit length scales the distance
  • A ray starting inside the box is already there, so it gets back 0
Parameters:
NameTypeDescription
rayRay3D
posVector3

Center of the box

sizeVector3

Full size of the box

Returns:
Type: 
number | undefined

(static) raycastPlane(ray, planePos, planeNormal) → {number|undefined}

Returns the distance along the ray to a plane, or undefined if parallel or behind

  • The hit is ray.getPosition(distance), a direction that is not unit length scales the distance
Parameters:
NameTypeDescription
rayRay3D
planePosVector3
planeNormalVector3
Returns:
Type: 
number | undefined

(static) raycastSphere(ray, pos, radius) → {number|undefined}

Returns the distance along the ray to the first intersection with a sphere, or undefined

  • The hit is ray.getPosition(distance), a direction that is not unit length scales the distance
  • A ray starting inside the sphere is already there, so it gets back 0
Parameters:
NameTypeDescription
rayRay3D
posVector3

Sphere center

radiusnumber
Returns:
Type: 
number | undefined

(static) vec3(xopt, yopt, zopt) → {Vector3}

Create a 3D vector, can take 0, 1, 2 or 3 numbers

  • vec3() is zero, vec3(s) fills all three, vec3(x, y) sets z to 0
Parameters:
NameTypeAttributesDefaultDescription
xnumber<optional>
0
ynumber<optional>
znumber<optional>
Returns:
Type: 
Vector3