First off its always nice to post what language you're using. Ill answer using DBP methods as I've never used DBC before.
Every single DBP app (unless specified otherwise) always has a camera to begin with right when a single 3D command is called (loading an object, creating one, etc.). This camera's number is 0.
Making cameras will only create cameras and positio them at 0,0,0, if you want a camera to follow an object around you have to program this yourself (or use the set camera to follow object command, but Ill explain using something else as you wont learn much from that.)
So, we've established that there is no need to create another camera as there is automatically one in the game labelled 0. First, lets setup a basic program with a camera looking at an object from above/behind:
SYNC ON:SYNC RATE 0:AUTOCAM OFF
MAKE OBJECT CUBE 1,10
POSITION CAMERA 0,200,-300
POINT CAMERA 0,0,0
DO
SYNC
LOOP
Hopefully you understand what the code other than the camera commands is doing. The first camera command we call is the AUTOCAM OFF command. When AUTOCAM is ON, the camera will automatically be positioned/pointed at the next loaded object, which isnt what we want in most cases (we'd rather have complete control and position it ourselves.)
The next two commands are POSITION CAMERA and POINT CAMERA. These are fairly straight forward; the POSITION CAMERA command positions the camera at the x,y,z coordinates you provide, in this case 0,200,-300. The POINT CAMERA command points the camera to the 3D position you specify, in this case 0,0,0. Simple?
You may have also noticed that we dont specify a camera number. This isnt needed since the camera we're working with is the default camera, 0. If we were working with camera 1, 2, 3, etc. we'd have to use the camera's respective numbr however.
Ok, now lets add some movement to the cube and a basic cam positioning system. First the code, then the explanation;
SYNC ON:SYNC RATE 0:AUTOCAM OFF
MAKE OBJECT CUBE 1,10
MAKE MATRIX 1,1000,1000,10,10
DO
IF UPKEY()=1 THEN MOVE OBJECT 1,1
IF DOWNKEY()=1 THEN MOVE OBJECT 1,-1
IF LEFTKEY()=1 THEN TURN OBJECT LEFT 1,1
IF RIGHTKEY()=1 THEN TURN OBJECT RIGHT 1,1
X# = OBJECT POSITION X(1)
Y# = OBJECT POSITION Y(1)
Z# = OBJECT POSITION Z(1)
POSITION CAMERA X#,Y#+20,Z#-60
POINT CAMERA X#,Y#,Z#
SYNC
LOOP
Ok, first off run that and see what it's like. (You'll notice I added in a matrix there so you can see yourself moving around).
You'll also see that I removed the original camera positioning/pointing code. That is because it was redundant since we'll be overriding that code by positioning it elsewhere inside the loop.
Alright, first off we have the basic movement code (I hope you understand how this works). Then, we store the object's current X,Y,Z positions into variables X#,Y#,Z# respectively. The reason we do this is mainly because it's easier/faster to type X#,Y#,Z# instead of OBJECT POSITION X,Y,Z all the time, though there is a tiny amount of speed-up in terms of game speed as well.
Next, we position the camera at X#,Y#+20,Z#-60. If you think about what this is doing it translates into positioning the camera at the object's x position, the object's y position plus 20 (so a bit higher than the object), and the object's z position minus 60 (so a bit behind the object).
Finally, we point the camera to the object's current X,Y,Z position so we can see the object.
You'll probably have realized right when you ran the code that this isnt working too well, as the camera isnt turning with the object, instead it's just staying behind it in one direction. Im pretty sure this isnt what you wanted, so Ill explain how to fix this using 3 methods. First using limbs, Second using a "generic" method, and Third using maths.
Using Limbs
I guess I should start by explaining what limbs are. Limbs are basically objects (they're not but we can think of them as objects for the time being
) that take on the position/rotation of another object. Load this code into DBP to see what I mean (the two spheres are limbs of the box object).
SYNC ON:SYNC RATE 0:AUTOCAM OFF
MAKE OBJECT CUBE 1,10
MAKE OBJECT SPHERE 2,5
MAKE MESH FROM OBJECT 1,2
ADD LIMB 1,1,1
OFFSET LIMB 1,1,20,0,0
ADD LIMB 1,2,1
OFFSET LIMB 1,2,-20,0,0
DELETE OBJECT 2
DELETE MESH 1
MAKE MATRIX 1,1000,1000,10,10
POSITION CAMERA 0,200,-300
POINT CAMERA 0,0,0
DO
IF UPKEY()=1 THEN MOVE OBJECT 1,1
IF DOWNKEY()=1 THEN MOVE OBJECT 1,-1
IF LEFTKEY()=1 THEN TURN OBJECT LEFT 1,1
IF RIGHTKEY()=1 THEN TURN OBJECT RIGHT 1,1
SYNC
LOOP
You'll see that as you move around, the two spheres follow the cube perfectly, as if they were part of the object. And this is in essence what limbs really are, extensions of the object.
So, how do limbs work?
A limb requires three things; a parent object number (in the example above, the parent object was 1), a limb number (just like an object number, a limb number identifies the limb, and finally a mesh number (a mesh is used to tell the limb what to look like.
Before we make the limb, we need the parent object. We accomplished this by creating the box, as it was our parent object in the example above.
We also need the mesh. To make a mesh, you use - you guessed it - the MAKE MESH commands. However it's not just MAKE MESH, its MAKE MESH FROM OBJECT. What this command does is creates a mesh (as mentioned before a mesh is the data that tells a limb what to look like) from the object specified; in the example above we created a mesh from the sphere object. But guess what, mesh's require ID numbers as well! So we numbered it 1. So, all together, MAKE MESH FROM OBJECT 1,2 creates a mesh numbered 1, from object 2. Its important not to confuse the end numbers as a lot of people tend to switch them accidentaly and make the mesh's number 2, from object 1.
Now that we have the parent object and mesh, we're able to add the limb. As you can see we use the ADD LIMB command. This command (as mentioned before) requires the parent object number, the limb number, and the mesh number, respectively. So as you can see in the above example we used ADD LIMB 1,1,1, which adds a limb to object 1, with an ID number of 1, using mesh 1.
The next limb command used is the OFFSET LIMB command. This is fairly straight forward, think of this like the limb's version of POSITION OBJECT. Basically the first value specified the parent object, the second specifies the limb number, the third specifies how far left or right of the object the limb should be positioned, the fourth specifies how far high or low from the object the limb should be positioned, and the fifth specifies how close or far from the object it should be positioned.
Finally, we delete the MESH and the object used to create the mesh, since we dont need them anymore.
Alright, now that you *hopefully* understand limbs, lets move onto using them for camera positioning. First lets think of what we want to do;
We want to position the camera behind the object at all times, above the object a tiny bit, and point the camera to the object's position.
Can you see the connection?
We can add a limb to the main object, position the limb behind the object and a bit above the object, and then use the limb's X,Y,Z position to position our camera!
I dont think I need to explain this now that you understand limbs and the basic cam commands, so here's the code;
SYNC ON:SYNC RATE 0:AUTOCAM OFF
MAKE OBJECT CUBE 1,10
MAKE OBJECT SPHERE 2,5
MAKE MESH FROM OBJECT 1,2
ADD LIMB 1,1,1
OFFSET LIMB 1,1,0,20,-60
DELETE OBJECT 2
DELETE MESH 1
MAKE MATRIX 1,1000,1000,10,10
DO
IF UPKEY()=1 THEN MOVE OBJECT 1,1
IF DOWNKEY()=1 THEN MOVE OBJECT 1,-1
IF LEFTKEY()=1 THEN TURN OBJECT LEFT 1,1
IF RIGHTKEY()=1 THEN TURN OBJECT RIGHT 1,1
LX# = LIMB POSITION X(1,1)
LY# = LIMB POSITION Y(1,1)
LZ# = LIMB POSITION Z(1,1)
X# = OBJECT POSITION X(1)
Y# = OBJECT POSITION Y(1)
Z# = OBJECT POSITION Z(1)
POSITION CAMERA LX#,LY#,LZ#
POINT CAMERA X#,Y#,Z#
SYNC
LOOP
Using the "Generic" Method
Thsi method is a lot easier than using limbs, but I just thought Id show you the limbs in the hopes that you might see another use for limbs. Ok, here's the code, then Ill explain:
SYNC ON:SYNC RATE 0:AUTOCAM OFF
MAKE OBJECT CUBE 1,10
MAKE MATRIX 1,1000,1000,10,10
DO
IF UPKEY()=1 THEN MOVE OBJECT 1,1
IF DOWNKEY()=1 THEN MOVE OBJECT 1,-1
IF LEFTKEY()=1 THEN TURN OBJECT LEFT 1,1
IF RIGHTKEY()=1 THEN TURN OBJECT RIGHT 1,1
X# = OBJECT POSITION X(1)
Y# = OBJECT POSITION Y(1)
Z# = OBJECT POSITION Z(1)
XA# = OBJECT ANGLE X(1)
YA# = OBJECT ANGLE Y(1)
ZA# = OBJECT ANGLE Z(1)
POSITION CAMERA X#,Y#,Z#
ROTATE CAMERA XA#,YA#,ZA#
MOVE CAMERA -60
POSITION CAMERA CAMERA POSITION X(),CAMERA POSITION Y()+20,CAMERA POSITION Z()
SYNC
LOOP
Ok this is extremely simple. First off we handle the object movement, then we store the object's positions and angles on the X,Y, and Z axis. With me so far?
Then we position the camera at the object's position, rotate the camera to the object's angles, move the camera backwards 10 units, and upwards 20.
What have we accomplished? Well by rotating the camera to the object's angles, the camera will always face the same direction as the object. By positioning the camera at the object's position and then moving it back/up, it will always stay behind the object as well, since the camera is facing the same direction as the object.
Ok, final method;
Using Mathematics
Alright, this last method, I dont claim to be a pro with
at all. To tell you the truth, I dont even know how/why it works, but Im sure one of the math gurus around here can help you out. However, here's some basic code using maths for you to play with;
<EDIT>
Well... I cant seem to get the maths working right now
, perhaps someone else will show you the method using maths but either way the above two methods are still widely accepted.
Hope it's been of some help,
- RUC'