bonk-docs/content/narrow_phase/GJK2D.md

21 lines
1.1 KiB
Markdown
Raw Permalink Normal View History

2019-09-15 06:39:26 +00:00
---
title: "GJK2D"
date: 2019-09-14T22:47:55-07:00
weight: 10
---
2020-07-18 20:13:59 +00:00
Bonk uses the Gilbert-Johnson-Keerthi, or GJK, algorithm to perform narrow phase collision detection.
2019-09-15 06:39:26 +00:00
2020-07-18 20:13:59 +00:00
To accurately check a collision, you must pass two sets of shapes and transforms to the **NarrowPhase.TestCollision** method. Remember that a transform operates on the vertices of a shape: it moves, rotates, and scales them in 2D space.
2019-09-15 06:39:26 +00:00
```cs
var circleA = new Circle(2);
var transformA = new Transform2D(new Vector2(-1, -1));
var circleB = new Circle(2);
var transformB = new Transform2D(new Vector2(1, 1));
2020-07-18 20:13:59 +00:00
var result = NarrowPhase.TestCollision(circleA, transformA, circleB, transformB);
2019-09-15 06:39:26 +00:00
```
2020-07-18 20:13:59 +00:00
The GJK algorithm can also return a *termination simplex*. You can think of the termination simplex as a key that helps you find the penetration vector with **NarrowPhase.Intersect**. We have a related method, **NarrowPhase.FindCollisionSimplex** which returns two variables. The first is whether an overlap has occurred. The second is the termination simplex. Note that if there was no collision, the value of the simplex is irrelevant.