1

I have successfully created a bounding sphere collision method for my 3D game. It works just fine, but when I want to check collision on a large flat floor, it creates a huge sphere that does not work. For objects like that, I need a bounding sphere collision method. I tried looking it up and came up with nothing. I then tried to create my own by following the same outline as the bounding sphere collision method, but that also did not work. Does anyone have a bounding box collision method that is preferably similar to the bounding sphere one? Thanks!

    private bool IsCollision(Model model1, Matrix world1, Model model2, Matrix world2)
    {
        Matrix gameWorldRotation = Matrix.CreateRotationX(MathHelper.ToRadians(RotationX)) * Matrix.CreateRotationY(MathHelper.ToRadians(RotationY));
        Matrix[] transforms1 = new Matrix[model1.Bones.Count];
        model1.CopyAbsoluteBoneTransformsTo(transforms1);
        Matrix[] transforms2 = new Matrix[model2.Bones.Count];
        model2.CopyAbsoluteBoneTransformsTo(transforms2);
        foreach (ModelMesh mesh1 in model1.Meshes)
        {
            Matrix meshWorld1 = gameWorldRotation * transforms1[mesh1.ParentBone.Index] * world1;
            foreach (ModelMesh mesh2 in model2.Meshes)
            {
                Matrix meshWorld2 = gameWorldRotation * transforms2[mesh2.ParentBone.Index] * world2;
                if (oldCollision(model1, meshWorld1 * Matrix.CreateScale(0.8f), model2, meshWorld2 * Matrix.CreateScale(0.8f))) return true;
            }
        }
        return false;
    }

    private bool oldCollision(Model model1, Matrix world1, Model model2, Matrix world2)
    {
        for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++)
        {
            BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere;
            sphere1 = sphere1.Transform(world1);

            for (int meshIndex2 = 0; meshIndex2 < model2.Meshes.Count; meshIndex2++)
            {
                BoundingSphere sphere2 = model2.Meshes[meshIndex2].BoundingSphere;
                sphere2 = sphere2.Transform(world2);

                if (sphere1.Intersects(sphere2))
                    return true;
            }
        }
        return false;

    }

1 Answer 1

1

The floor could be a Microsoft.Xna.Framework.Plane object. then you could use the following Xna framework method to check collision between any BoundingSphere and the floor Plane: http://msdn.microsoft.com/en-us/library/bb197613.aspx

Sign up to request clarification or add additional context in comments.

3 Comments

That sounds like a good idea, and I'm sure it is, but I have no clue how to: A. Draw a plane B. Do I have to make a separate model for it, or do I do it all in code? If you could tell me how to make a plane/how to draw a plane then I could do it. Thanks!
If you are already drawing a floor, you do not need to change anything it terms of rendering. A plane, like a bounding sphere or box is an invisible object used for collision testing. The only time you see a collision structure drawn is for debugging purposes. There are a number of ways to create a plane from your current scene. Is your floor always at a certain height relative to the world origin? is the floor a separate model or mesh currently?
Ohhhhhhhhhhhhhhh! I see. I thought it was something you drew. Thanks for that. I am now going to see if it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.