GJK accuracy at near contact

Started by
12 comments, last by pondwater 12 years, 9 months ago
So i've implemented what i believe to be a working GJK implementation using Casey's GJK Implementation Video and Code from a post on his forums. It works, it works great, except for when the collidables are very near contact. Ill try to explain the erronious scenario as best i can below:

Im using 'spheres' made up of triangular vertex faces as my colldibles. If two 'spheres' are nearly touching and I rotate one around its origin, the collision 'flickers'. This is due to the 'pointed' parts of the 'spheres' intersecting eachother at certain orientations (seen below for crappy mspaint diagram).

CUBdN.png

Now as the distance between the two 'spheres' decreases ever so slightly, an interection will be detected more often when rotating one of the spheres around as the angles at which they will intersect increases.

Vise versa, as the distance between the 'spheres' increases ever so slightly, the occurance of intersections decreases when rotating. The occurance of intersection would understandably decrease until it only occurs at very particular orientations.

Now my problem/question is as follows:

At a particular distance, there is this 'sweet spot' or 'sweet angle' i should say at which the 'spheres' are visibly not touching, yet an intersection is registered (seen below, same position, different orientation). I should note that his angle is very rare and difficult to manually orient to, but it exists.

RYFlH.png

Now i figure there are two explanations for this:

A) this a result of simple rounding errors or errors inherent to the GJK algorithm.

or

B) this unique to my implementation and therefore something in my implementation is not correct.

The reason im asking this is that i want to begin implementing the EPA agolrithm to obtain the collision normal / penetration depth and im worried that the above case may cause it to explode. My main confusion about whether this is a bug or not relates to the fact that I keep reading about 'detection margins' and 'persistant manifolds' and using 'shallow' vs 'deep' penetation scenarios for GJK and people having difficulties with 'barely touching' scenarios.
Advertisement
My experience with GJK is that 32 floating point precision is not sufficient to get reliable results for quadric shapes. GJK works very well and reliable for convex polyedra though. Note that spheres and capsules can be treated as points and segments and you add the radius later. This trick makes them robust as well. My personal recommendation is to NOT use GJK for cylinders, cones and other quadric shapes.

See GJK Erin Catto's presentation from 2010. It is setup to address a lot of precision problems you are describing here. Keep in mind that this presentation is based on experiences on his work at Blizzard and with Box2D. These are real world experiences with many games and not just some physic demos.

Regarding EPA I totally recommend against even trying it and waste time thinking about it. Besides its complexity to implement it can fail and you need a third algorithm for this case. From my experience GJK with convex polyhedra and SAT work very robust and reliable.

HTH,
-Dirk


http://code.google.com/p/box2d/downloads/detail?name=GDC2010_ErinCatto.zip&can=2&q=

My experience with GJK is that 32 floating point precision is not sufficient to get reliable results for quadric shapes. GJK works very well and reliable for convex polyedra though. Note that spheres and capsules can be treated as points and segments and you add the radius later. This trick makes them robust as well. My personal recommendation is to NOT use GJK for cylinders, cones and other quadric shapes.

See GJK Erin Catto's presentation from 2010. It is setup to address a lot of precision problems you are describing here. Keep in mind that this presentation is based on experiences on his work at Blizzard and with Box2D. These are real world experiences with many games and not just some physic demos.

Regarding EPA I totally recommend against even trying it and waste time thinking about it. Besides its complexity to implement it can fail and you need a third algorithm for this case. From my experience GJK with convex polyhedra and SAT work very robust and reliable.

HTH,
-Dirk


http://code.google.c...to.zip&can=2&q=


hmm well my 'spheres' are technically polyedra since they have triangular faces. Would it be safe to say that there is something wrong with my code that im getting collisions at certain angles when they objects are very close?

For example, here is one of those angles where it detects a collision when there clearly is no intersection.

FLv9F.png

Would this be caused by the rounding error? or is something wrong with my code? There is clearly a plane that seperates the two objects, shouldnt my gjk be returning false?

// start looping
for (int i = 0; i < maxIteration; i++) {

// find new point for the simplex
support(newestPoint, actorA, actorB, direction);

// test newest points position relative to origin
if (m3dDotProduct3(newestPoint, direction) < 0) {
// last point added did not pass the origin, Minkowski Sum cannot possibly contain the origin
return false;
}
// add a new point to the simplex
simplex.add(newestPoint);
// update simplex and direction, return true if simplex encompasses origin
if (updateSimplexAndDirection(simplex, direction)) {
// collision detected
return true;
}

}
return false;


Also, parts in the code where it it uses AB.cross(AO).cross(AB) i compute the vector triple product of AB,AO,AB instead. Could this be causing anything?
From the picture I would say it might be a bug. From the code snippet you posted it looks like you are using Casey's code. I remember that there was a bug with this code and it can report false positives. When I discussed this with Casey he said it wasn't an issue for him since he used it for culling. False positives are clipped away anyway later so in his particular case it isn't a problem. Sorry, but this is years ago. I really don't remember exactly.

If you want to figure this out you need to debug it. And that means you need to render the state of simplex relative to the origin at each iteration. This is what I did. The good thing about collision detection is that you can visualize everything! So make use of this.

Ah, I remember now. I think there was some issue with his argument that you only have to search specific Voronoi regions in practice. In reality you could search these regions first, but you must also check for the others as well. Just assert() that the origin is never in one of the regions - I hit that assert() quite often. I even discussed this with Erin and in the end we both agreed that Casey's optimization is causing too many problems. If I remember the exact problem I let you know here.

