开发者

XNA 3D Collision Help needed

开发者 https://www.devze.com 2023-02-28 07:40 出处:网络
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 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

Thank开发者_开发百科s in advance


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消