spatial/collider

Collision volumes for spatial queries and collision detection.

Provides Box, Sphere, Capsule, and Cylinder colliders with intersection tests and rotation-aware collision volume computation.

Types

pub type Collider =
  @internal InternalCollider

Values

pub fn box(
  min min: vec3.Vec3(Float),
  max max: vec3.Vec3(Float),
) -> Collider

Create a box collider from min and max points.

Example

let bounds = collider.box(
  min: vec3.Vec3(-1.0, -1.0, -1.0),
  max: vec3.Vec3(1.0, 1.0, 1.0),
)
pub fn box_from_center(
  center center: vec3.Vec3(Float),
  half_extents half_extents: vec3.Vec3(Float),
) -> Collider

Create a box collider from center and half-extents.

Example

let bounds = collider.box_from_center(
  center: vec3.Vec3(0.0, 5.0, 0.0),
  half_extents: vec3.Vec3(2.0, 1.0, 2.0),
)
pub fn capsule(
  start start: vec3.Vec3(Float),
  end end: vec3.Vec3(Float),
  radius radius: Float,
) -> Collider

Create a capsule collider from start point, end point, and radius.

A capsule is a line segment with a radius - perfect for character controllers.

Example

let character = collider.capsule(
  start: vec3.Vec3(0.0, 0.0, 0.0),
  end: vec3.Vec3(0.0, 2.0, 0.0),
  radius: 0.5,
)
pub fn center(collider: Collider) -> vec3.Vec3(Float)

Get the center of a collider.

pub fn contains_point(
  collider: Collider,
  point: vec3.Vec3(Float),
) -> Bool

Check if a point is inside a collider.

Works for Box, Sphere, Capsule, and Cylinder colliders.

Time Complexity: O(1) - constant time geometric calculation.

pub fn cylinder(
  center center: vec3.Vec3(Float),
  radius radius: Float,
  height height: Float,
) -> Collider

Create a cylinder collider from center, radius, and height.

The cylinder is aligned along the Y axis in local space.

Example

let pillar = collider.cylinder(
  center: vec3.Vec3(0.0, 5.0, 0.0),
  radius: 1.0,
  height: 10.0,
)
pub fn from_rotation(
  collider: Collider,
  position position: vec3.Vec3(Float),
  rotation rotation: quaternion.Quaternion,
  scale scale: vec3.Vec3(Float),
) -> Collider

Create a new collider from a local-space collider with position, rotation, and scale.

For Box: Computes a new axis-aligned bounding box that encompasses all 8 corners after rotation, translation, and scaling.

For Sphere: Transforms the center and scales the radius by the maximum scale component (since spheres remain spherical under uniform scaling).

Time Complexity: O(1) - transforms a constant number of points (8 for Box).

Example

// Local space box
let local_box = collider.box(
  min: vec3.Vec3(-1.0, -1.0, -1.0),
  max: vec3.Vec3(1.0, 1.0, 1.0),
)

// Rotated 45 degrees around Y axis
let rotation = q.from_axis_angle(vec3.Vec3(0.0, 1.0, 0.0), 0.785)

// Get world-space collider
let world_box = collider.from_rotation(
  local_box,
  position: vec3.Vec3(5.0, 0.0, 0.0),
  rotation: rotation,
  scale: vec3.Vec3(1.0, 1.0, 1.0),
)
pub fn intersects(a: Collider, b: Collider) -> Bool

Check if two colliders intersect.

Handles all collider type combinations.

Time Complexity: O(1) - constant time geometric calculation.

pub fn size(collider: Collider) -> vec3.Vec3(Float)

Get the size (dimensions) of a collider.

Returns the bounding box dimensions for all collider types.

pub fn sphere(
  center center: vec3.Vec3(Float),
  radius radius: Float,
) -> Collider

Create a sphere collider from center and radius.

Example

let bounds = collider.sphere(
  center: vec3.Vec3(0.0, 0.0, 0.0),
  radius: 2.5,
)
Search Document