I want to have another go at 3D within XNA. What I have seen from some other games that they just have a separate very low poly model "cage" around the environment model. However I can not find any reference to this. I have not that much experience with XNA 3D either.

Is it possible to have this cage within each of my environmental models already? Lets just say I call the mesh within the .FBX wall and col_wall. How would I call to these different meshes within XNA?

The player would just have a tight collision cube around. To make it a bit more efficient I will be making divide the map up by cubes and only calculate collision if the player is in it.

Question two: I can't find anywhere to do cube vs mesh collision. Is there a method for this? Or perhaps it is possible to build my collision cage out of cubes in the 3D app and on loading of the models in XNA replace them directly by cubes? So I could just do box to box collision which should be very cheap and still give the player the ability to move over ledges on the static models.


1 Answer 1


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 BoundingBoxes and BoundingSpheres 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);
            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 BoundingPrimitives 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.

  • 1
    \$\begingroup\$ It's wasteful to try to collide with the entire array every pass if it has many more than 1 BoundingPrimitive. An optimization would be to enclose the mess of them in a single BoundingSphere or BoundingBox to collide with first, and then do the detailed collision detection only if the rough passes. This is a pretty common collision detection optimization. \$\endgroup\$
    – Donnie
    Commented Nov 30, 2012 at 23:10
  • \$\begingroup\$ Yeah, sounds right. Don't know why I didn't think of that. \$\endgroup\$
    – A-Type
    Commented Dec 1, 2012 at 4:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .