简体   繁体   English

XNA-绕X和Z轴旋转3D

[英]XNA - Rotate around X and Z axis 3D

I'm new to XNA. 我是XNA的新手。 I just got stuck into something - I have to tilt/flip a character in my game that means rotate around X axis and the Z axis (not simultaneously!) I can do the turn - that means rotate around Y axis. 我只是陷入某种困境-我必须在游戏中倾斜/翻转角色,这意味着绕X轴和Z轴旋转(不能同时进行!)我可以转弯-意味着绕Y轴旋转。 I have the rotation matrix, and character's current position vector (also the facing/direction vector that can be obtained from rotation matrix) I implemented the turn ie rotation around Y axis by rotating the facing vector around Y axis by R radians. 我有旋转矩阵,还有角色的当前位置矢量(也可以从旋转矩阵获得的朝向/方向矢量),我实现了旋转,即通过将相对矢量绕Y轴旋转R弧度来绕Y轴旋转。 If i were to do the rotaion now around X axis, for example, what would be the approach? 例如,如果我现在要绕X轴旋转,那该怎么办? For example, change from standing position to a "Superman Flying" position would need the character to tilt... The y rotation didn't involve changing the position of the character, just changed the facing. 例如,从站立位置更改为“超人飞行”位置将需要角色倾斜... y旋转并不涉及更改角色的位置,而只是更改了面板。 But i suppose for X and z, i have to manipulate position as well? 但是我想对于X和Z,我也必须操纵位置吗? Please show me some guidance. 请给我一些指导。

Figured it out! 弄清楚了! Just had to rotate the model around right/forward vectors that are available through the rotation matrix. 只需将模型围绕可通过旋转矩阵获得的右/前向矢量旋转即可。 Found the following blog post very helpful: https://stevehazen.wordpress.com/2010/02/15/matrix-basics-how-to-step-away-from-storing-an-orientation-as-3-angles/#comment-376 发现以下博客文章非常有帮助: https : //stevehazen.wordpress.com/2010/02/15/matrix-basics-how-to-step-away-from-storing-an-orientation-as-3-angles/ #comment-376

As for code, here is a sample: Vector3 rightVector = target.CurrentModelMatrix.Right; var rotMatrix = Matrix.CreateFromAxisAngle(rightVector , (float)GetRadianAngle(10));// Will tilt the character forward around the right vector target.CurrentModelMatrix *= rotMatrix; 至于代码,这是一个示例: Vector3 rightVector = target.CurrentModelMatrix.Right; var rotMatrix = Matrix.CreateFromAxisAngle(rightVector , (float)GetRadianAngle(10));// Will tilt the character forward around the right vector target.CurrentModelMatrix *= rotMatrix; Vector3 rightVector = target.CurrentModelMatrix.Right; var rotMatrix = Matrix.CreateFromAxisAngle(rightVector , (float)GetRadianAngle(10));// Will tilt the character forward around the right vector target.CurrentModelMatrix *= rotMatrix;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM