Jump to content
  • Advertisement
Sign in to follow this  
of_ownage

TriangleIntersectAABB

This topic is 3727 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, anybody knows a functions to check if that is true. (maybe also a function to check it its fully inside ;) ) Triangle defined by v1,v2,v3 which are x/y/z floats. AABB definded by MinPt, MaxPt which are x/y/z floats. thank you

Share this post


Link to post
Share on other sites
Advertisement
1. test if bounding box of a triangle and your box overlap. if they don't return false
2. test if plane of your triangle intersects box. if it doesn't return false
3. perform 3 2d tests
for each coordinate system plane
test if projection of box (rectangle) overlaps projection of a triangle
if at least one such test reports non overlap - return false
return true
this method is actually test using minkowski sum, just simplified.

Share this post


Link to post
Share on other sites
I'd recommend you use the SAT.



void getTriangleInterval(const Vector& axis, const Vector& v1, const Vector& v2, const Vector& v3, float& min, float& max)
{
float d1 = v1.dotProduct(axis);
float d2 = v2.dotProduct(axis);
float d3 = v3.dotProduct(axis);

min = max = d1;
if(d2 < min) min = d2; else if (d2 > max) max = d2;
if(d3 < min) min = d3; else if (d3 > max) max = d3;
}

void getAABBInterval(const Vector& axis, const Vector& aabbmin, const Vector& aabbmax, float& min, float& max)
{
Vector centre = (aabbmax + aabbmin) * 0.5f;
Vector halfsize = (aabbmax - aabbmin) * 0.5f;
float p = centre.dotProduct(axis);
float rx = fabs(axis.x) * halfsize.x;
float ry = fabs(axis.y) * halfsize.y;
float rz = fabs(axis.z) * halfsize.z;
float r = rx + ry + rz;

min = p - r;
max = p + r;
}

bool triangleAABBSeparated(const Vector& axis, const Vector& v1, const Vector& v2, const Vector& v3, const Vector& min, const Vector& max)
{
// degenerate case
float axisLengthSquared = axis.dotProduct(axis);
if(axisLengthSquared < 1.0E-8f)
return false;

// the box interval
float aabbmin, aabbmax;
getAABBInterval(axis, min, max, aabbmin, aabbmax);

// the triangle interval
float trimin, trimax;
getTriangleInterval(axis, v1, v2, v3, trimin, trimax);

// check for interval overlap
if(trimin > aabmax || aabbmin > trimax)
return true;
else
return false;
}

bool triangleAABBIntersect(const Vector& v1, const Vector& v2, const Vector& v3, const Vector& min, const Vector& max)
{
// the separation axis to test
Vector axis;

// triangle plane
axis = (v2 - v1).crossProduct(v3 - v2);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

// box axes
axis = Vector(1, 0, 0);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

axis = Vector(0, 1, 0);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

axis = Vector(0, 0, 1);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

// first cross axes
axis = Vector(1, 0, 0).crossProduct(v2 - v1);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

axis = Vector(1, 0, 0).crossProduct(v3 - v2);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

axis = Vector(1, 0, 0).crossProduct(v1 - v3);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

// second cross axes
axis = Vector(0, 1, 0).crossProduct(v2 - v1);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

axis = Vector(0, 1, 0).crossProduct(v3 - v2);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

axis = Vector(0, 1, 0).crossProduct(v1 - v3);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

// third cross axes
axis = Vector(0, 0, 1).crossProduct(v2 - v1);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

axis = Vector(0, 0, 1).crossProduct(v3 - v2);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

axis = Vector(0, 0, 1).crossProduct(v1 - v3);
if(triangleAABBSeparated(axis, v1, v2, v3, min, max)) return false;

return true;
}

Share this post


Link to post
Share on other sites
for containment, after the intersection is positive, check if the three corners are inside the box.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!