4

I have a question concerning model rotation in XNA. The question is - what should I do (which values should be changed and how) to rotate green model in that way (red arrows):

http://img843.imageshack.us/i/question1.jpg/

Code used to draw :

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();
        }

    }

So I tried to apply your solution by changing my code slightly:

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)); 

By changing rotZ parameter indeed I was able to rotate the model. However the effect is not what I wanted to achieve http://img225.imageshack.us/i/questionau.jpg/, it changed its position. Is it because of a faulty model or some other mistake? I want the "cylinder" to remain in its position. Do you know how can I do this?

1
  • Have you considered to upvote your answers or mark one as accepted? I see you're new here so remember the upvotes is what drives this site... Commented Mar 24, 2011 at 23:13

2 Answers 2

1

Rotation with rotation matrices are cummulative. So you can probably calculate your rotation matrix by rotating the model 45 degrees "down", then apply your rotation around your wanted axis, then rotating the model 45 degrees "up" again. The product of these three matrices should give you your desired matrix.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried your solution and updated my first post. Thank you very much for your feedback as it moved me a bit closer to expected result.
Your shift comes from the pivot center of your model. Naturally, your code cannot "guess" that you just happen to want to rotate your composite model around the axis of the cylinder, and not the entire bounds. I'm no XNA expert so I don't know how you can set the rotation center of the model. Optionally you could calculate it yourself from the bounds of the entire model, the bounds of just the cylinder, and the rotated angle. In this case you'd have to do a series of transformations and translations.
1

Another option is to use a Quaternion to generation the rotation matrix. A Quaternion is basicly an axis and a rotaion around it. Which may be easier to manipulate in this case?

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.