**2**

# GJK accuracy at near contact

###
#1
Members - Reputation: **191**

Posted 10 July 2011 - 03:00 PM

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).

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.

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.

###
#2
Members - Reputation: **2011**

Posted 11 July 2011 - 09:19 AM

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=

###
#3
Members - Reputation: **191**

Posted 11 July 2011 - 01:09 PM

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.

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?

###
#4
Members - Reputation: **2011**

Posted 11 July 2011 - 01:44 PM

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

###
#5
Members - Reputation: **191**

Posted 11 July 2011 - 05:08 PM

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.

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..

###
#6
Members - Reputation: **2011**

Posted 11 July 2011 - 06:13 PM

###
#7
Members - Reputation: **191**

Posted 11 July 2011 - 06:49 PM

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!

###
#9
Members - Reputation: **191**

Posted 11 July 2011 - 08:28 PM

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? } }

###
#10
Members - Reputation: **191**

Posted 11 July 2011 - 10:43 PM

[b]line simplex:[/b] check if closest to A remove point B return check if closest to B remove point A return assume its closest to AB edge [b]triangle simplex:[/b] 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 [b]tetrahedron simplex[/b] 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?

###
#11
Members - Reputation: **2011**

Posted 12 July 2011 - 09:12 AM

Regarding the search direction it is all in the code. Use the right-hand-rule to figure it out.

http://en.wikipedia.org/wiki/Right-hand_rule

###
#12
Members - Reputation: **191**

Posted 12 July 2011 - 12:36 PM

This looks all good. For the tetrahedron case the origin can be closest to a vertex, edge, face or be inside the tetrahedron. I would use Erin's approach where you compute the barycentric coordinates and use these to check the Voronoi regions. This is very structured and efficient.

Regarding the search direction it is all in the code. Use the right-hand-rule to figure it out.

http://en.wikipedia....Right-hand_rule

Alright so i've worked through it, and i have 2 more questions:

1) the normals for the faces of the triangles, im using the right hand rule, the problem is, how can i guarentee my normals face out when the arrangement of the simplex points may vary. For example in the figure below, those are respective cross products under each triangle to get outward facing normals. The ordering of the cross product varies depending on the arrangement of the simplex points. Im not sure how to proceed about guaranteeing that the normals i calculate point outwards.

2) What is a good way to test if the origin is closest to a triangular face, to test for. For example, lets say my simplex is the 'left' triangle above and the face pointing in the direction of ABxAC is being tested, my current code is:

if (m3dDotProduct3(ABxAC, AO) > 0) { if (m3dDotProduct3(ACxAD, AO) <= 0) { if (m3dDotProduct3(ADxAB, AO) <= 0) { if (m3dDotProduct3(DCxDB, DO) <= 0) { // origin is closest to ABC face } } } }

EDIT: okay so i put breakpoints in my different simplex solver cases. Every 'closest to a point' scenario except A for a line simplex has not been called once. I also put code to ensure my normals face outside the tetrahedron, however according to my break points the normals ALWAYS face outwards if i use the 'left' triangle from the image cross products.

My algorithm is having trouble with getting stuck in infinite loops... urghh

###
#13
Members - Reputation: **2011**

Posted 13 July 2011 - 09:02 AM

###
#14
Members - Reputation: **191**

Posted 13 July 2011 - 04:37 PM

You can use the triple product and compute the volume when you are creating a tetrahedron simplex. If you get a negative result you can revert the winding of the triangle. Another way to verify this is to use the other vertex on a triangle. E.g. you looking at triangle ACD. Then you know that B must be behind this triangle. Same is true for all other combinations.

yeah turns out my issue isn't with my normals it's my tetrahedron edge and face tests they arent testing for the proper areas