AABB Ray Collision

Started by
6 comments, last by Randy187 20 years, 4 months ago
Is there a simple way to check if there is a intersection between a ray and a AABB? I don''t need the coordinate where it hit it, but only true or false. I can''t find anything i can convert to C++ or it calculates the hit coordinate and other stuff.
Advertisement
Hmm, I don''t know how much you know, but what I''ve done is I convert the AABB into a brush and then I use my ray to brush collision detection routine, but if you don''t know how to do any of that already then my advice is useless.
Why don't alcoholics make good calculus teachers?Because they don't know their limits!Oh come on, Newton wasn't THAT smart...
I''m making a kind of star wars like game with lasers (Ray''s) and using AABB''s for the objects first tests (before doing accurate tests). I think it will slow down to much using this method. Isn''t there a method which uses a AABB direct?
i doubt it''d be too slow, brush collision detection is probably the fastest method there is because it involves no square roots.
Why don't alcoholics make good calculus teachers?Because they don't know their limits!Oh come on, Newton wasn't THAT smart...
Sorry for posting the code like this , but i don''t know how
to write thos source tags
anyway the code is this , ist a ray intersection for an aab box
place the functions inline if you wish.
byez



/////////////////////////////////////////////////////////////////
// Axis Aligned Bounding Box functions ( AABB )


bool PlaneXIntr( float x1 ,float y1 ,float z1,
float x2 ,float y2 ,float z2,
float px0,
float py1,float py2,float pz1,float pz2)
{
float t,l,m,n;
l=x2-x1;
m=y2-y1;
n=z2-z1;
if ( l==0.0f) return false;
t=(px0-x1)/l;
if ( t>=0.0f && t<=1.0f)
{
float py,pz;
py=y1+t*m;
pz=z1+t*n;
if ( py>=py1 && py<=py2)
if ( pz>=pz1 && pz<=pz2)
{
// px=x1+t*l;
return true;
}
}
return false;
}

bool PlaneYIntr( float x1 ,float y1 ,float z1,
float x2 ,float y2 ,float z2,
float py0,
float px1,float px2,float pz1,float pz2)
{
float t,l,m,n;
l=x2-x1;
m=y2-y1;
n=z2-z1;
if ( m==0.0f) return false;
t=(py0-y1)/m;
if ( t>=0.0f && t<=1.0f)
{
float px,pz;
px=x1+t*l;
pz=z1+t*n;
if ( px>=px1 && px<=px2)
if ( pz>=pz1 && pz<=pz2)
{
// py=y1+t*m;
return true;
}
}
return false;
}


bool PlaneZIntr( float x1 ,float y1 ,float z1,
float x2 ,float y2 ,float z2,
float pz0,
float px1,float px2,float py1,float py2)
{
float t,l,m,n;
l=x2-x1;
m=y2-y1;
n=z2-z1;
if ( n==0.0f) return false;
t=(pz0-z1)/n;
if ( t>=0.0f && t<=1.0f)
{
float px,py;
px=x1+t*l;
py=y1+t*m;
if ( px>=px1 && px<=px2)
if ( py>=py1 && py<=py2)
{
// pz=z1+t*n;
return true;
}
}
return false;
}

bool RayBoxTest( float x1 ,float y1 ,float z1,
float x2 ,float y2 ,float z2,
float ax ,float ay ,float az,
float bx ,float by ,float bz)
{
if ( PlaneXIntr(x1,y1,z1,x2,y2,z2,ax,ay,by,az,bz)==true ) return true;
if ( PlaneXIntr(x1,y1,z1,x2,y2,z2,bx,ay,by,az,bz)==true ) return true;
if ( PlaneYIntr(x1,y1,z1,x2,y2,z2,ay,ax,bx,az,bz)==true ) return true;
if ( PlaneYIntr(x1,y1,z1,x2,y2,z2,by,ax,bx,az,bz)==true ) return true;
if ( PlaneZIntr(x1,y1,z1,x2,y2,z2,az,ax,bx,ay,by)==true ) return true;
if ( PlaneZIntr(x1,y1,z1,x2,y2,z2,bz,ax,bx,ay,by)==true ) return true;
return false;
}
tag is [/ s o u r c e], without the spaces<br><br>as for an algorithm, <br><br><a href = "http://www.gamedev.net/community/forums/topic.asp?topic_id=191429"> Check if a Rectangle and a Line collide</a><br><br>works in 2D or 3D the same

Everything is better with Metal.

If you are interested in very hi speed code I can give more details but the main ideas in coldet and more specifically AABoxes :

- Do the quickest rejection tests first.

-- If you ray is bounded you can use a AABox/AABox test first. It''s implicit but unclearly exploited in the code samples I have quickly read here.

-- then check the infinite line. The trick is the <b>separating plane and nearest vertex in the AABox</b> (Google). The nearest face/edge or vertex necessarilly contains the nearest vertex. Use the three orthographic 2D projections (1). That is 2D dot products + 3 additions. Use the sign bits your ray vector components to do that.

(1) if there is an intersection in 3D then there is an intersection on every 2D projection.

-- last if the infinite line intersects. You have to check for a potential intersection with the ray segment. Avoid unecessary divisions before compares or actual intersection computation if required. Also remark that if Ray.xmin< AAbox.xmin and Ray.xmax>AAbox.xmax then since the line intersects, your ray intersects.

Then reorder the algo so that the code is optimally efficient and avoid redundant tests. I think I have it somewhere in my code base (not on this machine) ... so if you wanna know more, let me know. If you don''t care about speed then don''t care ...

If you are interested in very hi speed code I can give more details but the main ideas in coldet and more specifically AABoxes :

- Do the quickest rejection tests first.

-- If you ray is bounded you can use a AABox/AABox test first. It''s implicit but unclearly exploited in the code samples I have quickly read here.

-- then check the infinite line. The trick is the separating plane and nearest vertex in the AABox (Google). The nearest face/edge or vertex necessarilly contains the nearest vertex. Use the three orthographic 2D projections (1). That is 2D dot products + 3 additions. Use the sign bits your ray vector components to do that.

(1) if there is an intersection in 3D then there is an intersection on every 2D projection.

-- last if the infinite line intersects. You have to check for a potential intersection with the ray segment. Avoid unecessary divisions before compares or actual intersection computation if required. Also remark that if Ray.xmin< AAbox.xmin and Ray.xmax>AAbox.xmax then since the line intersects, your ray intersects.

Then reorder the algo so that the code is optimally efficient and avoid redundant tests. I think I have it somewhere in my code base (not on this machine) ... so if you wanna know more, let me know. If you don''t care about speed then don''t care ...

"Coding math tricks in asm is more fun than Java"

This topic is closed to new replies.

Advertisement