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
- Source
Classes
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
| Name | Type | Attributes | Description |
|---|---|---|---|
pos | Vector3 | <optional> | |
rotation | Vector3 | <optional> | vec3(pitch, yaw, roll) in radians |
scale | Vector3 | <optional> |
- Source
- 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
| Name | Type | Description |
|---|---|---|
posA | Vector3 | |
sizeA | Vector3 | Full size of box A |
posB | Vector3 | |
sizeB | Vector3 | Full size of box B |
- Source
- 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
| Name | Type | Description |
|---|---|---|
pos | Vector3 | Sphere center |
radius | number | |
boxPos | Vector3 | |
boxSize | Vector3 | Full size of the box |
- Source
- 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
| Name | Type | Description |
|---|---|---|
pos | Vector3 | Sphere center |
radius | number | |
cylinderPos | Vector3 | |
cylinderRadius | number | |
cylinderHeight | number | Full height along Y |
- Source
- 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
| Name | Type | Description |
|---|---|---|
pos | Vector3 | Sphere center |
radius | number | |
boxPos | Vector3 | |
boxSize | Vector3 | Full size of the box |
- Source
- 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
| Name | Type | Description |
|---|---|---|
posA | Vector3 | |
radiusA | number | |
posB | Vector3 | |
radiusB | number |
- Source
- Type:
- Vector3 |
undefined
(static) isOverlapping3D(posA, sizeA, posB, sizeBopt) → {boolean}
Check if two axis aligned boxes are overlapping, touching edges do not overlap
| Name | Type | Attributes | Description |
|---|---|---|---|
posA | Vector3 | ||
sizeA | Vector3 | Full size of box A | |
posB | Vector3 | ||
sizeB | Vector3 | <optional> | Full size of box B, zero for a point |
- Source
- Type:
- boolean
(static) isPointInBox3D(point, pos, size) → {boolean}
Check if a point is inside an axis aligned box, boundary is inclusive
| Name | Type | Description |
|---|---|---|
point | Vector3 | |
pos | Vector3 | Center of the box |
size | Vector3 | Full size of the box |
- Source
- Type:
- boolean
(static) isVector3(v) → {boolean}
Check if the object is a valid Vector3
| Name | Type | Description |
|---|---|---|
v | any |
- Source
- Type:
- boolean
(static) randInSphere(radiusopt, minRadiusopt) → {Vector3}
Returns a random Vector3 inside a sphere, spread evenly through its volume, the 3D twin of randInCircle
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
radius | number | <optional> | 1 | |
minRadius | number | <optional> | 0 | Leave a hollow middle this big |
- Source
- 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
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
length | number | <optional> | 1 | |
coneAngle | number | <optional> | Half angle of the cone around +Y in radians, PI is every direction |
- Source
- 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
| Name | Type | Description |
|---|---|---|
ray | Ray3D | |
pos | Vector3 | Center of the box |
size | Vector3 | Full size of the box |
- Source
- 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
| Name | Type | Description |
|---|---|---|
ray | Ray3D | |
planePos | Vector3 | |
planeNormal | Vector3 |
- Source
- 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
| Name | Type | Description |
|---|---|---|
ray | Ray3D | |
pos | Vector3 | Sphere center |
radius | number |
- Source
- 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
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
x | number | <optional> | 0 | |
y | number | <optional> | ||
z | number | <optional> |
- Source
- Type:
- Vector3