• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
pondwater

GJK accuracy at near contact

13 posts in this topic

So i've implemented what i believe to be a working GJK implementation using [url="http://mollyrocket.com/9441"]Casey's GJK Implementation Video[/url] and [url="https://mollyrocket.com/forums/viewtopic.php?t=797"]Code from a post on his forums[/url]. 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).

[img]http://i.imgur.com/CUBdN.png[/img]

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.

[img]http://i.imgur.com/RYFlH.png[/img]

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

Share this post


Link to post
Share on other sites
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=
1

Share this post


Link to post
Share on other sites
[quote name='Dirk Gregorius' timestamp='1310397579' post='4833760']
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


[url="http://code.google.com/p/box2d/downloads/detail?name=GDC2010_ErinCatto.zip&can=2&q="]http://code.google.c...to.zip&can=2&q=[/url]
[/quote]

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.

[img]http://i.imgur.com/FLv9F.png[/img]

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?

[code]// 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;[/code]

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

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
[quote name='Dirk Gregorius' timestamp='1310413454' post='4833939']
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
[/quote]

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.
[img]http://i.imgur.com/PFpKU.png[/img]

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

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
[quote name='Dirk Gregorius' timestamp='1310429639' post='4834063']
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.
[/quote]

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!
0

Share this post


Link to post
Share on other sites
[quote name='Dirk Gregorius' timestamp='1310435790' post='4834088']
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 :)
[/quote]

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?

[code]

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?
}
}
[/code]

[img]http://i.imgur.com/iprES.png[/img]
0

Share this post


Link to post
Share on other sites
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.

[code][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

[/code]

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

[code]
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?
}
}[/code]

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

Share this post


Link to post
Share on other sites
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.org/wiki/Right-hand_rule
1

Share this post


Link to post
Share on other sites
[quote name='Dirk Gregorius' timestamp='1310483548' post='4834335']
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.
[url="http://en.wikipedia.org/wiki/Right-hand_rule"]http://en.wikipedia....Right-hand_rule[/url]
[/quote]

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.
[img]http://i.imgur.com/F2FP2.png[/img]

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:

[code]

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

[/code]


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
0

Share this post


Link to post
Share on other sites
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.
2

Share this post


Link to post
Share on other sites
[quote name='Dirk Gregorius' timestamp='1310569321' post='4834850']
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.
[/quote]

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0