XNA, as far as I know, contains no built-in code to collide ModelMesh
objects. If you want to collide an arbitrary mesh, you will need to write that code yourself-- not an enviable task if you ask me.
The only built-in collision abilities I'm aware of in XNA are BoundingBox
, BoundingSphere
, and Ray
. All three of these structs have built-in methods to collide with each other.
I believe common practice in XNA 3D development is to use your ModelMesh
for rendering and create a separate set of BoundingBox
es and BoundingSphere
s as a collision model. I'd probably create a container class for BoundingBox
and BoundingSphere
called BoundingPrimitive
and expose the Intersects
method (it's a little lame that BoundingBox/Sphere
don't inherit from the same object, making generic collision structures a bit more difficult). Something like this maybe:
public class BoundingPrimitive
{
BoundingSphere? sphere;
BoundingBox? box;
public BoundingPrimitive(BoundingBox b) { box = b; }
public BoundingPrimitive(BoundingSphere s) { sphere = s; }
public bool Intersects(BoundingPrimitive other, Matrix myWorld)
{
if (box.HasValue)
{
//a world transformation must be made to translate the bounding structure
//to world space (it should be created in local space of the object
//whose collision cage it represents)
return box.Value.Transform(myWorld).Intersects(other.box.HasValue ? other.box : other.sphere);
}
else if (sphere.HasValue)
{
return sphere.Value.Transform(myWorld).Intersects(other.box.HasValue ? other.box : other.sphere);
}
else
{
throw new InvalidOperationException("This BoundingPrimitive contains no bounding structure!");
}
}
/*there are probably some Intersects overrides to also expose*/
}
So, then create a collision structure for each object with a ModelMesh
in which you manually define an array of these BoundingPrimitive
s to abstract the model's collision 'cage'. This can be done at your discretion. We can call this collision structure Collider
. Collider
will have its own Intersects
method, which takes another Collider
as a parameter. Then it compares its array of BoundingPrimitives
to the other's and tests for collision between them. That's kind of an expensive operation as the number of bounding structures per object increases, so you could probably optimize that somewhere, or use a small number of bounding structures to abstract the collision cage.
So this is my best answer for you, I'm sure some of the wiser XNA gurus may have a bone to pick with it.
The main thing to keep in mind is that XNA is not an engine, it's a framework. You don't really get much physics or collision for free. You'll need to come up with the appropriate models, structures, algorithms and resolutions yourself.