Skip to content

Sphere

Represents a bounding sphere in 3D space.

Sphere stores a center point and a radius and is used as a simple bounding volume for intersection tests, culling, and spatial queries. A negative radius indicates an empty sphere. The class supports expansion with points, merging with other spheres, translation, and transformation by a matrix.

Construction

Constructors


Sphere() constexpr

Constructs a sphere from a center and radius.

cpp
Sphere::Sphere(const Vector3 center, float radius);
ParameterDescription
centerSphere center.
radiusSphere radius (negative indicates empty).

Properties

center Vector3

Sphere center in space.

cpp
Vector3 center {Vector3::Zero()};

radius float

Sphere radius. A negative value indicates an empty sphere.

cpp
float radius {-1.0f};

Functions

ApplyTransform() void

Applies a 4×4 transform to the sphere.

The center is transformed directly, while the radius is scaled by the largest scale factor present in the matrix to ensure conservative bounding behavior.

cpp
void Sphere::ApplyTransform(const Matrix4& transform);
ParameterDescription
transformMatrix to apply.

ExpandWithPoint() void

Expands the sphere to include a point.

If the sphere is empty, it becomes a zero-radius sphere at the point. Otherwise, the sphere grows minimally to contain the point.

cpp
void Sphere::ExpandWithPoint(const Vector3& p);
ParameterDescription
pPoint to include.

IsEmpty() bool

Checks whether the sphere is empty.

cpp
bool Sphere::IsEmpty() const;

Radius() float

Returns the sphere radius.

cpp
float Sphere::Radius() const;

Reset() void

Resets the sphere to an empty state.

cpp
void Sphere::Reset();

Translate() void

Translates the sphere by a vector.

cpp
void Sphere::Translate(const Vector3& translation);
ParameterDescription
translationOffset to apply.

Union() void

Expands this sphere to fully contain another sphere.

Handles empty spheres, identical centers, and general cases using two directional expansion points.

cpp
void Sphere::Union(const Sphere& other);
ParameterDescription
otherSphere to merge.

Released under the MIT License.