XNA 3D碰撞矩阵不工作 [英] XNA 3D Collision matrices not working

查看:178
本文介绍了XNA 3D碰撞矩阵不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我previously问一个为什么我撞不工作的问题。我有一个很好的答案这是有道理的:应用我在DrawModel方法将isCollision方法也做了同样的变换。然而,这并没有工作。我无法弄清楚如何使相同的变换在isCollision方法。如果有人可以帮助我,那将是巨大的。谢谢!以下是方法:

 私人布尔checkPlayerCollision(型号型号1,矩阵world1)
{
    //使地板矩阵
    矩阵flo​​orMatrix = Matrix.CreateTranslation(新Vector3类型(0,0,0));
    //使SHIP1矩阵
    矩阵ship1WorldMatrix = Matrix.CreateTranslation(ship1loc);
    //使SHIP2矩阵
    矩阵ship2WorldMatrix = Matrix.CreateTranslation(ship2loc);
    //检查与地面碰撞
    如果(IsCollision(型号1,world1,地板,floorMatrix))返回true;
    //检查与SHIP1碰撞
    如果(IsCollision(型号1,world1,型号,ship1WorldMatrix))返回true;
    //检查与SHIP2碰撞
    如果(IsCollision(型号1,world1,型号,ship2WorldMatrix))返回true;
    返回false;
}
 

这是检查选手的碰撞,我检查所有的模型与玩家模式的碰撞。

 私人布尔IsCollision(型号型号1,矩阵world1,型号模型2,矩阵world2)
{
    对于(INT meshIndex1 = 0; meshIndex1< model1.Meshes.Count; meshIndex1 ++)
    {
        BoundingSphere所sphere1 = model1.Meshes [meshIndex1] .BoundingSphere;
        sphere1 = sphere1.Transform(world1);

        对于(INT meshIndex2 = 0; meshIndex2< model2.Meshes.Count; meshIndex2 ++)
        {
            BoundingSphere所sphere2 = model2.Meshes [meshIndex2] .BoundingSphere;
            sphere2 = sphere2.Transform(world2);

            如果(sphere1.Intersects(sphere2))
                返回true;
        }
    }
    返回false;
}
 

这是我的实际检查碰撞的方法。

 私人无效DrawModel(型号模型,矩阵世界,矩阵视图,投影矩阵,Vector3类型LOC)
{
    矩阵gameWorldRotation = Matrix.CreateRotationX(MathHelper.ToRadians(的rotationX))* Matrix.CreateRotationY(MathHelper.ToRadians(将RotationY));
    矩阵[]变换=新的Matrix [model.Bones.Count]
    model.CopyAbsoluteBoneTransformsTo(转换);
    的foreach(ModelMesh网在model.Meshes)
    {
        的foreach(在mesh.Effects BasicEffect效果​​)
        {
            effect.World = gameWorldRotation *变换[mesh.ParentBone.Index] * Matrix.CreateTranslation(LOC);
            GraphicsDevice.RenderState.DepthBufferEnable = TRUE;
            effect.EnableDefaultLighting();

            //effect.World =世界;
            effect.View =图。
            effect.Projection =预测;
        }

        mesh.Draw();
    }
}
 

这是我的画模型,并进行矩阵变换的方法。 更多code可应随着更多信息的要求。

解决方案
  

我无法弄清楚如何使在相同的变换   isCollision方法。如果有人可以帮助我,那将是巨大的。

您做到这一点通过构建变换矩阵的更新方法已应用的任何移动或旋转后。然后保存内置矩阵,并通过它在这两个碰撞试验方法的使用和借鉴的方法。

 矩阵TransformationMatrix;
无效更新()
        {
        TransformationMatrix = Matrix.Create ... X(的rotationX)* Matrix.Create ... Y(将RotationY)*转换[mesh.ParentBone.Index] * Matrix.CreateTranslation(LOC);
        }
 

然后

 (IsCollision(TransformationMatrix)
{
sphere.Transform(TransformationMatrix);
}
 

  DrawModel(TransformationMatrix)
{
 effect.World = TransformationMatrix;
}
 

I previously asked a question about why my collision was not working. I got a good answer which makes sense: Apply the same transforms that I did in the DrawModel method to the isCollision method. This however, did not work. I was unable to figure out how to make the same transformations in the isCollision method. If anyone could help me, that would be great. Thanks! Here are the methods:

private bool checkPlayerCollision(Model model1, Matrix world1)
{
    //Make floor matrix
    Matrix floorMatrix = Matrix.CreateTranslation(new Vector3(0, 0, 0));
    //Make ship1 matrix
    Matrix ship1WorldMatrix = Matrix.CreateTranslation(ship1loc);
    //Make ship2 matrix
    Matrix ship2WorldMatrix = Matrix.CreateTranslation(ship2loc);
    //Check for collision with floor
    if (IsCollision(model1, world1, floor, floorMatrix)) return true;
    //Check for collision with ship1
    if (IsCollision(model1, world1, model, ship1WorldMatrix)) return true;
    //Check for collision with ship2
    if (IsCollision(model1, world1, model, ship2WorldMatrix)) return true;
    return false;
}

That was the check player collision where I check all the models for collision with the player model.

private bool IsCollision(Model model1, Matrix world1, Model model2, Matrix world2)
{
    for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++)
    {
        BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere;
        sphere1 = sphere1.Transform(world1);

        for (int meshIndex2 = 0; meshIndex2 < model2.Meshes.Count; meshIndex2++)
        {
            BoundingSphere sphere2 = model2.Meshes[meshIndex2].BoundingSphere;
            sphere2 = sphere2.Transform(world2);

            if (sphere1.Intersects(sphere2))
                return true;
        }
    }
    return false;
}

That was the method where I actually check the collision.

private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection, Vector3 loc)
{
    Matrix gameWorldRotation = Matrix.CreateRotationX(MathHelper.ToRadians(RotationX)) * Matrix.CreateRotationY(MathHelper.ToRadians(RotationY));
    Matrix[] transforms = new Matrix[model.Bones.Count];
    model.CopyAbsoluteBoneTransformsTo(transforms);
    foreach (ModelMesh mesh in model.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.World = gameWorldRotation * transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(loc);
            GraphicsDevice.RenderState.DepthBufferEnable = true;
            effect.EnableDefaultLighting();

            //effect.World = world;
            effect.View = view;
            effect.Projection = projection;
        }

        mesh.Draw();
    }
}

And that was the method where I draw the models and make the matrix transformations. More code is available upon request along with any more information.

解决方案

I was unable to figure out how to make the same transformations in the isCollision method. If anyone could help me, that would be great

You do this by building the transformation matrix in the Update method after you've applied any movement or rotation. Then save that built matrix and pass it for use in both collision test method and draw method.

Matrix TransformationMatrix;   
void Update()
        {
        TransformationMatrix = Matrix.Create...X(RotationX) * Matrix.Create...Y(RotationY) * transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(loc);
        }

then

(IsCollision(TransformationMatrix )
{
sphere.Transform(TransformationMatrix );
} 

and

DrawModel(TransformationMatrix )
{
 effect.World = TransformationMatrix ;
}

这篇关于XNA 3D碰撞矩阵不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