# Collision Detection Works/Doesn't(Suggestions?)

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

## Recommended Posts

I'm having a bit of a problem with my collision detection. It is for a birds-eye spaceship game. I've been testing my CD that uses Mesh.Intersect, and it works -- but only if the target object is sitting at origin and it still acts sort of goofy. I can move my spaceship (soOne in code below) and get what seems like proper detection in the mesh areas of soTwo (a dummy spaceship sitting there), but if I give the dummy ship thrust and send it on its way, the detection fails even when I'm inside the dummy ship. Code below just uses the X/Y axis of my spaceship for simplicity, it will test the X/Y of the ship's outer vectors once I get this part working. This is the base distance detector to see if they are near (SpaceObject class contains mesh data as objectMesh)
public void DetectCollision( SpaceObject soOne, SpaceObject soTwo  )
{
if( GetDistance( ref soOne.msngX, ref soTwo.msngX, ref soOne.msngY, ref soTwo.msngY ) <= soOne.objectRadius + soTwo.objectRadius )
{
bool bCollide = CheckIntersectObject( soTwo.objectMesh,
new Vector3( soOne.msngX, soOne.msngY, -cameraHeight ),
new Vector3( soOne.msngX, soOne.msngY, 5.0f ),
Matrix.RotationZ( soOne.msngFacing ) *
Matrix.Translation( soOne.msngX, soOne.msngY, 0 ) );
if( bCollide )
Draw2DText( "Collide!", 10, 75, Color.Red );
else Draw2DText( "Collision Near!", 10, 75, Color.Yellow );
}
}


(GetDistance is just one line 'return (float)Math.Sqrt( ( ( x1 - x2 )*( x1 - x2 ) ) + ( ( y1 - y2 ) * ( y1 - y2 ) ) );') If it gets past the distance check, it runs the CheckIntersectObject on the dummy ship's mesh using a ray's start & end based on my spaceship's location (which gets converted to object space to match up with the dummy ship's mesh)
bool CheckIntersectObject( Mesh objectMesh, Vector3 TestRayStart, Vector3 TestRayEnd, Matrix ObjWorldMat )
{
bool Hit;
float XDiff, YDiff, ZDiff;
Vector3 vecDirection;
Vector3 vecOrigin;
XDiff = TestRayEnd.X - TestRayStart.X;
YDiff = TestRayEnd.Y - TestRayStart.Y;
ZDiff = TestRayEnd.Z - TestRayStart.Z;

vecOrigin.X		= TestRayStart.X;
vecOrigin.Y		= TestRayStart.Y;
vecOrigin.Z		= TestRayStart.Z;
vecDirection.X	= XDiff;
vecDirection.Y	= YDiff;
vecDirection.Z	= ZDiff;

//Inverse Matrix
if( !ObjWorldMat.Equals( null ) )
{
Matrix.Invert( ObjWorldMat );
}

Vector3.TransformCoordinate( vecOrigin, ObjWorldMat );
Vector3.TransformNormal( vecDirection, ObjWorldMat );
Vector3.Normalize( vecDirection );

Hit = objectMesh.Intersect( vecOrigin, vecDirection );
return Hit;
}


I'm fairly new to DirectX and don't have a total grasp on the matrix functions and manipulations, so I don't see why this isn't working since the dummy ship's mesh data is always kept in object space. It shouldn't matter if the ship is moving or not, unless of course I messed up my math somewhere on the ray calculation. Any help is appreciated, thanks! Cyric cyric74@hotmail.com [Edited by - cyric74 on September 3, 2004 3:12:57 AM]

##### Share on other sites
Hmm I'm still having problems with this. Any suggestions would help greatly. Do I have a fundamental misunderstanding of how this works? Should I take this to the newbie forum?

Thanks,
Cyric

##### Share on other sites
In your call to Matrix.Invert, are you sure that you don't need to do something like:

ObjWorldMat = Matrix.Invert( ObjWorldMat );

??

I don't have the managed docs in front of me right now but you might want to check this.

In general, you shouldn't be using Mesh.Intersecto for collision detection. It's not made for that.

You should use some kind of bounding volume check. Let's say you define a sphere (just a center and a radius) around each object in your world. You can test if two objects have collided simply by checking to see if the distance between their centers is less than or equal to the sum of their radii. It's not an exact collision but often times, its close enough.

This topic has been discussed many times on in this forum. If you're interested in this method, do a search for bounding volume/sphere/box or collision detection in general and you'll see tons of info.

neneboricua

• 18
• 11
• 20
• 9
• 52
• ### Forum Statistics

• Total Topics
631397
• Total Posts
2999812
×