Istiaq Ahmed Istiaq Ahmed - 1 year ago 78
C# Question

XNA - Rotate around X and Z axis 3D

I'm new to 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.
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 i.e. rotation around Y axis by rotating the facing vector around Y axis by R radians.
If i were to do the rotaion now around X axis, for example, what would be the approach? 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. But i suppose for X and z, i have to manipulate position as well? Please show me some guidance.

Answer Source

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:

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;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download