Time for action – rotating the cube
1. Add the following declaration to the
Fields
region of theCube
class:private float rotation = 0f; private float zrotation = 0f;
2. Add an
Updat
e()
method to theCube
class as follows:#region Update public void Update(GameTime gameTime) { rotation = MathHelper.WrapAngle(rotation + 0.05f); zrotation = MathHelper.WrapAngle(zrotation + 0.025f); } #endregion
3. In the
Draw()
method of theCube
class, add two new items to the list of the generated matrices:Matrix rot = Matrix.CreateRotationY(rotation); Matrix zrot = Matrix.CreateRotationZ(zrotation);
4. Still in the
Draw()
method, replace the current line that setseffect.World
with the following:effect.World = center * rot * zrot * scale * translate;
5. In the
Update()
method of theCubeChaserGame
class, add the following line right before the existing call tobase.Update(gameTime)
:cube.Update(gameTime);
6. Execute your game and turn to face the cube:
What just happened?
Just like translation and scaling...