나는 확실히 고통스럽게 분명하지만 문제는 여기에 뭔가 내려다 보이는 해요 OK :로 영역 내 프로젝트에서충돌 항상이 충돌하지 않아야 때도 감지
나는 충돌의 두 가지 유형을 사용하고 있습니다를 구 및 상자 상자. 두 가지 모두 동일한 문제가 발생합니다. 그들은 항상 두 대상 간의 충돌을 감지합니다.
BoundingBox bb;
나는 또한 모델에 대한 BoundingBox의를 생성하는 방법을 가지고 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);
}
그때 만들어 내 baseGameObject 클래스의
나는 경계 상자를 선언 내 충돌을 위해 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;
}
내가 뭘 잘못하고 있는지 아는 사람이 있습니까? 할 수있는 가장 쉬운 것들의
'BoxCollision' 함수는 위치를 기반으로하지 않았거나 잘못 읽은 것 같습니다. (이 경우 아마도 (0,0,0)을 중심으로 두 개의 상자가 충돌하는지 ...) –
그리고 'SphereCollision'은 현재 false를 반환 할 수 있습니까? –
우리는 bb.Intersect 메서드에 코드를 볼 수 있습니까? ..? – Olle89