If you haven't already, finish part I of this assignment. This assignment contains a solution to part I, so you should be sure to finish that assignment before starting this one!
First, download Translate2.zip, and unzip it. Open up the solution Translate, compile, and run. You should see 3 cubes orbiting each other, and a red crosshair, as follows:
You can move and the crosshair around using the arrow keys (left/right and up/down) and the keys a and s (forward and backward) and the keys q, w, and e (yaw / pitch / roll)
. If you press the 1, 2, or 3 keys, then the world position and orientation of the crosshair will be sent to the corresponding object, calling the methods "setPositionFromWorldPostion" and "setOrientationFromWorldOrentation". Right now, the 1, 2, and 3 keys do nothing, since those methods have yet to be written!Let's assume we have two objects, Object 1 and Object 2. We have the position and orientation of Object 1 in world space (as a rotational matrix M_{1} and vector p_{1}) and the position and orientation of Object 2 in Object 1's space (as a rotational matrix matrix M_{2} and vector p_{2}). That is, M_{2} and p_{2} are relative to the position and orientation of Object 1.
When it is time to render Object 2 to the screen, we will need its position and orientation in world space. How can we get the position and orientation of Object 2 in world space? By using the position and orientation of Object 1 as follows (assuming column major matrices and column vectors):
what if we have the position and rotation of Object 2 in world space, and we want the position and rotation
of Object 2 in local space (that is, relative to Object 1)? Well, from above we have we have p_{2world} = M_{1}p_{2} + p_{1},
so with a little algebra we get:
p_{2world} = M_{1}p_{2} + p_{1}
p_{2world} - p_{1}= M_{1} p_{2}
M_{1}^{-1}(p_{2world} - p_{1}) = M_{1}^{-1} M_{1} p_{2 }
M_{1}^{-1}(p_{2world} - p_{1}) = p_{2 }
where M^{-1} is the inverse of the matrix M. As it turns out, finding the inverse of a rotational matrix is very easy: the inverse of a rotational matrix is just its transpose. That is, for a rotational matrix M, M^{-1} = M^{T}.
We can get the transpose of an Ogre::Matrix3 using the transpose method:Ogre::Matrix3 rotationalMatrix; // Code to create rotational matrix ... Ogre::Matrix3 inverse = rotationalMatrix.Transpose()
Of course, we can use similar logic to go from the world space back to local space as well ...
Ready to get to work? Here we go!
void setPositionFromWorldPosition(Ogre::Vector3 worldPosition)Modifies the local position of the object so that it has the correct world position
void setOrentationFromWorldOrientation(Ogre::Matrix3 worldOrientation)Modifies the local orientation of the object so that it has the correct world orientation