I put some comments into the program, this should help you to understand the code. When you start DB and paste the code into the editor you will get syntax highlighting.
(Lines starting with "`" are comments, lines between "remstart" and "remend" are comments)
remstart
-----------------------------------------------------------------------------
DBpro tutorial program about moving a object
followed by a camera
by Attila 2008
-----------------------------------------------------------------------------
remend
cls : `this will clear the screen
sync on : ` we use manual synchronization
` we create a grid to see the object moving over the grid
` This is not necessary but it will allow to see the movement of the object
` over the grid. Without the grid (or another reference) the object would
` look not to move
make matrix 1,500,500,20,20
remstart
a object has a position (x,y,z) and a rotation
we store these valuses in the variables x#,y#,z# and r# (for rotation)
^ z-axis
! /
! /
_____!/R#!____ x-axis
/!
/ !
v
y-axis
in this program we control only the objects position
the camera follows the object automatically with
SET CAMERA TO FOLLOW ...
the position the camera follows is passed in the first
3 parameter of the command. The camera position is higher
as the objects position y#+2 to allow a look down on the
object. The camera has a minimal distance of 10 units to
the object and the camera movement is soothed 100%
we will not position the camera in the beginning, so
the cam ist at position 0,0,0 and will get a camera flight
to the object because the cam will move from 0,0,0 to its
assigned position
remend
x#=250 : ` x - position
z#=250 : ` z - position
y#=2 : ` y - position
`now we create the object and position it at x,y,z-position
make object cube 1,2
color object 1, rgb(255,0,32) : ` a nice red cube
position object 1, x#,y#,z#
` Initial rotation = 0
r#=0
do
`
` each loop, we update the variables with the actual object position
`
x#= object position x(1)
z#= object position z(1)
y#= object position y(1)
`
` When a key (up,down) is pressed we set the speed to
` initiate a movement
`
if upkey()=1
speed# = .1
endif
if downkey()=1
speed# = -.1
endif
`
` When a key (left,right) is pressed we change the rotation
`
if rightkey()
r#=r#+.5
endif
if leftkey()
r#=r#-.5
endif
` we will stay between 0 & 360 degrees (DB supports although
` negative values, but I like to have positive values
`
if r# > 360 then r# = r# - 360
if r# < 0 then r# = r# + 360
`
` now we rotate the object to its new angle (r#)
`
yrotate object 1,r#
` now we calculate new object position
` we use standard trigonometric calculations to do this:
`
` when moving from position x1/z1 to x2/z2
`
` the effective movement in x-direction is
` cos(r#) multiplied by the speed
`
` the effective movement in z-direction is
` sin(r#)[or cos(90-r#) multiplied by the speed
nz#=z#
nx#=x#
ny#=y#
dz#=cos(r#)
dy#=cos(90-r#)
nz#=nz#+Speed#*dz#
nx#=nx#+Speed#*dy#
x#=nx#
y#=ny#
z#=nz#
` after the movement we will set the speed to 0 or the
` object will continue to move endless
`
speed#=0
`
` Now we put the object to its new position
`
position object 1,nx#,y#,nz#
` SET CAMERA TO FOLLOW X, Y, Z, Angle, Distance, Height, Smooth, Collision
set camera to follow x#,y#,z#, r#, 10, y#+2, 100, 1
`
` this will output the objects position & rotation to the screen
`
text 1,1, "FPS: "+str$(screen fps())+" R: "+str$(r#)
text 1,15, "x: "+str$(int(x#))+" y:"+str$(int(y#))+" z:"+str$(int(z#))
sync
loop
end