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;
}
私が間違っていることを誰かが知っていますか?