Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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);
        }
        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 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.

share|improve this answer
1  
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. –  Donnie Nov 30 '12 at 23:10
    
Yeah, sounds right. Don't know why I didn't think of that. –  A-Type Dec 1 '12 at 4:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.