Basic Collision Detection, How To ?

Jan 7, 2011 at 11:34 AM

I tried build a collision boxes like that:

            foreach (CollisionBox box in level.CollisionBoxes) 
                Vector3 centerPos = box.origin; 
                Vector3 scale = box.size - centerPos; 
                Matrix world = Matrix.CreateScale(scale); 
                world.Translation = centerPos; 
                GeometricPrimitive currentPrimitive = new CubePrimitive(GraphicsDevice); 
                currentPrimitive.WorldMatrix = world; 

But i level.CollisionBoxes has only 4 CollisionBox object and i got very wierd collision boxes around my level...


Then I tried BoundingBox.CreateFromSphere method for each model of level. Like:

            for (int i = 0; i < level.Geometry.Length; i++) 
                Model model = level.Geometry[i]; 
                LevelModelParams param = (LevelModelParams)model.Tag; 
                foreach (ModelMesh mesh in model.Meshes) 
                    GeometricPrimitive prim = new CubePrimitive(GraphicsDevice); 
                    BoundingBox bb = BoundingBox.CreateFromSphere(mesh.BoundingSphere); 
                    Vector3 centerPos = bb.Max - bb.Min; 
                    Vector3 scale = bb.Max - centerPos; 
                    Matrix world = Matrix.CreateScale(scale); 
                    world.Translation = centerPos / 2; 
                    prim.WorldMatrix = Matrix.CreateScale(scale); 
                    prim.WorldMatrix.Translation = mesh.BoundingSphere.Center; 

The results just get more wierd.

Result 2:

Then finally i said my self, "hey try some spheres"...

                    GeometricPrimitive prim = new SpherePrimitive(GraphicsDevice); 
                    Vector3 centerPos = mesh.BoundingSphere.Center; 
                    Matrix world = Matrix.CreateScale(mesh.BoundingSphere.Radius); 
                    world.Translation = centerPos; 
                    prim.WorldMatrix = world; 

Then spheres slap me to :P and then i said i need help..

Result 3:

How you do collision detection with this library? Am i have to do something in level design ?