XNA边界球体不会显示在正确的位置

时间:2013-04-13 17:56:58

标签: animation xna

我正在尝试为我的模型正确显示边界球体,以便我可以看到当我进行碰撞测试时发生了什么。我无法在正确的位置绘制边界球体。模特看起来很好。我认为问题出在边界球体的转换步骤中。球体本身是一个简单的.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();
    }
}

0 个答案:

没有答案