Collision always detected, even when they're shouldn't be a collision
OK I'm definitely overlooking something painfully obvious but here's the problem:
In my project I'm using two types of collision: sphere to sphere and box to box. Both are experiencing the same problem; they always detect a collision between the two objects.
in my baseGameObject class I declare the bounding box:
BoundingBox bb;
I also have the method that creates a boundingbox for a model and use that to define 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);
tr开发者_StackOverflow社区ansforms = 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);
}
I then made a method to use bb for my collision.
public bool BoxCollision(BoundingBox secondBox)
{
if (bb.Intersects(secondBox))
return true;
else
return false;
}
And finally I use the method to determine collision detection.
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; }
}
}
the position thing was a test to see if they collide. I can set the objects position to any position and the collision is still detected. I have the same problem for the bounding sphere collision.
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;
}
Does anyone know what I'm doing wrong?
One of the easiest things to do is to grab both your spheres and compare there coordinates..
e.g.
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")
Then if you really want you can refactor your code to how youve got it above. Probably easier to do the bounding boxes first before the spheres.
精彩评论