GJK

GJK (Gilbert–Johnson–Keerthi) collision detection algorithm

collision detection
geometry
moon add FlyCloudC/GJK@0.1.1
Download zip
Author
Version
0.1.1
License
Apache-2.0
Last updated
9 months ago
Downloads
24
README

#FlyCloudC/GJK

This project implements the GJK (Gilbert–Johnson–Keerthi) collision detection algorithm in MoonBit, supporting intersection detection between circles and polygons, as well as any shape that implements the ConvexShape trait.

#Usage

///|
test {
let s : Polygon = Polygon([Vec2(0, 0), Vec2(0, 2), Vec2(2, 2), Vec2(2, 0)])
let t : Polygon = Polygon([Vec2(0, 0), Vec2(4, 0), Vec2(2, 4)])
let c : Circle = Circle(Vec2(1, 1), 1)
inspect(intersecting(s, c), content="true")
inspect(intersecting(t, c), content="true")
inspect(intersecting(s, t), content="false")
}

#
ConvexShape

pub(open) trait ConvexShape {
far(Self, Vec2) -> Vec2
}

A trait for convex geometric shapes that can compute their support point in a given direction.

The support point is the point on the shape that is furthest in the specified direction. This is a fundamental operation used in collision detection algorithms like GJK (Gilbert-Johnson-Keerthi) and EPA (Expanding Polytope Algorithm).

Methods:

  • far(Self, Vec2) -> Vec2: Returns the support point of the shape in the given direction.

#
Circle

pub(all) struct Circle(Vec2, Double)

#
Polygon

pub(all) type Polygon FixedArray[Vec2]

Note: The FixedArray must not be empty.

#
Polygon::inner

#deprecated("Use `struct T(A)` to declare a newtype and use `.0` access the underlying type instead.")
fn Polygon::inner(self : Polygon) -> FixedArray[Vec2]
Convert newtype to its underlying type, automatically derived.

#
Vec2

pub(all) struct Vec2(Double, Double)

#
intersecting

fn[A : ConvexShape, B : ConvexShape] intersecting(s1 : A, s2 : B, iter_step? : Int) -> Bool

Determines whether two shapes are intersecting using the Gilbert-Johnson-Keerthi (GJK) collision detection algorithm.

Parameters:

  • s1 : The first shape to test for intersection.
  • s2 : The second shape to test for intersection.
  • iter_step : The maximum number of iterations to perform (default: 100). Higher values provide more accuracy but may impact performance.

Returns true if the shapes are intersecting, false otherwise.