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

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

问题描述

我之前问过一个关于为什么我的碰撞不起作用的问题.我得到了一个很好的答案:将我在 DrawModel 方法中所做的相同变换应用到 isCollision 方法.然而,这并没有奏效.我无法弄清楚如何在 isCollision 方法中进行相同的转换.如果有人可以帮助我,那就太好了.谢谢!方法如下:

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.

推荐答案

我无法弄清楚如何在isCollision 方法.如果有人可以帮助我,那就太好了

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

在应用任何移动或旋转后,您可以通过在 Update 方法中构建变换矩阵来完成此操作.然后保存构建的矩阵并将其传递给碰撞测试方法和绘制方法使用.

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

然后

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

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

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

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