1.8 Freelook with the mouse

By Ryan Murphy, ramurphy100@hotmail.com.

Freelook with the mouse in Crystal Space is not exceedingly difficult. In your program where you have your keyboard handling routine, just add the following below the keyboard section.

// Process Keyboard Events
...
// Process Mouse Events
int mid_x = g2d->GetWidth () / 2;
int mid_y = g2d->GetHeight () / 2;

// Mouse moved horizontally?
if (mouse->GetLastX () > mid_x)
  cam->GetTransform ().RotateThis (CS_VEC_ROT_RIGHT, speed * (mouse->GetLastX () - mid_x));
if (mouse->GetLastX () < mid_x)
  cam->GetTransform ().RotateThis (CS_VEC_ROT_LEFT, speed * (mid_x - mouse->GetLastX ()));

// Mouse moved vertically?
if (mouse->GetLastY () > mid_y)
  cam->GetTransform ().RotateThis (CS_VEC_TILT_DOWN, speed * (mouse->GetLastY () - mid_y));
if (mouse->GetLastY () < mid_y)
  cam->GetTransform ().RotateThis (CS_VEC_TILT_UP, speed * (mid_y - mouse->GetLastY ()));

// Put the mouse back in the middle
g2d->SetMousePosition (mid_x, mid_y);
...

NOTE: You need to have previously got the camera pointer by using:

cam = view->GetCamera();

This code will let the mouse rotate the camera in all directions.