Rotation and Translation
Practical Exercises

Download Project

First, download, and unzip it. Open up the solution Translate, compile, and run. You should see 3 static squares, as follows:


Not very exciting, huh? Well, this is supposed to have the blocks orbiting each other. However, the code is not quite correct yet. For this in-class assignment, we will fix the code so that the blocks orbit each other properly.

Moveable Objects

This project is built around the MovingObject Class (NOTE: The MovingObject class is for demonstration purposes only! MovingObject recreates functionality already in Ogre, so you can see how the math works. Do not use this MovingObject class in your project code!)

MovingObject data members:

MovingObject Methods

MovingObjects contain the following methods that you do not need to modify

Finally, MovingObjects contain the following methods that you need to modify

Row Vectors vs Column Vectors

Assuming we are using row vectors, then we would rotate a vector v around the z axis using a rotational matrix M as follows:

x y z
cos Θ sin Θ 0
-sin Θ cos Θ 0
0 0 1

or, vM. If, However, we are using column vectors, we would rotate the vector v around the z axis as follows:

cos Θ -sin Θ 0
sin Θ cos Θ 0
0 0 1

or Mv. If we wanted to rotate a vector v first by matrix M1, then by M2, then by M3 using row vectors, we would do: vM1M2M3. If, however, we are using column vectors, we would do: M3M2M1v. Ogre3D uses column vectors (and not row vectors!)

Matrices in Ogre3D

If we wanted to create the following matrix to rotate a column vector by theta degrees around the z axis:

cos Θ -sin Θ 0
sin Θ cos Θ 0
0 0 1

We could do it in several ways. We could create the matrix directly:

    Ogre::Radian theta = Ogre::Radian(Ogre::Math::PI / 4);

    Ogre::Matrix3 rotate(Ogre::Math::Cos(theta), -Ogre::Math::Sin(theta), 0,
                         Ogre::Math::Sin(theta), Ogre::Math::Cos(theta),  0,
                         0,                      0,                       1);

Or, we could create the matrix from transformed x/y/z basis vectors, using the FromAxes method. Note that since Ogre uses column vectors, the basis vectors are columns of the created matrix:

    Ogre::Radian theta = Ogre::Radian(Ogre::Math::PI / 4);

    Ogre::Vector3 xBasis = Ogre::Vector3(Ogre::Math::Cos(theta),  Ogre::Math::Sin(theta), 0);
    Ogre::Vector3 yBasis = Ogre::Vector3(-Ogre::Math::Sin(theta), Ogre::Math::Cos(theta), 0);
    Ogre::Vector3 zBasis = Ogre::Vector3(0,                       0,                      1);
    Ogre::Matrix3 rotate;
    rotate.FromAxes(xBasis, yBasis, zBasis);

Now, if we wanted to use this rotation matrix to rotate a vector v, we would do it as follows:

    Ogre::Vector3 v;
    Ogre::Matrix3 rotate;
    // set value for v, create rotation matrix rotate as above
    v = rotate * v;

What to do

Ready to get to work? Here we go!

Supporing files