Archived

This topic is now archived and is closed to further replies.

langguy

3D Collision Detection

Recommended Posts

There seem to be a lot of different ways to perform collision detection in 3D. What is the most efficient way to do this (ie looks good but doesn''t compensate from performace, and doesnt require a ton of code)? Code example(s) in C#/DX9 would be helpful but not mandatory.

Share this post


Link to post
Share on other sites
How I''m doing mine is like this:

The world will be in quadtree structures;

The collision detection will only test the current quad (and maybe the adjacent ones if it overlaps into 2 or more).

This eliminates things that are not in the immediate vicinity.

Maybe then an axis aligned bounding box, sphere or ellipsoid - this will ensure that the object collides with the local vicinity. Sphere is faster, but less ''tight'' than an AA bounding box.

From here, it is up to you; you can go further and test against the faces if your game needs it.

The aim is to cull any obvious non-collisions at first, do a quick test to see if anything is roughly locally colliding and then test it in more detail.

Share this post


Link to post
Share on other sites
Hmmmm....Okay, I''ve got the part with bounding spheres working okay. I think I read somewhere about testing to see if a ray intersects with a mesh. What''t the concept behind that?

Share this post


Link to post
Share on other sites
That would be you doing planar maths and calculating whether or not two surfaces overlap (as I said before).

You''d need the vectors that make up the face and the normal. I haven''t had much experience of this yet so I can''t advise you unfortunately.

Hope it helps anyway

Share this post


Link to post
Share on other sites
Here''s what I have been trying to do: I use bounding spheres to perform a simple collision detection. If that passes, I cast a ray from the center of the first bounding sphere pointing towards the center of the second bounding sphere. I get the end of the ray using proportions (the proportion is the the radius of the second sphere over the total distance between the centers of both spheres) If that ray intersects with the second mesh, I do the same test, but vice-versa(eg ray points from second bounding sphere to first bounding sphere). If both of those pass, a collision is detected. It works on paper, but it doesn''t seem to work in my code:

public static bool SimpleColDet(MeshObject moFirst, MeshObject moSecond, Direct3DHandler d3d)
{
double dDistance = d3d.Distance(new Vector3(moFirst.X(),
moFirst.Y(), moFirst.Z()),
new Vector3(moSecond.X(),
moSecond.Y(), moSecond.Z()));
double dTotalRads = (double)moFirst.fRadius + (double)moSecond.fRadius;

if(dDistance < dTotalRads)
{// <--- It works up to this point.

float fXDiff = moSecond.X() - moFirst.X();
float fYDiff = moSecond.Y() - moFirst.Y();
float fZDiff = moSecond.Z() - moFirst.Z();
float fProportion = (float)dDistance / moSecond.fRadius;
//do First-to-Second ray test

Vector3 vRaySource = new Vector3(moFirst.X(), moFirst.Y(), moFirst.Z());
Vector3 vRayDest = new Vector3(fXDiff * fProportion + moFirst.X(), fYDiff * fProportion + moFirst.Y(), fZDiff * fProportion + moFirst.Z());

if(moSecond.sm.Mesh.Intersect(vRaySource, vRayDest))
{//if it passes, do Second-to-First ray test

fProportion = moFirst.fRadius / (float)dDistance;
vRaySource = new Vector3(moSecond.X(), moSecond.Y(), moSecond.Z());
vRayDest = new Vector3(fXDiff * fProportion, fYDiff * fProportion, fZDiff * fProportion);

if(moFirst.sm.Mesh.Intersect(vRaySource, vRayDest))
return true;
else return false;
}
else return false;
}
else return false;
}

MeshObjects are just a skinned mesh with a matrix attatched (the X(), Y(), and Z() functions access specific parts of the matrix) and the radius of its bounding sphere; and d3d is just a collection of functions I use to simplify development. Like I said, this works on paper but not in my code. Can anyone look this over and see if there''s a subtle mistake in my math or if there''s just a better way to do it?

Share this post


Link to post
Share on other sites