Rotate But Stay Still

※ Don’t mind the inaccurate illustrations
I have a 3D world with origin (0,0,0), and a camera facing it at some orientation represented by a 3×3 matrix. The center of the camera is (x,y,z). I then apply a rotation matrix to the camera’s orientation matrix so the view (from the user’s point-of-view) has “rotated”.
However, after I apply the rotation, I need to calculate a new center position for the camera such that the origin looks like it is still in the same position relative to the “camera”.
After many ideas and several coding mistakes (including using the z coordinate as the x which obviously lead to very wrong results), the final computation is as follows:
x' = rotation_matrix[0]*x
y' = rotation_matrix[1]*y
z' = rotation_matrix[2]*z;
It looks so annoyingly simple, I can’t believe it took me three weeks to get this to work.
And of course, right after I solved that, I found out that the way I computed the rotation matrix was wrong! I am given an angle and a direction, but I wrongly assumed that the direction would always be along one of the axes. Apparently the rotation direction is based on the current orientation of an object that is in the world.
Thankfully, our project already had code that computes the new orientation matrix given a rotation angle in a certain direction. All I had to do was call that function. (In a previous assignment, though, I had to study and understand it because there were some computation errors. Even though the errors were in the function that computed the angles from the orientation matrix, it also helped me understand the reverse which is what I need now. Here is a helpful document that got me through that task.)
Once I had the new orientation matrix, it was only just a matter of computing the rotation matrix in order to apply what I had already figured out the first time around.
rotation_matrix = new_orientation*inverse(old_orientation)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: