This month's tutorial focuses on the movement of a character FPS style using the Dark Physics Dll for DB Professional. To complete this tutorial, you will need to purchase and upgrade Dark Physics at The Game Creators website. Also you will need a copy of DB Professional and at least a little coding experience. With that said, let us begin.
First off, we set up our standard Start code.
`setup code
sync on : sync rate 60
set dir "<where ever your files are>"
set display mode <the display settings you want>
`now into the Physics.
`We have to call phy start in order to activate the
`Dark Physics plug-in.
phy start
`now we set up our level
make object box 1,50,1,50
make object box 2,50,50,1 : position object 2,0,25,25
make object box 3,50,50,1 : position object 3,0,25,-25
make object box 4,1,50,50 : position object 4,-25,25,0
make object box 5,1,50,50 : position object 5,25,25,0
`now we use DP to make a static collision body for
`every part of our level. This ensures that the walls
`will remain in place.
for t = 1 to 5
phy make rigid body static box t
next t
`make our player and set him up for collision.
player=6
make object box player,1,2,1 : position object player,0,2,0
`objectnumber,posx,posy,posz,sizex,sizey,sizez,step,slope
phy make character controller box player,0,2,0,.6,.9,.6,.25,50
` A brief explanation of how the character controller works...
`the objectnumber is the object that will be attached to the `character
`controller, allowing you to move it.
`the posx,posy, and posz are the starting position of the controller
`sizex,y, and z are the size of the controller.
`this one needs some explaining. Notice how size on the x axis is
`only .6, while the box is 1 unit in diameter?
`this is because of the "skin width"
`in order to prevent the physics engine from goofing up,
`each object has its own "buffer zone", referred to as skin width.
`so if I set the sizex to 1 then the physics engine would automatically
`expand the collision boundaries to greater than the objects actual
`width. If this happened then the character wouldn't be able to fit
`into places that he would logically fit into.
`by reducing the actual size of the controller we can reduce the `buffer
`zone until the collision boundaries are exactly the same size as
`the character.
`step is how big an object the player can step over.
`slope is how great of an angle the player can climb, so that
`your player cannot climb up the faces of mountains.
` begin the main loop
do
` call the handle player subroutine
gosub handle_player
phy update
sync
loop
`begin the handle player subroutine
handle_player:
`every key has a scan code, ans since we will be using the
`classic FPS controls, we will use scancodes 17(w) 30(a) 31(s) and
`32(d)
` we call the scancode command to check if no key is being pressed.
if scancode()=0 then phy move character controller player,0
`now we use the keystate() command for forwards and backwards `movement.
if keystate(17)=1
phy move character controller player,10
endif
if keystate(31)=1
phy move character controller player,-10
endif
`now comes the complicated part.
`the character controller can not move left or right, so
`we must find a work around if we want to have strafing.
`The work around is relatively simple. we simply rotate the
`character controller + or - 90 degrees, move it, then rotate it
`back.
if keystate(30)=1
rotate object player,0,-90,0
phy move character controller player,10
rotate object player,0,90,0
endif
if keystate(32)=1
rotate object player,0,90,0
phy move character controller player,10
rotate object player,0,-90,0
endif
`now that we have movement, it is time for the mouse camera `movement.
`Mouse Camera Movement
position camera object position x(player),object position y(player)+1,object position z(player)
CAMY#=CAMY#+MOUSEMOVEX()*.1
CAMX#=CAMX#+MOUSEMOVEY()*.1
IF CAMX#>90 AND CAMX#<135 THEN CAMX#=90
IF CAMX#<-90 THEN CAMX#=-90
YROTATE CAMERA CAMY#
XROTATE CAMERA CAMX#
YROTATE OBJECT player,CAMY#
`position the mouse at the center of the screen.
position mouse 512,384
return
`allow me to explain a little bit this last part.
`the assigning of variables is pretty standard, with camX and `camY being the camera x and y angle.
`the IF CAMX#>90 part is to ensure that the camera will not go
`past straight down or straight up. If you don't understand the `point of this, then take out that code and run the program, and `it will be pretty obvious after a time.
`this wraps up the tutorial. you will notice when you run the `program that you cannot jump. quite simply, you will have to `search on the forums for how to do that. be sure to tune in `next month for how to implement shooting.