2011-03-17 64 views
4

我有一个关于XNA模型旋转的问题。现在的问题是 - 我该怎么办(其值应更改以及如何)那样的旋转绿化模范(红色箭头):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参数,我确实能够旋转模型。不过效果不是我想要达到的http://img225.imageshack.us/i/questionau.jpg/,它改变了立场。是因为模型有缺陷还是其他一些错误?我希望“圆柱体”能够保持其位置。你知道我该怎么做?

+0

您是否考虑过注册您的答案或将其标记为已接受?我看到你在这里是新的,所以记得upvotes是什么驱动这个网站... – Pedery 2011-03-24 23:13:07

回答

1

旋转矩阵的旋转是累积的。因此,您可以通过将模型旋转45度“向下”来计算旋转矩阵,然后围绕想要的轴应用旋转,然后再次将模型旋转45度“向上”。这三个矩阵的乘积应该给你你想要的矩阵。

+0

我试过你的解决方案,并更新了我的第一篇文章。非常感谢您的反馈,因为它让我更接近预期的结果。 – Konrad 2011-03-18 14:01:35

+0

你的转变来自你的模型的枢轴中心。当然,你的代码不能“猜测”你恰好想要围绕圆柱轴旋转你的复合模型,而不是整个边界。我不是XNA专家,所以我不知道如何设置模型的旋转中心。可选地,您可以从整个模型的边界,圆柱的边界以及旋转的角度自己计算它。在这种情况下,你必须做一系列的转换和翻译。 – Pedery 2011-03-20 02:33:21

1

另一种选择是使用四元数生成旋转矩阵。四元数基本上是一个轴和它周围的一个旋转。在这种情况下可能更容易操作?

+0

有趣的概念:) http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation – Pedery 2011-03-20 02:33:42