[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### 4.3.1.5 Locomotion (Moving Around)

Staring at that solid wall gets a bit boring after some time. The problem is that we can't move the camera to change our view point. Let's add some code to do exactly this. Edit ``simple.cpp'` again and change ``ProcessFrame()'` as follows:

    void Simple::ProcessFrame ()
    {
      // First get elapsed time from the virtual clock.
      csTicks elapsed_time = vc->GetElapsedTicks ();

      // Now rotate the camera according to keyboard state
      float speed = (elapsed_time / 1000.0) * (0.03 * 20);

      iCamera* c = view->GetCamera();
      if (kbd->GetKeyState (CSKEY_SHIFT))
      {
        // If the user is holding down shift, the arrow keys will cause
        // the camera to strafe up, down, left or right from it's
        // current position.
        if (kbd->GetKeyState (CSKEY_RIGHT))
          c->Move (CS_VEC_RIGHT * 4 * speed);
        if (kbd->GetKeyState (CSKEY_LEFT))
          c->Move (CS_VEC_LEFT * 4 * speed);
        if (kbd->GetKeyState (CSKEY_UP))
          c->Move (CS_VEC_UP * 4 * speed);
        if (kbd->GetKeyState (CSKEY_DOWN))
          c->Move (CS_VEC_DOWN * 4 * speed);
      }
      else
      {
        // left and right cause the camera to rotate on the global Y
        // axis; page up and page down cause the camera to rotate on the
        // _camera's_ X axis (more on this in a second) and up and down
        // arrows cause the camera to go forwards and backwards.
        if (kbd->GetKeyState (CSKEY_RIGHT))
          rotY += speed;
        if (kbd->GetKeyState (CSKEY_LEFT))
          rotY -= speed;
        if (kbd->GetKeyState (CSKEY_PGUP))
          rotX += speed;
        if (kbd->GetKeyState (CSKEY_PGDN))
          rotX -= speed;
        if (kbd->GetKeyState (CSKEY_UP))
          c->Move (CS_VEC_FORWARD * 4 * speed);
        if (kbd->GetKeyState (CSKEY_DOWN))
          c->Move (CS_VEC_BACKWARD * 4 * speed);
      }

      // We now assign a new rotation transformation to the camera.  You
      // can think of the rotation this way: starting from the zero
      // position, you first rotate "rotY" radians on your Y axis to get
      // the first rotation.  From there you rotate "rotX" radians on the
      // your X axis to get the final rotation.  We multiply the
      // individual rotations on each axis together to get a single
      // rotation matrix.  The rotations are applied in right to left
      // order .
      csMatrix3 rot = csXRotMatrix3 (rotX) * csYRotMatrix3 (rotY);
      csOrthoTransform ot (rot, c->GetTransform().GetOrigin ());
      c->SetTransform (ot);

      ...
    }

With this change, you can rotate the camera with the left and right arrow keys and move forward and backward with the up and down arrow keys. Try it out to see the effect. To rotate the camera we use `SetTransform()` which expects a transformation matrix.

That's it for now. In this tutorial you learned how to set up the Crystal Space system for use, how to create a simple room with some lights, how to respond to events, and how to handle some basic camera operations.

You can now go on to the next tutorial (see section Simple Tutorial 2: Sprites)) to learn how to add a 3D sprite to this application.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ]

This document was generated using texi2html 1.76.