GreenFox,
Below is a code snippet in where I have used the
cos and
sin maths to make the camera follow behind the object. Also, I have used the point camera command to make the camera always look at the object. In the program below, which is fully executable, the user is enabled to use the arrow keys to rotate and move the cube character, and also use keys
w and
a to increase/decrease the distance between the cube character and the camera.
Using sin and cos, there is not much I can explain, the reason being that I, myself, never learned such maths in school. However, I have put forth much experimentation in the past to learn how to gain some useful control over the commands. These maths are essentially how the [/i]new position
(newpositionx();newpositionz(),newpositiony() commands function. In the case of my lack of knowledge, you will just need to study over the source code yourself, and run some of your own experiments.
set display mode 800,600,32
sync on
sync rate 60
REM << make character
make object cube 1,10
REM << make ground
make object plain 2,300,300
xrotate object 2,90
position object 2,0,-5,0
REM << initialize distance# variable
distance# = 20
ink rgb(150,0,0),0
REM <<<<<<<<<< MAIN LOOP >>>>>>>>>>>
repeat
REM << rotate the cube
if rightkey() = 1 then y_angle# = wrapvalue(y_angle# + 2)
if leftkey() = 1 then y_angle# = wrapvalue(y_angle# - 2)
REM << foward;backward movement
if upkey() = 1
inc x#,sin(y_angle#) * 2
inc z#,cos(y_angle#) * 2
endif
if downkey() = 1
inc x#,sin(wrapvalue(y_angle# + 180)) * 2
inc z#,cos(wrapvalue(y_angle# + 180)) * 2
endif
REM << increase/decrease camera distance from cube
if scancode() = 31 then inc distance#,0.50
if scancode() = 17 then dec distance#,0.50
REM << update object position
position object 1,x#,0,z#
yrotate object 1,y_angle#
REM << place camera behind the cube, even during rotation
position camera x# + (sin(wrapvalue(y_angle# + 180)) * distance#),15,z# + (cos(wrapvalue(y_angle# + 180)) * distance#)
REM << point camera at very center of cube
point camera x#,2,z#
REM << draw statistics to screen
text 5,5,"Cube X Pos = " + str$(object position x(1)) + ": Cube Z Pos = " + str$(object position z(1)) + ": Cube Y Rot = " + str$(y_angle#)
text 5,20,"Cam X Pos = " + str$(camera position x()) + ": Cam Z Pos = " + str$(camera position z()) + ": Cam Distance from Cube = " + str$(distance#) + " Units"
center text 400,575,"Use the letters 'W' and 'S' to increase or decrease camera distance from cube"
sync
until mouseclick() = 2
end

+NanoBrain+