我试图让边界球为我的模型正确显示,这样我就可以看到在进行碰撞测试时发生了什么。我无法让边界球在正确的位置绘制。模型看起来不错。我相信问题出在边界球的转换步骤中。球体本身是一个简单的 .x 文件,半径为 1 个单位(在 boundingSphereModel 中)。然后我将它缩放到实际网格的边界球半径。我确实有具有不同半径的球体,但它们都显示在玩家脚下而不是身体各个部位上。我究竟做错了什么?
这是我的代码:
private void drawPlayer(Matrix viewMatrix, Player player)
{
SkinnedModel skinnedModel = player.getModel();
Matrix[] boneTransforms = new Matrix[skinnedModel.Model.Bones.Count];
skinnedModel.Model.CopyAbsoluteBoneTransformsTo(boneTransforms);
Matrix transformationMatrix = player.getWorldTransformation();
foreach (ModelMesh modelMesh in player.getMeshes())
{
if (drawBoundingSpheres)
{
int boneIndex = modelMesh.ParentBone.Index;
Matrix boneTransform = boneTransforms[boneIndex];
GraphicsDevice.RenderState.FillMode = FillMode.WireFrame;
Matrix scaleMatrix = Matrix.CreateScale(modelMesh.BoundingSphere.Radius);
Matrix worldMatrix = scaleMatrix * boneTransform * transformationMatrix;
// loop through all bounding sphere's meshes, transforming & drawing them
foreach (ModelMesh boundingSphereModelMesh in boundingSphereModel.Meshes)
{
foreach (ModelMeshPart meshPart in boundingSphereModelMesh.MeshParts)
{
BasicEffect basicEffect = (BasicEffect)meshPart.Effect;
basicEffect.World = worldMatrix;
basicEffect.View = viewMatrix;
basicEffect.Projection = cameraProjection;
// configure lighting
basicEffect.AmbientLightColor = new Vector3(0.1f);
}
boundingSphereModelMesh.Draw();
}
// done with bounding spheres; switch back to solid fill mode
GraphicsDevice.RenderState.FillMode = FillMode.Solid;
}
// draw model mesh
foreach (ModelMeshPart meshPart in modelMesh.MeshParts)
{
SkinnedModelBasicEffect basicEffect = (SkinnedModelBasicEffect)meshPart.Effect;
basicEffect.World = transformationMatrix;
basicEffect.Bones = player.getBones();
basicEffect.View = viewMatrix;
basicEffect.Projection = cameraProjection;
basicEffect.Material.EmissiveColor = new Vector3(0.0f);
basicEffect.Material.DiffuseColor = new Vector3(0.8f);
basicEffect.Material.SpecularColor = new Vector3(0.3f);
basicEffect.Material.SpecularPower = 8;
basicEffect.NormalMapEnabled = false;
basicEffect.SpecularMapEnabled = false;
// configure lighting
basicEffect.AmbientLightColor = new Vector3(0.1f);
basicEffect.LightEnabled = true;
basicEffect.EnabledLights = EnabledLights.Four;
basicEffect.PointLights[0].Color = LIGHT_COLOR;
basicEffect.PointLights[0].Position = COLOR0_POSITION;
basicEffect.PointLights[1].Color = LIGHT_COLOR;
basicEffect.PointLights[1].Position = COLOR1_POSITION;
basicEffect.PointLights[2].Color = LIGHT_COLOR;
basicEffect.PointLights[2].Position = COLOR2_POSITION;
basicEffect.PointLights[3].Color = LIGHT_COLOR;
basicEffect.PointLights[3].Position = COLOR3_POSITION;
}
modelMesh.Draw();
}
}