rage game engine free game engine

Tutorial 4 - Simple Free Look Camera

Sulaco Home
/ News / About Rage 3D / Current Features / Download Rage 3D / License / News Archive / Our Team / Rage 3D Functions / Rage 3D Games / Screen Shots / Tutorials /

Google
 
Web www.rage-game-engine.za.net



Hosting provided by Sulaco New Media Bulkmail and Email Marketing

 Ads


Tutorial 4 - Simple Free Look Camera


In this tutorial we add a movable first person shooter camera the scene.

This tutorial is a continuation on the previous tutorial called "Adding a scybox to Rage 3D"

To set up the camera we need to also add a few other piecesof code.
Firstly, we need to set up our Mouse even callback functions.
To do this we use:

rglSetSceneMouseCallbacks(@SceneOnMouseMove, @SceneOnMouseDown, @SceneOnMouseUp);

The tree functions that we pass in look like this:

procedure SceneOnMouseDown(aButton: Integer; aXpos, aYpos, aLastXPos, aLastYPos : Single); stdcall;
begin
// Handle the mouse button down event
end;


This function is used to trap mouse button down events

procedure SceneOnMouseUp(aButton: Integer);
begin
// Handle the mouse button up event
end;


This function is used to trap mouse button up events

Both of the callbacks pass back the button that was pressed or released as an integer between 0 and 2(left middle and right).

In the case of the mouse down callback the current mouse position and the previous mouse position is also returned in screen coordinates.

procedure SceneOnMouseMove(aXpos, aYpos, aLastXPos, aLastYPos : Single); stdcall;
begin
// Do any mouse movement code here
end;


This callback is fired whenever the mouse is moved.
It returns the current mouse position and the previous mouse position in screen coordinates.
In our case we will use these callbacks to control our camera look direction.
To do this we add the code below to the SceneOnMouseMove callback.

{Use the built in mouse look function}
rglMouseLookCamera(Round(aXpos), Round(aYpos));
{Set the cursor's position back to the center of the screen}
rglSetSceneMousePositionToCenter;


Fist we need to tell rage to do a mouse look using the current x and y position of the mouse and then we set the mouse position back to the middle of the screen (Rendering area) so that we dont have the cursor running off the edge of the window, causing strange glitches in the mouse look.

Now we are ready to set up our camera movement.

We do this by using the keyboard callback events.
Firstly we need to Create our callbacks, and attach them to the Rage engine.
We create our callbacks like this:

procedure SceneOnKeyDown( aKey : PChar); stdcall;
var
lSpeed : Single;
begin
//Add Key Down code here
end;

procedure SceneOnKeyUp(aKey : PChar); stdcall;
begin
//Add Key Up code here
end;


The SceneOnKeyDown is fired when a key is pressed on the keyboard.
The SceneOnKeyUp event is fired when we release the pressed key.

Now that we have defined the callbacks, we can attach them to the rage 3d game engine.
We call rglSetSceneKeyCallbacks to do this.
Below is an example:
{Set up the key callbacks so that we can trap keystoke events}
rglSetSceneKeyCallbacks(@SceneOnKeyDown, @SceneOnKeyUp);


Next we need to tell rage what keys it needs to monitor.
We do this using the rglRegisterSceneKey.

rglRegisterSceneKey was introduced into the engine because event callbacks from a dll can become quite expensive, and this is a good way of minimizing that cost.

In this tutorial we want to receive callbacks got the W,A,S and D keys so we regisater them like this:

rglRegisterSceneKey(Ord('W')); // Forward
rglRegisterSceneKey(Ord('S')); // Backwards
rglRegisterSceneKey(Ord('A')); // Strafe Left
rglRegisterSceneKey(Ord('D')); // Strafe Right


Now all thats left is to add the key down code to manage the camera movement.

We add the code below to the SceneOnKeyDown callback event

lSpeed := 1;
if (aKey = 'W') then
rglMoveCamera(lSpeed);

if (aKey = 'S') then
rglMoveCamera(-lSpeed);

if (aKey = 'A') then
rglStrafeCamera(lSpeed);

if (aKey = 'D') then
rglStrafeCamera(-lSpeed);


rglMoveCamera is used to move the cameral forward or backwards in the direction the camera is looking at.

To move forward we pass in a positive value, and backwards, we use a negative value.

rglStrafeCamera is used to move the camera at a 90 degree angle from the current look direction.

To strafe the camera left we pass a positive value to rglStrafeCamera, and a positive value to strafe the camera right.

You will note that I set the lSpeed variable to 1.
In a game you would bind (using math) lSpeed to the Frames per second, so that you have smooth mouse movement in your game.

And thats it!! We now have a classic style Fist person shooter camera.

You will note that the camera functions also allow you to create other camera styles including follow camera, thirs person view's and many others.

For a full list of camera functions, you can have a look at the Camera Functions for RageGL

Download the code

Please note: In order to use and compile any of these tutotrials, you will need to download the Binary and Art for the engine



 Ads




SourceForge.net Logo