0

私は自分のゲームで壁の衝突に取り組んでいますが、現在の方法では、壁に足を踏み入れて動けなくなります。キャラクターを壁で滑らせようとしていますが、それでも衝突します。

私のキャラクターは、彼が向いている角度を使用して作成したベクトルから離れます。

これは私の衝突関数です:

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

1 に答える 1

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 が何であるかはわかりません...概念を表現するためにそれらを無視しました

于 2013-08-14T01:07:51.507 に答える