components used: 3D State

this tutorial will not work with borland kylix

Part 1Part 2 download source code

Now that we've got our scene being displayed on the form, we want to be able to manipulate the camera so that we can explore the world a little. To do this, we're going to first add a couple of lines to our FormShow procedure. Just before the 'camera := STATE_camera_get_default_camera;' line, you need to enter the following:

STATE_camera_get_location(STATE_camera_get_default_camera(), CamLoc[0], CamLoc[1], CamLoc[2]);

What we're doing here is retrieving the current location of our default camera in the world, and placing the X, Y, and Z coordinates of this location into an array called CamLoc. Just like every other variable, we need to declare the array (which can hold three values), and we'll do that by putting the following line in our private declarations above or below the camera variable:

CamLoc: array[0..2] of Double;

Now that we have these coordinates stored, we can now increase or reduce them to move the camera around. We'll set up an OnKeyDown event that looks like the following to allow us to move the camera left, right, forwards and backwards.

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
key of
   vk_up: CamLoc[0] := CamLoc[0] - 5;
   vk_down: CamLoc[0] := CamLoc[0] + 5;
   vk_left: CamLoc[1] := CamLoc[1] - 5;
   vk_right: CamLoc[1] := CamLoc[1] + 5;

  STATE_camera_set_location(STATE_camera_get_default_camera(), CamLoc[0], CamLoc[1], CamLoc[2]);

This code works quite simply. If you've followed our tutorial on using keys in Delphi, then you'll be familiar with the setup of checking the value of key to see which key has been pressed on the keyboard. I suggest you go and look at it now if you don't understand the code in the case statement, but basically what we're doing is checking to see which of the arrow keys has been pressed, and adjusting the X or Y coordinate of the CamLoc array accordingly.

Once we've updated the array, we need to set the location of the camera based on these new coordinates. We do this with 'set_location' which works in the same way as 'get_location' except this time we're giving the camera the coordinates, rather than getting them. Now that we've done that, we refresh the form so that the view of the camera is updated for the user to see. It's as simple as that!

If you look at the source code for this tutorial, you'll see that we've gone a step further and added functions to the 'A', 'D', 'W' and 'S' keys, so that we can look left, right, up and down respectively, as well as move. It should make sense to you based on what we've done already. We only introduce two new STATE_camera functions: 'rotate_z' and 'rotate_y', both of which allow us to rotate the camera on a particular axis.

Okay, that's it for this first tutorial on the 3DState Engine. In the next tutorial we'll show you how to move objects in our world, as well as continue playing with the camera. I hope you've been able to see just how easy the 3DState engine is to use - we've written very little code to get this far, and to be honest it doesn't get much more complicated. Have a look at the many example files that come with the 3DState engine, and read the help file descriptions for each command. You'll quickly discover new things to play with, which should keep you occupied until the next tutorial. If you've any questions, email me at the usual address.

Part 1Part 2 download source code

Subscribe to the feed NEWS Feed
archived news | mailing list

[ all tutorials found at are Copyright © 2008 Ben Watt ]