gjk

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package gjk implements the Gilbert-Johnson-Keerthi (GJK) algorithm for collision detection.

GJK detects whether two convex shapes overlap by testing if their Minkowski difference contains the origin. The algorithm builds a simplex incrementally, converging toward the origin in typically 3-6 iterations.

For detailed algorithm explanation with pseudocode and visual examples, see: ALGORITHMS.md - "GJK Algorithm" section

References:

  • Gilbert, Johnson, Keerthi: "A Fast Procedure for Computing the Distance Between Complex Objects in Three-Dimensional Space" (1988)
  • Van den Bergen: "Collision Detection in Interactive 3D Environments" (2003)

Index

Constants

This section is empty.

Variables

View Source
var SimplexPool = sync.Pool{
	New: func() interface{} {
		return &Simplex{}
	},
}

Functions

func GJK

func GJK(a, b *actor.RigidBody, simplex *Simplex) bool

GJK performs collision detection between two convex rigid bodies.

Algorithm overview:

  1. Start with initial search direction (toward B from A)
  2. Get first support point in Minkowski difference
  3. Iteratively refine simplex toward origin
  4. If origin is contained → collision
  5. If can't reach origin → no collision

Typical convergence: 3-6 iterations for most shapes.

Returns:

  • bool: true if collision detected, false otherwise

The simplex is modified in place and contains 1-4 points. For collisions, it's always a tetrahedron (4 points) containing the origin, which EPA uses as its initial polytope.

func MinkowskiSupport

func MinkowskiSupport(a, b *actor.RigidBody, direction mgl64.Vec3) mgl64.Vec3

MinkowskiSupport computes a support point in the Minkowski difference (A - B).

The Minkowski difference A - B is the set of all vectors (a - b) where a ∈ A and b ∈ B. For collision detection, we only need the extreme points (support points) in any direction.

Parameters:

  • a, b: The two rigid bodies to test
  • direction: The direction to find the furthest point

Returns:

Support point: furthestPoint(A, direction) - furthestPoint(B, -direction)

This is the fundamental query that makes GJK work for any convex shape - shapes only need to implement a Support() function, not expose their full geometry.

Types

type Simplex

type Simplex struct {
	Points [4]mgl64.Vec3
	Count  int
}

Simplex represents a set of 1-4 points in the Minkowski difference space. The simplex evolves during GJK iterations, always containing the most recent support points. Size progression: 1 point → 2 points (line) → 3 points (triangle) → 4 points (tetrahedron)

func (*Simplex) Reset

func (s *Simplex) Reset()

Jump to

Keyboard shortcuts

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