Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

When I run the collision code, there are no errors, but when I shoot the enemy nothing happens..... Any problems that are blindingly obvious?

Class Level Variables

public Model model { get; protected set; }

Collision Code

        public bool CollidesWith(Model otherModel, Matrix otherWorld)
    {
        // Loop through each ModelMesh in both objects and compare
        // all bounding spheres for collisions
        foreach (ModelMesh myModelMeshes in model.Meshes)
        {
            foreach (ModelMesh hisModelMeshes in otherModel.Meshes)
            {
                if (myModelMeshes.BoundingSphere.Transform(
                    GetWorld()).Intersects(
                    hisModelMeshes.BoundingSphere.Transform(otherWorld)))
                    return true;
            }
        }
        return false;
    }
share|improve this question
2  
You should do some debugging. Create an obvious "hit" scenario, and see if the bounding sphere positions are right. I think the transformations might not be correct. –  Marton Aug 19 at 7:00
 
One thing I find really useful is adding Debugger.Break() (or throwing an exception) for methods when "nothing happens." That helps troubleshoot real fast (eg. not building in Any CPU so can't debug easily). –  ashes999 Aug 19 at 10:12
1  
Note your myModelMeshes.BoundingSphere.Transform(GetWorld()) should be saved in a variable -- it only needs to be done once per iteration of the outer loop, not repeatedly in the inner loop. –  bobobobo Aug 19 at 14:58

1 Answer

up vote 3 down vote accepted

Seems to me that you are not applying the bone transforms.

If you are going to evaluate collisions this way, it would be much better to calculate the absolute bounding spheres in world coordinates. This way you avoid to repeat transformations.

 class MyModel {
       public Model Model;
       public Matrix MyWorld;
       public BoundingSphere[] Bounds;
       public Matrix[] MeshTransform;

       public void CalculateAbsoluteBounds() {
           Matrix[] transforms = new Matrix[Model.Bones.Count];
           Model.CopyAbsoluteBoneTransformsTo(transforms);

           for (int i=0; i<Model.Meshes.Count; i++)
           {
              // You can use MeshTransform to draw the model...
              Matrix.Multiply( ref transforms[Model.Meshes[i].ParentBone.Index], 
                               ref MyWorld, 
                               out MeshTransform[i]); 
              // Transform bounding sphere now with MeshTransform
              Model.Meshes[i].BoundingSphere.Transform(
                   ref MeshTransform[i], out Bounds[i]);     
           }
       }
 }

And it would be a great idea to draw wireframe spheres to debug them easily

share|improve this answer
 
When i try that I get 3 errors, which are: Error 1 The best overloaded method match for 'Microsoft.Xna.Framework.BoundingSphere.Transform(ref Microsoft.Xna.Framework.Matrix, out Microsoft.Xna.Framework.BoundingSphere)' has some invalid arguments Error 2 Argument 1 must be passed with the 'ref' keyword Error 3 Argument 2 must be passed with the 'out' keyword –  RageGolem10 Aug 21 at 5:12
 
I wrote this without testing ... I have edited the code... –  Blau Aug 21 at 8:06

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.