LittleJS 1.30.0

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

Functions

Functions

buildMatrix(pos, rotation, scale, matrix) → Matrix4math3d.js:589

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
pos Vector3 optional
rotation Vector3 optional

vec3(pitch, yaw, roll) in radians

scale Vector3 optional
matrix Matrix4 optional

Written into instead of a new one, for a loop that builds many

collideBoxBox3D(posA, sizeA, posB, sizeB, rotationA, rotationB) → Vector3|undefinedmath3d.js:872

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
  • Turned boxes are tested by the separating axis test, the push is along the direction they overlap least on
posA Vector3
sizeA Vector3

Full size of box A

posB Vector3
sizeB Vector3

Full size of box B

rotationA Vector3 optional

How box A is turned, upright when left out

rotationB Vector3 optional

How box B is turned

collideSphereBox(pos, radius, boxPos, boxSize, boxRotation) → Vector3|undefinedmath3d.js:771

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

pos Vector3

Sphere center

radius number
boxPos Vector3
boxSize Vector3

Full size of the box

boxRotation Vector3 optional

How the box is turned, upright when left out

collideSphereCylinder(pos, radius, cylinderPos, cylinderRadius, cylinderHeight) → Vector3|undefinedmath3d.js:832

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

pos Vector3

Sphere center

radius number
cylinderPos Vector3
cylinderRadius number
cylinderHeight number

Full height along Y

collideSphereInBox(pos, radius, boxPos, boxSize) → Vector3|undefinedmath3d.js:800

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
pos Vector3

Sphere center

radius number
boxPos Vector3
boxSize Vector3

Full size of the box

collideSphereSphere(posA, radiusA, posB, radiusB) → Vector3|undefinedmath3d.js:749

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

posA Vector3
radiusA number
posB Vector3
radiusB number
isOverlapping3D(posA, sizeA, posB, sizeB, rotationA, rotationB) → booleanmath3d.js:730

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

posA Vector3
sizeA Vector3

Full size of box A

posB Vector3
sizeB Vector3 optional

Full size of box B, zero for a point

rotationA Vector3 optional

How box A is turned, upright when left out

rotationB Vector3 optional

How box B is turned

isPointInBox3D(point, pos, size, rotation) → booleanmath3d.js:710

Check if a point is inside a box, boundary is inclusive

point Vector3
pos Vector3

Center of the box

size Vector3

Full size of the box

rotation Vector3 optional

How the box is turned, upright when left out

isVector3(v) → booleanmath3d.js:33

Check if the object is a valid Vector3

v any
randInSphere(radius, minRadius) → Vector3math3d.js:59

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

radius number optional, default 1
minRadius number optional, default 0

Leave a hollow middle this big

randVector3(length, coneAngle) → Vector3math3d.js:45

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

length number optional, default 1
coneAngle number optional

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

raycastBox(ray, pos, size, rotation) → number|undefinedmath3d.js:982

Returns the distance along the ray to the first intersection with a 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
ray Ray3D
pos Vector3

Center of the box

size Vector3

Full size of the box

rotation Vector3 optional

How the box is turned, upright when left out

raycastPlane(ray, planePos, planeNormal) → number|undefinedmath3d.js:961

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
ray Ray3D
planePos Vector3
planeNormal Vector3
raycastSphere(ray, pos, radius) → number|undefinedmath3d.js:933

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
ray Ray3D
pos Vector3

Sphere center

radius number
vec3(x, y, z) → Vector3math3d.js:22

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
x number optional, default 0
y number optional
z number optional