1

OK、私は間違いなく痛々しいほど明白な何かを見落としていますが、ここに問題があります:

私のプロジェクトでは、2 種類の衝突を使用しています: 球から球へ、およびボックスからボックスへ。どちらも同じ問題を抱えています。2 つのオブジェクト間の衝突は常に検出されます。

baseGameObject クラスで、バウンディング ボックスを宣言します。

       BoundingBox bb;

モデルのバウンディング ボックスを作成し、それを使用して bb を定義するメソッドもあります。

      public void Initialize()
      {
          bb = CreateBoundingBox();
      }


    protected BoundingBox CalculateBoundingBox()
    {

        Vector3 modelMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);
        Vector3 modelMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
        transforms = new Matrix[model.Bones.Count];

        foreach (ModelMesh mesh in model.Meshes)
        {
            Vector3 meshMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);
            Vector3 meshMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);

            foreach (ModelMeshPart part in mesh.MeshParts)
            {
                int stride = part.VertexBuffer.VertexDeclaration.VertexStride;

                byte[] vertexData = new byte[stride * part.NumVertices];
                part.VertexBuffer.GetData(part.VertexOffset * stride, vertexData, 0, part.NumVertices, 1); // fixed 13/4/11

                Vector3 vertPosition = new Vector3();
                for (int ndx = 0; ndx < vertexData.Length; ndx += stride)
                {
                    vertPosition.X = BitConverter.ToSingle(vertexData, ndx);
                    vertPosition.Y = BitConverter.ToSingle(vertexData, ndx + sizeof(float));
                    vertPosition.Z = BitConverter.ToSingle(vertexData, ndx + sizeof(float) * 2);

                    meshMin = Vector3.Min(meshMin, vertPosition);
                    meshMax = Vector3.Max(meshMax, vertPosition);
                }
            }

            meshMin = Vector3.Transform(meshMin, transforms[mesh.ParentBone.Index]);
            meshMax = Vector3.Transform(meshMax, transforms[mesh.ParentBone.Index]);

            modelMin = Vector3.Min(modelMin, meshMin);
            modelMax = Vector3.Max(modelMax, meshMax);
        }

        return new BoundingBox(modelMin, modelMax);

    }

次に、衝突に bb を使用する方法を作成しました。

    public bool BoxCollision(BoundingBox secondBox)
    {
        if (bb.Intersects(secondBox))
            return true;
        else            
            return false;
    }

そして最後に、メソッドを使用して衝突検出を決定します。

    public void CollisionCheck()
    {
        foreach (NonPlayerChar npc in npcList)
        {
            if(player.SphereCollision(npc.model, npc.getWorldRotation()))
            { npc.position = vector3.Zero; }

            if (player.BoxCollision(npc.bb))
            { npc.position = vector3.Zero; }                 
        }

    }

位置の問題は、それらが衝突するかどうかを確認するためのテストでした。オブジェクトの位置を任意の位置に設定できますが、衝突は引き続き検出されます。境界球の衝突についても同じ問題があります。

   public bool SphereCollision(Model secondModel, Matrix secondWorld)
    {
        foreach (ModelMesh modelMeshes in model.Meshes)
        {
            foreach (ModelMesh secondModelMesh in secondModel.Meshes)
            {
                if(modelMeshes.BoundingSphere.Transform(getWorldRotation()).Intersects(secondModelMesh.BoundingSphere.Transform(secondWorld)))
                    return true;
            }
        }
        return false;
    }

私が間違っていることを誰かが知っていますか?

4

1 に答える 1

1

最も簡単な方法の1つは、両方の球を取得して、そこで座標を比較することです。

例えば

sphere1_x = sphere.x;
sphere2_x = sphere2.x;
width of sphere = 2 for example;

if ((sphere1_x + width/2) > (sphere2_x + width/2))
system.out.writeline("collison");
else if (sphere1_x (check for other ways of connecting between the coordinate system x>x2, x<x2, y>y2, y<y2 etc)
else
system.out.writeline("no collision")

次に、本当に必要な場合は、コードを上記の方法にリファクタリングできます。おそらく、球の前に最初にバウンディングボックスを実行する方が簡単です。

于 2011-08-02T13:35:12.683 に答える