Skip to content

@web-engine-dev/math


@web-engine-dev/math / Sphere

Class: Sphere ​

A bounding sphere defined by a center point and radius. Immutable by design - all operations return new instances.

Bounding spheres are the fastest primitive for broad-phase culling:

  • Frustum-sphere test is 6 dot products
  • Sphere-sphere test is 1 distance comparison
  • Rotation-invariant (no need to recompute on rotation)

Example ​

ts
const sphere = new Sphere(new Vec3(0, 5, 0), 2);
if (sphere.containsPoint(point)) { ... }
if (sphere.intersectsSphere(otherSphere)) { ... }

Constructors ​

Constructor ​

new Sphere(center?, radius?): Sphere

Parameters ​

center? ​

Vec3 = ...

radius? ​

number = 0

Returns ​

Sphere

Properties ​

center ​

readonly center: Vec3


radius ​

readonly radius: number

Accessors ​

diameter ​

Get Signature ​

get diameter(): number

Diameter: 2r

Returns ​

number


surfaceArea ​

Get Signature ​

get surfaceArea(): number

Surface area: 4πr²

Returns ​

number


volume ​

Get Signature ​

get volume(): number

Volume: 4/3 πr³

Returns ​

number

Methods ​

closestPoint() ​

closestPoint(point): Vec3

Returns the closest point on the sphere surface to a given point.

Parameters ​

point ​

Vec3

Returns ​

Vec3


containsPoint() ​

containsPoint(point): boolean

Checks if a point is inside the sphere.

Parameters ​

point ​

Vec3

Returns ​

boolean


containsSphere() ​

containsSphere(sphere): boolean

Checks if the sphere fully contains another sphere.

Parameters ​

sphere ​

Sphere

Returns ​

boolean


distanceToPoint() ​

distanceToPoint(point): number

Returns the distance from a point to the sphere surface (negative if inside).

Parameters ​

point ​

Vec3

Returns ​

number


equals() ​

equals(sphere, epsilon?): boolean

Parameters ​

sphere ​

Sphere

epsilon? ​

number = 0

Returns ​

boolean


expand() ​

expand(amount): Sphere

Returns a sphere expanded by the given amount.

Parameters ​

amount ​

number

Returns ​

Sphere


expandByPoint() ​

expandByPoint(point): Sphere

Returns a sphere that encloses this sphere and a point.

Parameters ​

point ​

Vec3

Returns ​

Sphere


intersectsAABB() ​

intersectsAABB(box): boolean

Checks if the sphere intersects an AABB.

Parameters ​

box ​

AABB

Returns ​

boolean


intersectsPlane() ​

intersectsPlane(plane): -1 | 0 | 1

Checks which side of a plane the sphere is on.

Parameters ​

plane ​

Plane

Returns ​

-1 | 0 | 1

1 = entirely on positive side, -1 = entirely on negative side, 0 = intersecting.


intersectsSphere() ​

intersectsSphere(sphere): boolean

Checks if the sphere intersects another sphere.

Parameters ​

sphere ​

Sphere

Returns ​

boolean


merge() ​

merge(other): Sphere

Returns a sphere that encloses both this sphere and another.

Parameters ​

other ​

Sphere

Returns ​

Sphere


toAABB() ​

toAABB(): AABB

Returns the AABB that encloses this sphere.

Returns ​

AABB


toString() ​

toString(): string

Returns ​

string


transform() ​

transform(matrix): Sphere

Transforms the sphere by a matrix. The radius is scaled by the maximum axis scale.

Parameters ​

matrix ​

Mat4

Returns ​

Sphere


translate() ​

translate(offset): Sphere

Returns a sphere translated by a vector.

Parameters ​

offset ​

Vec3

Returns ​

Sphere


from() ​

static from(center, radius): Sphere

Creates a sphere from center and radius.

Parameters ​

center ​

Vec3

radius ​

number

Returns ​

Sphere


fromAABB() ​

static fromAABB(box): Sphere

Creates a bounding sphere that encloses an AABB.

Parameters ​

box ​

AABB

Returns ​

Sphere


fromPoints() ​

static fromPoints(points): Sphere

Computes a bounding sphere from an array of points using Ritter's algorithm. Not guaranteed to be optimal but runs in O(n) and produces tight bounds.

Parameters ​

points ​

Vec3[]

Returns ​

Sphere

Proprietary software. All rights reserved.