Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Set Camera To Follow Tutorial

Author
Message
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 8th Feb 2008 02:49 Edited at: 24th Feb 2008 07:49
The Secrets Of Set Camera To Follow

All 3D games which have a third person camera view need the camera to track the player's character. The game isn't much use if the character moves off into the distance leaving the camera just sitting there...

So, how do you make the camera follow the player?

Well, there are two ways. One is to use maths and calculate the position of the camera in 3D space based on the current player's position.

This isn't that difficult as the Dark Basic functions Object Position X(), Object Position Y() and Object Position Z() will instantly tell us where the player is whereas Camera Position X(), Camera Position Y() and Camera Position Z() give us the same information but for the camera.

Given this information, and some maths calculations we can calculate the required new position and angles for the camera.

But, all the maths involved isn't necessary at all as there is a Dark Basic command called Set Camera To Follow which does everything for you. It has a bewildering list of parameters you have to use to make it work and DB's built-in help files don't really do a good job in explaining how it all works. As such many users don't use it.

Hopefully, this tutorial will clear things up a bit.


Syntax:

The Set Camera To Follow command is used in the following way:

Set Camera To Follow X#, Y#, Z#, Angle#, Camdist#, Camheight#, Camsmooth#, ColFlag

(All parameters are float values apart from ColFlag which is integer and can be either a 0 or a 1).

X, Y & Z

These are set to the X, Y & Z co-ordinates of the object you want the camera to follow. You can use the float variables you use to position the object you are tracking, or if you use the turn/rotate commands and then Move Object instead, Object Position X(), Object Position Y() and Object Position Z() can be used.

Angle

This is the angle that you want the camera to be at as it tracks the object and is normally set to the same angle as the object being tracked. The camera then stays behind the object.

If you want the camera to track the object from the side similar to the way a TV camera tracks horses in a horserace, you need to add 90 or 270 to the object's angle - depending on the side of the tracked object you want to be. Adding 180 will position the camera in front of the object moving backwards.

Camdist

This is the number of 3D units that will be between the object being tracked and the camera.

Camheight

This is the one parameter that many newcomers misunderstand and as such makes it look like the command doesn't work properly. Many assume it is the height above the object being tracked, whereas in reality it is actually the absolute height of the camera in the 3D world. You therefore have to set this to the object's height plus the required additional height of the camera.

Camsmooth

This parameter sets the speed that the camera locks onto the specified tracking position. The slower this happens, the smoother the effect. The value range can be from 1 where there is no smoothing to 100 which is a lot of smoothing. For a normal smoothing effect, a value of around 10-15 is a good place to start.

ColFlag

This flag turns camera collision on. Setting it to 1 will prevent the camera from entering areas defined as static collision boxes. Setting it to 0 turns collision off.

It's important to note however that this collision has no effect with matrices - the camera will still pass through them if the Camheight parameter isn't set high enough.



A brief description of the lines which make up the main Do..Loop:

We use MouseMoveX() to store the left/right mouse movement in MMx and add that to object 1's Y angle. This steers the cube.

Next we move the cube and grab it's new X and Z position in order to use Get Ground Height() which tells us the cube's height. We add 1.0 to this value when re-positioning the object. This ensures that the cube always stays 1.0 3D units above the matrix.

Finally, we add the required height to whatever height object 1 is currently positioned. This means that as the cube moves up and down (over hills for example), the camera always stays a fixed distance above the object.

Example Code For DBPro:

As DBPro is considerably faster than DBC, the previous code snippet runs too fast in DBPro. So, here's the same code snippet but adapted to run better in DBPro. To kill two birds with one stone, it also uses the keyboard to steer the cube instead of the mouse, so if you need this in a DBC program, just alter the first four lines of the main loop accordingly (altering the rotate angle and move speed to suit).




Useful Pointers When Using Set Camera To Follow

As Set Camera To Follow has a smoothing parameter, if it is used, it is not a 'call once' command. For anything to happen, it has to be called continuously until the camera eventually reaches it's destination.

If the object being tracked is moving, this may never happen until it comes to a halt. When this happens, the camera will slowly come to a halt in the required position - depending on the value of the CamSmooth value.

This command can therefore be used with non-moving objects to provide smooth transitions between different views.

For example, in the case of a boxing ring where the camera is showing a full front view. You might want to switch to a side view and rather than instantly switch, have the camera swing smoothly around to the new position. With Set Camera To Follow, this can be done by simply altering the angle value.as the following simple example shows:



That's it for now. I'll add further uses for Set Camera To Follow along with examples if and when I think of them.

TDK_Man

Not_Maindric
16
Years of Service
User Offline
Joined: 10th Jul 2007
Location: Omaha, NE
Posted: 8th Feb 2008 03:17
This is a pretty good tut. I read the first 3 or 4 paragraphs, and I almost understand the first way you explain. I am not able to test seeing as I have to go to bed soon, but I will tomarrow. Looking forward to testing it!

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 8th Feb 2008 21:11 Edited at: 8th Feb 2008 21:12
@ TDK

I can set the camsmooth as high as I want, and it doesn`t throw an error... It only stops moving when I set it above 10000.

Oooooops!!! I accidentally formated drive c.
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 9th Feb 2008 00:31
The max value of 100 I used is as stated in the DBC help files. I've never needed anywhere near as much smoothing as that before so I've never tried larger values!

I think 20 is the largest I've ever tried...

TDK_Man

pcRaider
16
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 1st Jun 2008 18:15
Set Camera To Follow

A DBP command includes a bug.
ColFlag does not work.
It is broken.

I want to think about a substitute method.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 1st Jun 2008 19:12
@pcraider
make a hidden object that sticks to the camera, then detect collisions with that

It is far better to complete a 10 line program than to start a 10,000 line program.
pcRaider
16
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 1st Jun 2008 20:47
Ok, OBese87.
it's work.

TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 1st Jun 2008 22:29
Quote: "A DBP command includes a bug."


Are you trying it with a static collision box - it's the only type that works (apparently).

TDK_Man

pcRaider
16
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 2nd Jun 2008 06:09
Quote: "Are you trying it with a static collision box - it's the only type that works (apparently)."


not work for DBP.

test code.

Login to post a reply

Server time is: 2024-04-19 17:31:39
Your offset time is: 2024-04-19 17:31:39