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; 
                collisionPrimitives.Add(currentPrimitive); 
            } 


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

Result1: http://img684.imageshack.us/img684/6480/collisionrectangle1.png

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; 
 
                    collisionPrimitives.Add(prim); 


The results just get more wierd.

Result 2: http://img192.imageshack.us/img192/6217/collisionrectangle2.png

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; 
 
                    collisionPrimitives.Add(prim); 


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

Result 3: http://img190.imageshack.us/img190/4450/collisionrectangle3.png

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