Home > @gglib/math

math package

A 3d math library

Remarks

This package implements common structures used in 3D computations like

The api offers various operations among these structures as static functions and as instance methods. All structures share the same API design principles

Functions starting with create do create a new instance of a structure, for example:

Vec3.create(1, 2, 3)
Vec3.createZero()

Function starting with init do initialize an existing instance or object

Vec3.init({}, 1, 2, 3) // initializes the given object `{}`
Vec3.init(v, 1, 2, 3)  // initializes the given instance `v`
v.init(1, 2, 3)        // initializes the instance `v`

Operations as instance methods modify the instance

v1.multiply(v2) // writes the result into `v1`

Operations as static methods create a new instance or write the result to the optional out parameter

Vec3.multiply(v1, v2)     // creates and returns a new Vec3 instance
Vec3.multiply(v1, v2, v3) // writes result into the given instance `v3` and retruns it
Vec3.multiply(v1, v2, {}) // writes result into the given instance `{}` and returns it

Classes

Class Description
BoundingBox An axis aligned box volume.
BoundingCapsule
BoundingFrustum Describes a frustum volume
BoundingSphere Defines sphere volume.
Mat2 A 2x2 matrix using column major layout
Mat3 A 3x3 matrix using column major layout
Mat4 A 4x4 matrix using column major layout
Plane An infinite plane
Quat A quaternion.
Ray A ray with a starting position and a pointing direction.
Rect A rectangle.
Vec2 A vector with two components.
Vec3 A vector with three components.
Vec4 A vector with four components.

Enumerations

Enumeration Description
BoundingFrustumPlane Enumeration of bounding frustum planes
IntersectionType Enumeration of intersection types
PlaneIntersectionType Enumeration of plane intersection types

Functions

Function Description
boxContainsBox(min1, max1, min2, max2) Checks whether a box contains another box volume
boxContainsCapsule(boxMin, boxMax, capsuleStart, capsuleEnd, capsuleR) Checks whether a box contains a capsule volume
boxContainsFrustum(min, max, frustum) Checks whether a box contains a frustum volume
boxContainsSphere(min, max, center, radius) Checks whether a box contains a sphere volume
boxIntersectBox(min1, max1, min2, max2) Checks whether a box intersects another box
boxIntersectsCapsule(boxMin, boxMax, capsuleStart, capsuleEnd, capsuleR) Checks whether a box intersects a capsule volume
boxIntersectSphere(min, max, center, radius) Checks whether a box intersects a sphere
boxIntersectsPlane(min, max, plane) Checks whether a box intersects a plane
boxIntersectsPoint(min, max, point) Checks whether a box intersects a point
clamp(value, min, max)
closestPointOnPlane(point, plane, out) Calculates the point on a plane that is the closest to a given point
closestPointOnSegment(point, segmentStart, segmentEnd, out) Calculates a point on a line segment that is closest to a given point
closestPointOnTriangle(point, a, b, c, out) Calculates a point on triangle that is closest to a given point
closestPointsOfSegments(segment1Start, segment1End, segment2Start, segment2End, outP1, outP2) Calculates the closest two points between two segments
distancePlaneToPoint(plane, point)
distanceSquaredPointToSegment(a, b, c)
easeInCubic(t)
easeInOutCubic(t)
easeInOutQuad(t)
easeInOutQuart(t)
easeInOutQuint(t)
easeInQuad(t)
easeInQuart(t)
easeInQuint(t)
easeLinear(t)
easeOutCubic(t)
easeOutQuad(t)
easeOutQuart(t)
easeOutQuint(t)
frustumContainsBox(frustum, min, max) Checks whether a frustum contains a box volume
frustumContainsSphere(frustum, center, radius) Checks whether a frustum contains a sphere volume
frustumIntersectsBox(frustum, min, max) Checks whether a frustum intersects a box
frustumIntersectsPlane(frustum, plane) Checks whether a frustum intersects a plane
frustumIntersectsPoint(frustum, point) Checks whether a frustum intersects a point
frustumIntersectsSphere(frustum, center, radius) Checks whether a frustum intersects a sphere
hermite(v1, t1, v2, t2, s)
lerp(a, b, t)
planeIntersectsBox(plane, boxMin, boxMax) Checks for intersection between a plane and a box
planeIntersectsCapsule(plane, capsuleStart, capsuleEnd, capsuleR) Checks for intersection between a plane and a capsule
planeIntersectsFrustum(plane, frustum) Checks for intersection between a plane and frustum
planeIntersectsPoint(plane, point) Checks for intersection between a plane and a 3d point
planeIntersectsSphere(plane, center, radius) Checks for intersection between a plane and a sphere
planePlaneIntersection(plane1, plane2, outPosition, outDirection) Calculates the intersection edge between two planes
planePlanePlaneIntersection(p1, p2, p3, out) Calculates the intersection point between three planes
rayIntersectsBox(rayPos, rayDir, boxMin, boxMax) Checks whether a ray intersects an axis aligned bounding box
rayIntersectsBoxAt(rayPos, rayDir, boxMin, boxMax) Calculates the distance where a ray intersects an axis aligned bounding box
rayIntersectsPlane(orig, dir, plane) Checks whether a ray intersects a plane
rayIntersectsPlaneAt(orig, dir, plane) Calculates the distance where a ray intersects a plane
rayIntersectsSphere(orig, dir, center, radius) Checks whether a ray intersects a plane
rayIntersectsSphereAt(orig, dir, center, radius) Calculates the distance where a ray intersects a sphere
rayIntersectsTriangle(orig, dir, v0, v1, v2) Checks whether a ray intersects a triangle
rayIntersectsTriangleAt(orig, dir, v0, v1, v2) Calculates the distance where a ray intersects a triangle
sphereContainsBox(center, radius, min, max) Checks whether a sphere contains a box volume
sphereContainsCapsule(sphereCenter, sphereRadius, capsuleStart, capsuleEnd, capsuleRadius) Checks whether a sphere contains a capsule volume
sphereContainsFrustum(center, radius, frustum) Checks whether a sphere contains a frustum volume
sphereContainsSphere(c1, r1, c2, r2) Checks whether a sphere contains another sphere volume
sphereIntersectsCapsule(sphereCenter, sphereRaidus, capsuleStart, capsuleEnd, capsuleRadius) Checks whether a sphere intersects a capsule
sphereIntersectsPlane(center, radius, plane) Checks whether a sphere intersects a plane
sphereIntersectsPoint(center, radius, point) Checks whether a sphere intersects a point
sphereIntersectsSphere(c1, r1, c2, r2) Checks wphether a sphere intersects another sphere
sphereIntersectsTriangle(center, radius, v0, v1, v2) Checks whether a sphere intersects a triangle
toDegrees(radians)
toRadians(degrees)
transformBox(box, transform, out)
transformCapsule(capsule, transform, out)
transformFrustum(frustum, transform, out)
transformPlane(plane, transform, out)
transformSphere(sphere, transform, out)

Interfaces

Interface Description
ArrayLike_2 Something that looks like an array
BoundingVolume
IMat An object holding an array of numbers, intended to be used as a matrix
IPoint A 2 dimensional point.
IRect A rectangle that is composed of a IPoint and a ISize.
ISize An object having width and height properties.
IVec2 A 2 dimensional vector.
IVec3 A 3 dimensional vector.
IVec4 A 4 dimensional vector.

Type Aliases

Type Alias Description
Mat2Data
Mat3Data
Mat4Data