XNA 3D Collision matrices not working

筅森魡賤 提交于 2019-12-11 04:06:48

问题


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.


回答1:


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



回答2:


You should build your world matrices from all three transformations (scale, rotation, translation, in that order). If you used only translation in your game then your method should work fine. Maybe your model is messed up. Try rendering bounding spheres to see if they truly intersect. Here is a really good BoundingSphereRenderer class.



来源:https://stackoverflow.com/questions/20761783/xna-3d-collision-matrices-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!