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

5.2.7 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 `SetupFrame()' as follows:

 
void Simple::SetupFrame ()
{
  // 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_RIGHT))
    c->GetTransform ().RotateThis (VEC_ROT_RIGHT, speed);
  if (kbd->GetKeyState (CSKEY_LEFT))
    c->GetTransform ().RotateThis (VEC_ROT_LEFT, speed);
  if (kbd->GetKeyState (CSKEY_PGUP))
    c->GetTransform ().RotateThis (VEC_TILT_UP, speed);
  if (kbd->GetKeyState (CSKEY_PGDN))
    c->GetTransform ().RotateThis (VEC_TILT_DOWN, speed);
  if (kbd->GetKeyState (CSKEY_UP))
    c->Move (VEC_FORWARD * 4 * speed);
  if (kbd->GetKeyState (CSKEY_DOWN))
    c->Move (VEC_BACKWARD * 4 * speed);
  ...
}

That's all! With this simple 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 RotateThis() which expects a vector to rotate along and an angle given in radians (the speed parameter). There are a number of predefined vectors which you can use. A couple of them are used in this example.

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

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



This document was generated using texi2html