3d墙滑动碰撞

时间:2013-08-14 00:40:05

标签: c# 3d xna collision

我正在为我的游戏中的墙壁碰撞工作,以及我现在拥有它的方式我被卡在墙上。我试图让我的角色在墙上滑动但仍然碰撞。

我的角色使用他面对的角度移动我创建的矢量。

这是我的碰撞功能:

    private static bool CheckForCollisions(ref Crate c1, ref Player c2,bool direction)
    {
        for (int i = 0; i < c1.model.Meshes.Count; i++)
        {
            // Check whether the bounding boxes of the two cubes intersect.
            BoundingSphere c1BoundingSphere = c1.model.Meshes[i].BoundingSphere;
            c1BoundingSphere.Center += c1.position + new Vector3(2, 0, 2);
            c1BoundingSphere.Radius = c1BoundingSphere.Radius / 1.5f;

            for (int j = 0; j < c2.model.Meshes.Count; j++)
            {
                BoundingSphere c2BoundingSphere = c2.model.Meshes[j].BoundingSphere;
                if (direction)
                    c2BoundingSphere.Center += c2.position + new Vector3(c2.getPlannedDirection().X, 0, 0);
                else if (!direction)
                    c2BoundingSphere.Center += c2.position + new Vector3(0, 0, c2.getPlannedDirection().Y);
                //c2BoundingSphere.Center += c2.position;

                if (c1BoundingSphere.Intersects(c2BoundingSphere))
                {
                    return true;
                }
            }
        }
        return false;
    }

这是我的更新:

for (int x = 0; x <= 29; x++)
        {
            for (int y = 0; y <= 29; y++)
            {
                if (crate[x, y].getType() == 11 && collisionEnabled)
                { 
                    if (CheckForCollisions(ref crate[x, y], ref player,true))
                    {
                        player.clearPlannedDirectionX();
                        //Console.Write(player.getPosition().X + "," + player.getPosition().Y + "," + player.getPosition().Z);
                        //movePlayer = false;
                    }
                    if (CheckForCollisions(ref crate[x, y], ref player,false))
                    {
                        player.clearPlannedDirectionZ();
                        //Console.Write(player.getPosition().X + "," + player.getPosition().Y + "," + player.getPosition().Z);
                        //movePlayer = false;
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

这看起来应该是在gamedev堆栈交换上,我建议切换到射线测试而不是球体。

无论如何,如果你嫁给了基于球体的系统,你可以将玩家“推”出墙外:

private static bool CorrectCollisions(ref Crate c1, ref Player c2)
{
    for (int i = 0; i < c1.model.Meshes.Count; i++)
    {
        // Check whether the bounding boxes of the two cubes intersect.
        BoundingSphere c1BoundingSphere = c1.model.Meshes[i].BoundingSphere;

        c1BoundingSphere.Center += c1.position + new Vector3(2, 0, 2);
        c1BoundingSphere.Radius = c1BoundingSphere.Radius / 1.5f;

        for (int j = 0; j < c2.model.Meshes.Count; j++)
        {
            BoundingSphere c2BoundingSphere = c2.model.Meshes[j].BoundingSphere;
            c2BoundingSphere.Center += c2.position;

            Vector3 dir=c2BoundingSphere.Center - c1BoundingSphere.Center;
            float center_dist_sq=dir.dot(dir);
            float min_dist=c2BoundingSphere.Radius+c1BoundingSphere.Radius;
            if (center_dist_sq < min_dist*min_dist) {
                    dir.normalize();
                    c2.position += dir*(min_dist-sqrt(center_dist_sq));
            }
        }
    }
    return false;
} 

然后您的更新看起来更像:

for (int x = 0; x <= 29; x++) {
    for (int y = 0; y <= 29; y++) {
        if (crate[x, y].getType() == 11 && collisionEnabled) {
            CorrectCollisions(ref crate[x, y], ref player);
        }
    }
}

我不确定(2,0,2)向量或2 / 3rds的目的是什么......我只是忽略它们来表达概念