我有一个关于XNA模型旋转的问题。问题是 - 我应该怎么做(哪些值应该改变以及如何改变),才能以那种方式旋转绿色模型(红色箭头):
http://img843.imageshack.us/i/question1.jpg/
用于绘制的代码:
DrawModel(elementD, new Vector3(-1, 0.5f, 0.55f), 0,-90,0);
private void DrawModel(Model model, Vector3 position, float rotXInDeg, float rotYInDeg, float rotZInDeg)
{
float rotX = (float)(rotXInDeg * Math.PI / 180);
float rotY = (float)(rotYInDeg * Math.PI / 180);
float rotZ = (float)(rotZInDeg * Math.PI / 180);
Matrix worldMatrix = Matrix.CreateScale(0.5f, 0.5f, 0.5f) * Matrix.CreateRotationY(rotY) *Matrix.CreateRotationX(rotX)*Matrix.CreateRotationZ(rotZ)* Matrix.CreateTranslation(position);
Matrix[] xwingTransforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(xwingTransforms);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.View = cam.viewMatrix;
effect.Projection = cam.projectionMatrix;
effect.World = (xwingTransforms[mesh.ParentBone.Index] * worldMatrix);
}
mesh.Draw();
}
}
所以,我试图通过稍微修改我的代码来应用你的解决方案:
Matrix worldMatrix = Matrix.CreateScale(0.5f, 0.5f, 0.5f) * Matrix.CreateRotationY(rotY) * Matrix.CreateRotationX(rotX) * Matrix.CreateTranslation(position)
* Matrix.CreateRotationX((float)(45 * Math.PI / 180)) * Matrix.CreateRotationZ(rotZ) * Matrix.CreateRotationX((float)(-45 * Math.PI / 180));
通过更改rotZ参数,我确实能够旋转模型。然而,效果并不是我想要的(链接),它改变了位置。这是因为有缺陷的模型还是其他错误?我希望“圆柱体”保持在原位。你知道我该怎么做吗?