XNA rotation

I have a question about model rotation in XNA. Question: what should I do (what values โ€‹โ€‹should I change and how) to rotate the green model this way (red arrows):

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

Code used for drawing:

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 slightly modifying my code:

 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 the rotZ parameter, 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 due to a faulty model or some other error? I want the "cylinder" to remain in its position. Do you know how I can do this?

+4
source share
2 answers

Rotation with rotation matrices is cumulative. Therefore, you can calculate your rotation matrix by rotating the model 45 degrees down, then apply your rotation around the desired axis, and then rotate the model again 45 degrees up. The product of these three matrices should give you the desired matrix.

+1
source

Another option is to use a quaternion to generate a rotation matrix. Quaternion is basically the axis and grand piano around it. What could be easier to manipulate in this case?

+1
source

Source: https://habr.com/ru/post/1344208/


All Articles