XNA 3D 碰撞 需要帮助

发布于 2024-11-03 12:10:54 字数 2784 浏览 0 评论 0原文

我的截止日期是今晚午夜(大约 9 小时),我已经花了好几个小时来解决这个问题,这让我发疯。我对 XNA 相当陌生,所以请深入解释一下如果可能的话我应该做什么:) 任何正在编程这个小游戏的人,你是一个飞入某些方块的球体,然后当你碰撞时你会得到分数。这应该很容易,但对于我来说,我找不到一种方法来检测碰撞,我在谷歌上搜索了很长时间,我发现的唯一的东西并不是我可以轻松地实现到我的代码中的东西。

任何人都会收到我的代码,这样您就可以大致了解我使用的内容:

 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>();

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

提前致谢

i have a deadline for tonight midnight (in around 9 hours) and i have been working with this problem for a good few hours and its driving me mad. I am rather new to XNA so please explain in depth what i should do if possible :) anywho im programming this little game where you are a sphere that flies into some squares and then you get points when you collide. it should be easy but for the life of me i cant find a way to detect collision, ive been searching google for ages and the only thing i find arent things i can easily implement into my code.

Anywho heres my code so you can get a general idea of what i use:

 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();
       }
   }
}

i know its alot of code but i dont know how else to explain it.

the thing ive tryed so far is to make a if sentence and then try to intercept my models

which looked like this:

if (mymodel.Intersects(models))
        {

        }

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

I hope this explains well enough what my problem is

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一向肩并 2024-11-10 12:10:54

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

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

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

If there isn't too many models, here is a basic brut force way:

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

Is this the functionality you need, or do you need something different?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文