SaturnRingLibrary 0.9.1
SGL wrapper
 
Loading...
Searching...
No Matches
SRL::Math::Types::Sphere Class Reference

Detailed Description

High-performance sphere primitive for collision detection and bounding volume applications.

The Sphere class represents a perfect mathematical sphere in 3D space, defined by a center point and radius. It provides optimized intersection tests with various geometric primitives and serves as an efficient bounding volume for complex objects.

Key features:

  • Memory-efficient representation (center point + radius)
  • Constant-time intersection tests with common primitives
  • Fixed-point arithmetic for consistent behavior across platforms
  • Inheritance from Shape for polymorphic usage in collision systems

Performance characteristics:

  • Sphere-sphere tests: O(1) complexity, very fast
  • Sphere-AABB tests: O(1) complexity, slightly more expensive than sphere-sphere
  • Sphere-plane tests: O(1) complexity, very fast
  • Sphere-point tests: O(1) complexity, extremely fast

Common applications:

  • Bounding volume for complex meshes
  • Collision detection in physics simulations
  • Level-of-detail calculations
  • Spatial partitioning acceleration structures
  • Fast rejection tests in ray tracing

Implementation notes:

  • All intersection tests use squared distances where possible to avoid expensive square root operations
  • The radius is stored as a fixed-point value for consistent precision
  • Inheritance from Shape allows polymorphic usage in collision systems
Note
When using spheres as bounding volumes for complex objects, consider the trade-off between tightness of fit and computational efficiency. Spheres provide the fastest intersection tests but may not approximate elongated objects as well as other bounding volumes like AABBs or OBBs.
See also
AABB For axis-aligned bounding box alternative
Shape For the base class interface
Plane For plane intersection tests

#include <sphere.hpp>

Inheritance diagram for SRL::Math::Types::Sphere:
SRL::Math::Types::Shape

Public Member Functions

constexpr Sphere (const Vector3D &center, const Fxp &radius)
 Creates sphere from center and radius.
 
bool Contains (const Vector3D &point) const
 Tests if a point is inside the sphere.
 
constexpr AABB GetBoundingBox () const
 Calculates bounding box containing sphere.
 
constexpr Vector3D GetPosition () const
 Gets shape center position.
 
constexpr Fxp GetRadius () const
 Gets sphere radius.
 
template<Precision P = Precision::Default>
bool Intersects (const AABB &box) const
 Tests intersection with AABB.
 
bool Intersects (const Plane &plane) const override
 Tests intersection with plane.
 
bool Intersects (const Sphere &other) const
 Tests intersection with another sphere.
 
constexpr void SetPosition (const Vector3D &pos)
 Sets shape center position.
 

Protected Attributes

Vector3D position