well... i have nothing better to do, so i'll demonstrate to the best of my ability how to understand the code Ruccus posted above
`Turret Program
`Controls: ASWD to move / turn. Spacekey to fire.
`Overview:
` - The player is a tank, that can control it's turret by moving the
` mouse around the screen.
` - The tank moves independantly of the turret orientation.
` - Bullet collision is done using DBP's native raycasting commands,
` although using sparky's collision dll would be a good idea to switch to.
`Code:
`App setup
SYNC ON:SYNC RATE 0:AUTOCAM OFF:RANDOMIZE TIMER()
`Variables
player = 1
turret = 2
target = 3
angle# = 0
`Camera
POSITION CAMERA 0,300,0:PITCH CAMERA DOWN 90
`Player
MAKE OBJECT CUBE player,30:POSITION OBJECT player,0,15,0
MAKE OBJECT BOX turret,20,10,30:COLOR OBJECT turret, RGB(100,100,100)
`Target (the respawn function positions the target randomly in the world)
MAKE OBJECT SPHERE target,50:respawn(target)
`Main Loop
DISABLE SYSTEMKEYS:WHILE ESCAPEKEY() = 0
`Player Rotation
INC angle#, KEYSTATE(32)-KEYSTATE(30)
ROTATE OBJECT player, 0, WRAPVALUE(angle#), 0
`Player movement
MOVE OBJECT player, KEYSTATE(17)-KEYSTATE(31)
`Get the player's position...
x# = OBJECT POSITION X(player)
y# = 15
z# = OBJECT POSITION Z(player)
`Position the turret ontop of the player...
POSITION OBJECT turret, x#, y# + 20,z#
`Now we want to calculate the angle between the player and the mouse.
`To do this we need to convert the mouse's 2D screen location into a
`3D world location, because the screen uses a different positioning
`layout that the 3D world. This can be done with the pick screen command.
`We call the pick screen command with the mouse's coordinates and the
`camera's height, and then add the pick vector results to the camera's
`x and z position to get the mouse's 3D x and z position.
PICK SCREEN MOUSEX(), MOUSEY(), CAMERA POSITION Y()
mx# = CAMERA POSITION X() + GET PICK VECTOR X()
mz# = CAMERA POSITION Z() + GET PICK VECTOR Z()
`Now we need to calculate the angle between the player and the mouse.
`This can be achieved with the atanfull command.
mangle# = ATANFULL(x#-mx#,z#-mz#)
`And finally, we rotate the turret object to this calculated angle, which
`whill essentially point the turret towards our mouse, while the base
`of the tank - the player - continues to move freely.
ROTATE OBJECT turret, 0, mangle#, 0
`Bullet Calculation
`If the spacekey is pressed
IF SPACEKEY()
`Cast an intersection ray from the player's coordinates to the
`mouse's 3D coordinates, checking for collision with the target
`object. If a collision occurs, respawn the target.
IF INTERSECT OBJECT(target, x#, y#, z#, mx#, 25, mz#)
respawn(target)
ENDIF
ENDIF
`Render
SYNC
`Repeat until escapekey is pressed, then close program
ENDWHILE:END
`Respawn Function
FUNCTION respawn(object)
`Calculate a random angle and distance
angle# = RND(360)
dist# = 50 + RND(50)
`Use this angle and distance to calculate a point in the 3D world.
`The newvalue commands take an angle and a distance or "step" value,
`in combination with a current position, and calculate a new position.
`These are the commands we will also be using for figuring out
`the intersection ray's ending position for bullet calculations.
`See the raycheck() command for more.
x# = NEWXVALUE(0, angle#, dist#)
z# = NEWZVALUE(0, angle#, dist#)
POSITION OBJECT object, x#, 25, z#
ENDFUNCTION
"SYNC ON:SYNC RATE 0:AUTOCAM OFF:RANDOMIZE TIMER()"
I'm sure you know what the sync on and sync rate commands do, so i'll skip over those.
"autocam off"- when an object is made (when autocam is on, which it is by default), the camera moves back to the origin <0,0,0>, when autocam is off, then the camera stays in it's position.
"randomize timer()" - The randomize command is used in junction with the RND command. Basically, the randomize command sets the "Seed" value. All RND commands after the randomize command, will be determined by that seed value. For example, try entering this code:
randomize 1
print "Seed value set to 1"
print rnd(10)
print rnd(10)
print rnd(10)
print rnd(10)
randomize 1
print "Seed value reset to 1"
print rnd(10)
print rnd(10)
print rnd(10)
print rnd(10)
randomize 1
print "Seed value reset to 1"
print rnd(10)
print rnd(10)
print rnd(10)
print rnd(10)
wait 20000
if you execute that, you'll see that, given the same seed, the sequence of rnd numbers after that will be the same.
After that first line, theres some variables set up, pretty self explanitory.
POSITION CAMERA 0,300,0
ITCH CAMERA DOWN 90
sets the camera position, and changes the pitch of the camera down. (pitch, yaw, roll, im not exactly sure which is which, but it rotates the camera)
Then, it makes, positions, and colors the objects.
Now for the function. You already said that you didn't understand this, so i'll explain them.
Take for example this code:
returnvalue = whatever(100,200,300)
print returnvalue
wait 20000
function whatever(a,b,c)
b=a/2
c=b*c/a
g=a*b*c
endfunction g
This is exactly the same as this code:
`set the variables, exactly the same as (100,200,300) after the function used
a=100
b=200
c=300
`uses the variables.
b=a/2
c=b*c/a
g=a*b*c
`returnvalue=whatever(100,200,300). Since in the function "whatever", it ended with "endfunction g", the function returned
`the variable "g".
returnvalue=g
print returnvalue
wait 20000
I'll leave it at that. If you still don't understand functions, theres plenty of rescources online if you just do a google search, but that might return alot of code in other languages. (doesn't really matter, the concept's the same)
Now for the main loop. I'll skip the "disable systemkeys command"
WHILE ESCAPEKEY() = 0
...
...
...
endwhile
end
This is basically what the do/loop is.
The while command is pretty simple.
if the command after the "while" command is true, after the "Endwhile" command is called, the loop will continue down from the while command to the endwhile command, then it repeats until the command after the "while" command is false.
In your regular DBPro loop, do/loop ends whenever you press the escape key.
This code:
`Player Rotation
INC angle#, KEYSTATE(32)-KEYSTATE(30)
ROTATE OBJECT player, 0, WRAPVALUE(angle#), 0
`Player movement
MOVE OBJECT player, KEYSTATE(17)-KEYSTATE(31)
`Get the player's position...
x# = OBJECT POSITION X(player)
y# = 15
z# = OBJECT POSITION Z(player)
`Position the turret ontop of the player...
POSITION OBJECT turret, x#, y# + 20,z#
well, it looks like you're already familiar with that.
This code is the hard part.
`Now we want to calculate the angle between the player and the mouse.
`To do this we need to convert the mouse's 2D screen location into a
`3D world location, because the screen uses a different positioning
`layout that the 3D world. This can be done with the pick screen command.
`We call the pick screen command with the mouse's coordinates and the
`camera's height, and then add the pick vector results to the camera's
`x and z position to get the mouse's 3D x and z position.
PICK SCREEN MOUSEX(), MOUSEY(), CAMERA POSITION Y()
mx# = CAMERA POSITION X() + GET PICK VECTOR X()
mz# = CAMERA POSITION Z() + GET PICK VECTOR Z()
`Now we need to calculate the angle between the player and the mouse.
`This can be achieved with the atanfull command.
mangle# = ATANFULL(x#-mx#,z#-mz#)
`And finally, we rotate the turret object to this calculated angle, which
`whill essentially point the turret towards our mouse, while the base
`of the tank - the player - continues to move freely.
ROTATE OBJECT turret, 0, mangle#, 0
PICK SCREEN MOUSEX(), MOUSEY(), CAMERA POSITION Y()
mx# = CAMERA POSITION X() + GET PICK VECTOR X()
mz# = CAMERA POSITION Z() + GET PICK VECTOR Z()
pick screen screen x, screen y, distance
take this code:
pick screen mousex(), mousey(),10
imagine this: your camera/the viewer is given a random location, and random angle. Now, on your screen, imagine placing a finger on the place that your mouse is, and reaching into the screen by 10 units, in a straight line. That's what the pick screen command does. Since commands in dbpro can only return scalar values, you have to use the commands "get pick vector x", "get pick vector y", and "get pick vector z"
Hope that helped