Followers 0

# Need code for AABB vs triangle collision with least separation distance

## 5 posts in this topic

I am looking for some 3D AABB vs. triangle collision code and it has been really hard to find.

More specifically, I need not only a boolean test of whether a box and a triangle overlap. I also need to find the least distance I need to move the box away so that it does not overlap anymore, to solve collisions.

I looked at this resource, http://www.geometrictools.com/LibMathematics/Intersection/Intersection.html but it does not seem to give the distance, I also looked at this code http://fileadmin.cs.lth.se/cs/Personal/Tomas_Akenine-Moller/code/tribox3.txt and it too does not seem to give the distance. I even translated it to Javascript actually, http://pastebin.com/FC7AYCx8. Googling, I found some old threads, including this one, http://www.gamedev.net/topic/386242-triangle---aabb-intersection/ but that too does not care about the distance.

So, if someone could link me to some code of a function that takes a triangle and a box and gives the least translation vector to separate them, I would appreciate very much.

0

##### Share on other sites

Thanks for your help, but I already know that the problem of OBB vs. triangle is the same as AABB vs triangle... and I already have a boolean function, as the one you posted. My problem is specifically with getting this distance instead of a boolean.

If you could help me modify the function you coded to return distance, or help me understand what it's doing I'd appreciate very much... more specifically, what this part of the code is doing:

p0 = v0.z*v1.y – v0.y*v1.z;
p2 = v2.z*(v1.y – v0.y) – v2.y*(v1.z – v0.z);
r = e1 * Abs(f0.z) + e2 * Abs(f0.y);
if (Max(-Max(p0, p2), Min(p0, p2)) > r) return 0;

Edit: Oh, I looked at that book you mentioned and I figured out what is going on in the code, and now I will be able to find the best translation vector! Thank you.

Edited by drnick@ludumdare
0

##### Share on other sites

You can look at my SAT presentation here: https://box2d.googlecode.com/files/DGregorius_GDC2013.zip

Essentially you need to keep track of the axis of minimum penetration instead of simply existing if you found a separating axis. Note that for disjoint objects the separating axis does not realize the actual distance. For the distance between two disjoint convex polyhedra I would use GJK.

0

##### Share on other sites

https://github.com/lucasdealmeidasm/TriangleBox.js
What I achieved, now that I have succeed.

The main difference between this solution and the one in the book is that this one normalizes the axes before calculating distances to give you the least translation vector, this makes this solution much more useful for game development.

Note you can skip this and compare the squared distances and then just normalize the final minimum separating axis vector instead to save a bunch of normalizings (sqrts).
0

## Create an account

Register a new account