需要XNA 3D碰撞帮助

时间:2011-04-25 13:27:45

标签: 3d xna collision-detection

我今晚午夜(大约9个小时)有一个截止日期,我一直在处理这个问题几个小时,这让我很生气。我对XNA很新,所以请深入解释一下如果可能的话我应该做什么:)任何我编程这个小游戏,你是一个飞到一些正方形的球体然后你碰撞时得到点。它应该很容易,但对于我的生活,我无法找到一种方法来检测碰撞,我已经搜索谷歌的年龄,我发现的唯一的东西,我可以很容易地实现我的代码。

Anywho是我的代码,所以你可以大致了解我的用法:

 public class Cmodel
{
   public Vector3 Position { get; set; }
   public Vector3 Rotation { get; set; }
   public Vector3 Scale { get; set; }

   public Model Model { get; set; }
   private Matrix[] modelTransforms;

   private GraphicsDevice graphicsDevice;
   private BoundingSphere boundingSphere;

   private void buildBoundingSphere()
   {
       BoundingSphere sphere = new BoundingSphere(Vector3.Zero, 0);
       foreach (ModelMesh mesh in Model.Meshes)
       {
           BoundingSphere transformed = mesh.BoundingSphere.Transform(modelTransforms[mesh.ParentBone.Index]);
           sphere = BoundingSphere.CreateMerged(sphere, transformed);

       }
       this.boundingSphere = sphere;
   }
   public Cmodel(Model Model, Vector3 Position, Vector3 Rotation, Vector3 Scale, GraphicsDevice graphicsDevice)
   {
       this.Model = Model;
       modelTransforms = new Matrix[Model.Bones.Count];
       Model.CopyAbsoluteBoneTransformsTo(modelTransforms);

       buildBoundingSphere();
       this.Position = Position;
       this.Rotation = Rotation;
       this.Scale = Scale;

       this.graphicsDevice = graphicsDevice;

   }
   public BoundingSphere BoundingSphere
   {
       get
       {
           Matrix worldTransform = Matrix.CreateScale(Scale) * Matrix.CreateTranslation(Position);
           BoundingSphere transformed = boundingSphere;
           transformed = transformed.Transform(worldTransform);

           return transformed;
       }
   }



   public void Draw(Matrix View, Matrix Projection)
   {
       Matrix baseWorld = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) * Matrix.CreateTranslation(Position);

       foreach (ModelMesh mesh in Model.Meshes)
       {
           Matrix localWorld = modelTransforms[mesh.ParentBone.Index] * baseWorld;

           foreach (ModelMeshPart meshPart in mesh.MeshParts)
           {
               BasicEffect effect = (BasicEffect)meshPart.Effect;

               effect.World = localWorld;
               effect.View = View;
               effect.Projection = Projection;

               effect.EnableDefaultLighting();

           }
           mesh.Draw();
       }
   }
}

我知道很多代码,但我不知道如何解释它。

到目前为止,我尝试过的事情是制作一个if句子,然后尝试拦截我的模型

看起来像这样:

if (mymodel.Intersects(models))
        {

        }

List<Cmodel> models = new List<Cmodel>();
    List<Cmodel> mymodel = new List<Cmodel>();

我希望这能很好地解释我的问题是什么

提前致谢

1 个答案:

答案 0 :(得分:1)

如果模型不是太多,这里有一个基本的蛮力方式:

foreach(Cmodel cm in models)//assuming these are the squares
{
  if(playerSphere.boundingSphere.Intersect(cm.boundingSphere))
    {
        //yay! add points.
    }
}

这是您需要的功能,还是需要不同的功能?