geometry

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package geometry provides allocation-free 3D collision primitives: vectors, quaternions, capsules, spheres, AABBs, OBBs, rays, triangles, and pairwise + swept intersection tests. All operations are pass-by-value to stay zero-allocation on hot paths.

Used by kit/spatial (broadphase indices), kit/game/lagcomp (hit traces), kit/unreal/collision (server-side level geometry queries), and any non-game consumer that needs basic 3D geometry (robotics, AR, GIS).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AABBOverlap

func AABBOverlap(a, b AABB) bool

AABBOverlap reports whether two AABBs overlap on all axes.

func OBBOverlap

func OBBOverlap(a, b OBB) bool

OBBOverlap reports whether two oriented bounding boxes overlap using the Separating Axis Theorem. Tests all 15 candidate axes (3 from each OBB's local frame + 9 cross products).

func RayAABB

func RayAABB(r Ray, b AABB) (tMin, tMax float32, ok bool)

RayAABB returns the entry and exit parameters along r for box b. ok=false means the ray misses; tMin > tMax can also be checked by the caller.

func RaySphere

func RaySphere(r Ray, s Sphere) (float32, bool)

RaySphere returns the closest positive intersection distance and ok=true, or ok=false if the ray misses.

func SphereSphere

func SphereSphere(a, b Sphere) bool

SphereSphere reports whether two spheres overlap.

func SweptCapsuleStatic

func SweptCapsuleStatic(c Capsule, motion Vec3, world []Tri) (float32, bool)

SweptCapsuleStatic sweeps a capsule by motion against a set of static triangles. Returns the earliest time-of-impact in [0, 1] and whether a hit occurred. Conservative implementation: tests the bounding sphere (capsule midpoint, longest-extent + radius) per triangle; sufficient for projectile/melee adjudication where motion magnitudes are modest.

func SweptSphereTriangles

func SweptSphereTriangles(s Sphere, motion Vec3, world []Tri) (float32, bool)

SweptSphereTriangles sweeps a sphere by motion against a set of static triangles, returning the earliest time-of-impact in [0, 1] and a hit flag. Conservative: uses the swept-sphere-vs-triangle test pairwise.

func TriTriOverlap

func TriTriOverlap(t1, t2 Tri) bool

TriTriOverlap reports whether two triangles overlap, using the Möller-style coplanarity + edge-test approach. Reasonably fast; not the absolute fastest known algorithm but clear and correct.

Types

type AABB

type AABB struct {
	Min, Max Vec3
}

func (AABB) Center

func (b AABB) Center() Vec3

Center of the AABB.

func (AABB) Contains

func (b AABB) Contains(p Vec3) bool

Contains returns true if p lies inside b (inclusive on the boundary).

func (AABB) Expand

func (b AABB) Expand(margin float32) AABB

Expand returns a new AABB grown by margin along each axis.

func (AABB) HalfExtents

func (b AABB) HalfExtents() Vec3

HalfExtents of the AABB.

func (AABB) Union

func (b AABB) Union(o AABB) AABB

Union returns the smallest AABB enclosing both.

type Capsule

type Capsule struct {
	A, B   Vec3
	Radius float32
}

type OBB

type OBB struct {
	Center      Vec3
	HalfExtents Vec3
	Rotation    Quat
}

OBB is an oriented bounding box centred at Center with the given half-extents along its local axes, rotated by Rotation.

type Quat

type Quat [4]float32

Quat is a unit quaternion stored as (x, y, z, w). The identity rotation is Quat{0, 0, 0, 1}.

func QuatFromAxisAngle

func QuatFromAxisAngle(axis Vec3, radians float32) Quat

func QuatIdentity

func QuatIdentity() Quat

func (Quat) Conjugate

func (q Quat) Conjugate() Quat

func (Quat) Mul

func (q Quat) Mul(r Quat) Quat

func (Quat) Rotate

func (q Quat) Rotate(v Vec3) Vec3

Rotate applies the quaternion rotation to v.

type Ray

type Ray struct {
	Origin, Dir Vec3
}

type Sphere

type Sphere struct {
	Center Vec3
	Radius float32
}

type Tri

type Tri struct {
	A, B, C Vec3
}

type Vec3

type Vec3 [3]float32

Vec3 is a 3D vector. Components are float32 for cache friendliness in game-server hot paths where millions of operations per tick are common. Use float64 conversions explicitly when interfacing with stdlib math.

func CapsuleCapsule

func CapsuleCapsule(a, b Capsule) (bool, Vec3)

CapsuleCapsule reports whether two capsules overlap and returns the midpoint of the closest segment pair as the contact point.

func RayTriangle

func RayTriangle(r Ray, t Tri) (Vec3, float32, bool)

RayTriangle implements Möller–Trumbore. Returns hit point, distance, and whether the triangle was hit.

func V3

func V3(x, y, z float32) Vec3

func (Vec3) Add

func (a Vec3) Add(b Vec3) Vec3

func (Vec3) Cross

func (a Vec3) Cross(b Vec3) Vec3

func (Vec3) Distance

func (a Vec3) Distance(b Vec3) float32

func (Vec3) DistanceSq

func (a Vec3) DistanceSq(b Vec3) float32

func (Vec3) Div

func (a Vec3) Div(s float32) Vec3

func (Vec3) Dot

func (a Vec3) Dot(b Vec3) float32

func (Vec3) Len

func (a Vec3) Len() float32

func (Vec3) LenSq

func (a Vec3) LenSq() float32

func (Vec3) Lerp

func (a Vec3) Lerp(b Vec3, t float32) Vec3

Lerp returns a*(1-t) + b*t.

func (Vec3) Mul

func (a Vec3) Mul(s float32) Vec3

func (Vec3) Neg

func (a Vec3) Neg() Vec3

func (Vec3) Normalize

func (a Vec3) Normalize() Vec3

func (Vec3) Sub

func (a Vec3) Sub(b Vec3) Vec3

func (Vec3) X

func (a Vec3) X() float32

func (Vec3) Y

func (a Vec3) Y() float32

func (Vec3) Z

func (a Vec3) Z() float32

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL