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 M1 and vector p1) and the position and orientation of Object 2 in Object 1's space (as a rotational matrix matrix M2 and vector p2). That is, M2 and p2 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 p2world = M1p2 + p1,
so with a little algebra we get:
p2world = M1p2 + p1
p2world - p1= M1 p2
M1-1(p2world - p1) = M1-1 M1 p2
M1-1(p2world - p1) = p2
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 = MT.
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