Computing and Software Department, McMaster University
Introduction to GJK
John McCutchan
November 9, 2006
Mccutchan: Introduction to GJK(slide 1), 1
Introduction
◮ Introduction to GJK
◮ Terminology
◮ Algorithm in detail
◮ Example
◮ Minkowski Difference (GJK with two convex objects)
◮ Polyhedra Support Function
◮ Sphere Support Function
◮ Cylinder Support Function
◮ Transformation Support Function
◮ Computing P of minimum norm in CH(Q) and reducing Q
◮ References
Mccutchan: Introduction to GJK(slide 2), 2
Introduction to GJK
Given two convex shapes
◮ Computes distance d
◮ Can also compute closest pair of points PA and PB
Mccutchan: Introduction to GJK(slide 3), 3
Terminology – Support Point
Supporting (extreme) point P for direction d returned by support
mapping function Support(d)
Mccutchan: Introduction to GJK(slide 4), 4
Terminology – Simplex
Simplex
Mccutchan: Introduction to GJK(slide 5), 5
Terminology – Convex Hull
Mccutchan: Introduction to GJK(slide 6), 6
Algorithm in detail
◮ 1. Initialize simplex set Q with up to d+1 points from C (in d
dimensions)
◮ 2. Compute point P of minimum norm in CH(Q)
◮ 3. If P is the origin, exit; return 0.0;
◮ 4. Reduce Q to the smallest subset Q’ of Q, such that P in
CH(Q’)
◮ 5. Let V = Support(-P)
◮ 6. If V no more extreme in direction -P than P itself, exit;
return length(P)
◮ 7. Add V to Q. Go to step 2
Mccutchan: Introduction to GJK(slide 7), 7
Example 1/10
Input: Convex shape C
Mccutchan: Introduction to GJK(slide 8), 8
Example 2/10
1. Initialize simplex set Q with up to d+1 points from C (in d
dimensions)
Q = [Q1, Q0]
Mccutchan: Introduction to GJK(slide 9), 9
Example 3/10
2. Compute point P of minimum norm in CH(Q)
Mccutchan: Introduction to GJK(slide 10), 10
Example 4/10
3. If P is the origin, exit; return 0.0;
4. Reduce Q to the smallest subset Q’ of Q, such that P in CH(Q’)
Q = [Q1, Q0]
Mccutchan: Introduction to GJK(slide 11), 11
Example 5/10
5. Let V = Support(-P)
Mccutchan: Introduction to GJK(slide 12), 12
Example 6/10
6. If V no more extreme in direction -P than P itself, exit; return
length(P)
7. Add V to Q, go to step 2
Q = [Q1, Q0, V ]
Mccutchan: Introduction to GJK(slide 13), 13
Example 7/10
2. Compute point P of minimum norm in CH(Q)
Q = [Q1, Q0, V ]
Mccutchan: Introduction to GJK(slide 14), 14
Example 8/10
3. If P is the origin, exit; return 0.0;
4. Reduce Q to the smallest subset Q’ of Q, such that P in CH(Q’)
Q = [Q3]
Mccutchan: Introduction to GJK(slide 15), 15
Example 9/10
5. Let V = Support(-P)
Mccutchan: Introduction to GJK(slide 16), 16
Example 10/10
6. If V no more extreme in direction -P than P itself, exit; return
length(P)
Mccutchan: Introduction to GJK(slide 17), 17
Minkowski Difference (GJK with two convex objects)
◮ Problem: How do we handle two convex objects, A and B?
◮ Solution: Use Minkowski Difference of A and B.
Mccutchan: Introduction to GJK(slide 18), 18
Minkowski Sum
Mccutchan: Introduction to GJK(slide 19), 19
Minkowski Sum
MinkowskiSum(A, B) = a + b : a ∈ A, b ∈ B
MinkowskiDifference(A, B) = MinkowskiSum(A, −B)
MinkowskiDifference(A, B) = a − b : a ∈ A, b ∈ B
Mccutchan: Introduction to GJK(slide 20), 20
Minkowski Difference
◮ What happens to points in A and B that are overlapping when
you take the minkowski difference?
◮ They are mapped to the origin.
Mccutchan: Introduction to GJK(slide 21), 21
Minkowski Difference
◮ So, A and B are intersecting iff MinkowskiDifference(A,B)
contains the origin!
◮ The algorithm can stay the same, we just need to change the
support point function to compute the Minkowski Difference
◮ MinkowskiDiffSupport(A,B,d) = A.Support(d) -
B.Support(-d)
Mccutchan: Introduction to GJK(slide 22), 22
Support Point Functions – Polyhedra
Given: C – A convex hull of points
Support(d) = max(d · p : p ∈ C )
Mccutchan: Introduction to GJK(slide 23), 23
Support Point Functions – Sphere
Given: Sphere centered at c with radius r
d
Support(d) = c + r
||d||
Mccutchan: Introduction to GJK(slide 24), 24
Support Point Functions – Cylinder
Given: Cylinder centered at c and whose central axis is spanned by
the unit vector u. Let the radius of the cylinder be r and the half
height be n. As well, Let w = d − (u · d)u be the component of d
orthogonal to u.
If w 6= 0:
w
Support(d) = c + sign(u · d)nu + r
||w ||
else:
Support(d) = c + sign(u · d)nu
Mccutchan: Introduction to GJK(slide 25), 25
Support Point Functions – Transformation
Given: T (x) = Bx + c Where B is the rotation matrix’s basis and
c is the translation. SupportC is the support function of the
untransformed convex object.
Support(SupportC , d) = T (SupportC (B T d))
Mccutchan: Introduction to GJK(slide 26), 26
Computing P of minimum norm in CH(Q’) and reducing Q
to Q’
◮ Overview of affine hulls and convex hulls
◮ Equivelance of affine and convex hulls
◮ Finding closest point on affine hull to origin
◮ Finding smallest Q’ where P is in CH(Q’)
Mccutchan: Introduction to GJK(slide 27), 27
Affine and Convex Hulls
◮ Affine Hull: AH(S) = λ1 x1 + λ2 x2 + ... + λk xk |xi ∈ S, λi ∈ R
◮ i = 1, ..., k, λ1 + λ2 + ...λk = 1, k = 1, 2, ...
◮ Convex Hull: CH(S) = λ1 x1 + λ2 x2 + ... + λk xk |xi ∈ S, λi ∈ R
◮ i = 1, ..., k, λ1 + λ2 + ...λk = 1, k = 1, 2, ..., λi >= 0
The point P closest to the origin is defined
Pn as a convex
combination
Pn of the points in Q. P = i =1 λi xi where
i =1 λi = 1.0 and λi ≥ 0 Since we are looking for the smallest Q’
that contains P we can add another restriction: λ > 0 Now we are
looking for Q ′ = xi : λi > 0
Mccutchan: Introduction to GJK(slide 28), 28
Equivelance of affine and convex hulls
If we can find a set Q ′ = xi : i ∈ Y for which i ∈ Y , λi > 0.0 in
X X
AH(Q ′ ) = λi xi , λi = 1.0
i ∈Y i ∈Y
and for all j ∈
/ Y , λj <= 0.0 in
X X
AH(Q ′ ∪ Xj ) = λi xi , λi = 1.0
i ∈Y ∪j i ∈Y ∪j
For such a set Q ′ we have P(AH(Q ′ )) = P(CH(Q ′ ))
Mccutchan: Introduction to GJK(slide 29), 29
Finding Q’
◮ You find Q’ by iterating over all subsets of Q and checking if
they fit the two previous conditions.
◮ Need to compute all of the λi terms for all subsets
Mccutchan: Introduction to GJK(slide 30), 30
Closest point to origin on affine hull of triangle (2-simplex)
◮ Affine hull of a triangle is plane containing the triangle vertices
◮ We need to find a point P = λ1 x1 + λ2 x2 + λ3 x3
◮ P will be closest to the origin if the vector from the origin to
P is perpendicular to the plane
◮ In other words when P is perpendicular to the triangles edges
◮ Two arbitrary edges are x1 x2 and x1 x3 we want x1 x2 · P = 0
and x1 x3 · P = 0
◮ If we substitute λ1 x1 + λ2 x2 + λ3 x3 for P with both edges we
get:
Mccutchan: Introduction to GJK(slide 31), 31
Closest point to origin on affine hull of triangle (2-simplex)
1 1 1
A = (x2 − x1 ) · x1 + (x2 − x1 ) · x2 + (x2 − x1 ) · x3
(x3 − x1 ) · x1 + (x3 − x1 ) · x2 + (x3 − x1 ) · x3
b = [1, 0, 0]
x = [λ1 , λ2 , λ3 ]
Mccutchan: Introduction to GJK(slide 32), 32
Johnson’s Distance sub algorithm
In general:
1 ... 1
(x2 − x1 ) · x1 ... (x2 − x1 ) · xm
A=
... ...
(xm − x1 ) · x1 ... (xm − x1 ) · xm
b = [1, 0, ..., 0]
x = [λ1 , ..., λm ]
Mccutchan: Introduction to GJK(slide 33), 33
Johnson’s Distance sub algorithm
◮ 1. Need to solve Ax = b for every subset of Q
◮ 2. Search for smallest Q ′ which satisfies above two conditions
3. Q = Q ′ and P = i ∈Q ′ λi xi
P
◮
Mccutchan: Introduction to GJK(slide 34), 34
Johnson’s Distance sub algorithm
◮ How can all of the Ax = b systems be solved efficiently?
◮ Gino: Cramers Rule
◮ McCutchan: Use Maple/Matlab to precompute generic
λ = A−1 b for simplex size of 2,3,4
◮ Gino’s is faster but McCutchan’s is simple and obvious.
◮ GJK is already so fast and O(1) that speed difference not
noticeable on modern machines
Mccutchan: Introduction to GJK(slide 35), 35
Alternative way to find Q’
◮ Look at the problem geometrically
◮ Use voronoi region checks to find which part of simplex the
origin is in.
◮ Solve single set of equations once proper sub simplex has been
found.
◮ Pros: Most efficient and Intuitive way of working with GJK.
◮ Cons: May be floating point issues in using two different
mathematical formulations. One for determining the sub
simplex and the other for solving for the lambda values.
Mccutchan: Introduction to GJK(slide 36), 36
References
◮ Christer Ericson’s Real-time Collision Detection
◮ Christer Ericson’s Sigraph slides on GJK
◮ Gino Van Den Bergen’s Collision Detection in interactive 3D
environments
Mccutchan: Introduction to GJK(slide 37), 37