So I guess you need to write you own simplex solver. Look at Erin's code in Box2D or Erwin's code in Bullet for good examples.

HTH,
-Dirk

From the picture I would say it might be a bug. From the code snippet you posted it looks like you are using Casey's code. I remember that there was a bug with this code and it can report false positives. When I discussed this with Casey he said it wasn't an issue for him since he used it for culling. False positives are clipped away anyway later so in his particular case it isn't a problem. Sorry, but this is years ago. I really don't remember exactly.

If you want to figure this out you need to debug it. And that means you need to render the state of simplex relative to the origin at each iteration. This is what I did. The good thing about collision detection is that you can visualize everything! So make use of this.

Ah, I remember now. I think there was some issue with his argument that you only have to search specific Voronoi regions in practice. In reality you could search these regions first, but you must also check for the others as well. Just assert() that the origin is never in one of the regions - I hit that assert() quite often. I even discussed this with Erin and in the end we both agreed that Casey's optimization is causing too many problems. If I remember the exact problem I let you know here.

So I guess you need to write you own simplex solver. Look at Erin's code in Box2D or Erwin's code in Bullet for good examples.

HTH,
-Dirk


hmmm alright. I've tried looking at most of the articles/ books suggested by most people, but i used casey's because his explanation was much easier to interpret. So what you're suggesting is to run tests for the areas casey assumed the origin could not be? So for a triangle simplex i should also test for the edge BC? and for a tetrahedron simplex test for the triangle BCD?

EDIT: hmm i guess there are A LOT more regions that need to be tested now looking at this diagram from Ericson's ppt presentations.
PFpKU.png

So i figure these are all the areas that must be accounted for:

2 point simplex: 3 regions; A, B, and AB
3 point simplex: 8 regions; A, B, C, AB, AC, BC, ABC 'above' and 'below
4 point simplex: 14 regions; A, B, C, D, AB, AC, AD, BC, BD, CD, ABC, ABD, ACD, BCD

Am I missing any? Im trying to think of the best way to structure my if statements.... any advice is greatly appretiated. Im looking at the box2d code currently, bullets impossible for me to make any sense of..
Well, Casey's presentation is very nice, but it by no means a reference implementation! As I already said in another post here I recommend looking at Erin Catto's presentation. It is *trivial* to write an efficient simplex solver for the tetrahedron case from his work. Note that even Bullet uses a fallback implementation onto the triangle case as suggested in Christer Ericson's book. A solid GJK implementation is not an easy problem :). So don't worry if it takes some time.

Well, Casey's presentation is very nice, but it by no means a reference implementation! As I already said in another post here I recommend looking at Erin Catto's presentation. It is *trivial* to write an efficient simplex solver for the tetrahedron case from his work. Note that even Bullet uses a fallback implementation onto the triangle case as suggested in Christer Ericson's book. A solid GJK implementation is not an easy problem :). So don't worry if it takes some time.


Dirk you are a shining beacon of light right now, thank you so much for all the help. Im gonna plug away through his presentation and the box2d code. I may run into some issues with the tetrahedron case which ill psot here. Wish me luck!
You will do it! If you have questions post here and if I miss your post for some reason just feel free to send me a PM.

Good Luck :)

You will do it! If you have questions post here and if I miss your post for some reason just feel free to send me a PM.

Good Luck :)


hmm im having some confusion in trying to visualize my simplex and the voronoi regions in 3D. Is it safe to assume the 6 regions (8 when taking into account being above or below the triangle) are mutually exclusive? The origin can only exist in a single region?



if ( ((BCxBA)xBA) · BX >= 0 ) { //<--- should that be >= or >? i've seen both used, also, if i dotted it by AX instead of BX, would that make a difference?
if (AX · AB >= 0) {
if (BX ·BA >= 0) {

// the origin exists only in Eab
// remove point c from simplex
// what should search direction be?
}
}


iprES.png
Alright im working along, basically ive structured my simplex solver to test from simplest simplex component to to more complicated component to see which region the origin is enclosed in.

line simplex:

check if closest to A
remove point B
return

check if closest to B
remove point A
return

assume its closest to AB edge

triangle simplex:

check if closest to A
remove point B
remove point C
return

check if closest to B
remove point A
remove point C
return

check if closest to C
remove point A
remove point B
return

check if closest to AB edge
remove point C
return

check if closest to AC edge
remove point B
return

check if closest to BC edge
remove point A

assume its above or below triangle
if above triangle, continue
if below triangle, rewind triangle so it faces down

tetrahedron simplex

check if closest to A
remove point B
remove point C
remove point D
return

check if closest to B
remove point A
remove point C
remove point D
return

check if closest to C
remove point A
remove point B
remove point D
return

check if closest to D
remove point A
remove point B
remove point C
return



Stuck here, two questions:

A) for a triangle case, if lets say the origin is contained by the edge BC, what should the new search direction be? BCxBXxBC? BCxCXxBC? either?

B) for tetrahedron edge cases, to detect whether the origin is between the two adjacent face sides im using the following logic:

let ABC be the normal for the triangular face of points ABC
let ACD be the normal for the triangular face of points ACD


if (dot(ABC, AO) >= 0 ) {
if (dot(ACD, AO) >= 0) {
// the origin is within an acute angle of both faces, and therefore is somewhere between them along the edge AC
// remove B and D
// what should i set the direction to?
}
}


this code will be after checking if the origin is closest to a single vertex, so i shouldnt have to check for that.

Also, is there anyway to ensure that the normals calculated for the triangles are always facing out? is there a particular ordering of the letters that should be used?

This topic is closed to new replies.

Advertisement