0
votes

I am trying to rotate an object around a global y-axis (basically yaw around 0,1,0 no matter what the current orientation is). I'd show a picture, but reputation... I want to rotate yaw around a global axis instead of the axis local to the object. For example if I slightly tilt a box via pitch, then try to rotate yaw, it will rotate the box at the tilted angle instead of straight as if the box hadn't been tilted at all.

Each object stores its position and rotation in a matrix that is updated every update call. The code for rotation looks like so:

D3DXMatrixRotationYawPitchRoll(&rotation, yaw, pitch, roll);
D3DXMatrixMultiply(&position, &rotation, &position);

where position is the outstanding matrix that is translated and rotated every update. This is what is rotating it locally, but I want to rotate yaw via a global axis.

How can I write it so that yaw rotation is completed as if the local up vector was (0,1,0) even if tilted?

2

2 Answers

0
votes

In the following code there are two rotation matrices, A and B. One is a rotation around the local axis by worldYaw and the other is a rotation around the global axis by worldYaw.

D3DXMatrixRotationYawPitchRoll(&rotation, yaw, pitch, roll);
D3DXMatrixRotationYawPitchRoll(&worldRotation, worldYaw, 0, 0);
D3DXMatrixMultiply(&A, &rotation, &worldRotation);
D3DXMatrixMultiply(&B, &worldRotation, &rotation);

I can never keep it in my head if D3D uses row or column vectors, so I can't say if A or B is what you're looking for :)

0
votes

Fixed the issue. Decided to store the old values from the previous frame in order to rebuild the position matrix.

I now build a matrix off of the current frame's changes, complete with translation and rotation, and then add those values onto the total values before creating the final matrix.

Works like a charm!