我有一个看起来会起作用的碰撞方法,但确实如此,但是边界球总是在0,0,0处。我如何解决这个问题?根据要求,可以提供更多代码和/或详细信息。任何帮助表示赞赏。先谢谢!
BoundingSphere CreateBoundingSphereForModel(Model model, Matrix worldMatrix)
{
Matrix[] boneTransforms = new Matrix[this.model.Bones.Count];
this.model.CopyAbsoluteBoneTransformsTo(boneTransforms);
BoundingSphere boundingSphere = new BoundingSphere();
BoundingSphere meshSphere;
for (int i = 0; i < model.Meshes.Count; i++)
{
meshSphere = model.Meshes[i].BoundingSphere.Transform(boneTransforms[i]);
boundingSphere = BoundingSphere.CreateMerged(boundingSphere, meshSphere);
}
BoundingSphereRenderer.Render(boundingSphere.Transform(worldMatrix), GraphicsDevice, view, projection, Color.Green);
return boundingSphere.Transform(worldMatrix);
}
bool IsCollision2(Model model1, Matrix world1, Model model2, Matrix world2)
{
BoundingSphere bs1 = CreateBoundingSphereForModel(model1, world1);
BoundingSphere bs2 = CreateBoundingSphereForModel(model2, world2);
if (bs1.Intersects(bs2))
return true;
return false;
}
private bool checkPlayerCollision(Model model1, Matrix world1)
{
//Make player location matrix
Vector3 playloc = new Vector3(X, Y, Z);
//Make ship1 matrix
Matrix ship1WorldMatrix = Matrix.CreateTranslation(ship1loc) * Matrix.CreateScale(0.1f);
//Make ship2 matrix
Matrix ship2WorldMatrix = Matrix.CreateTranslation(ship2loc) * Matrix.CreateScale(0.1f);
//Check for collision with ship1
if (IsCollision2(model1, world1, model, ship1WorldMatrix)) return true;
//Check for collision with ship2
if (IsCollision2(model1, world1, model, ship2WorldMatrix)) return true;
return false;
}
答案 0 :(得分:0)