Get the media file here:
[url]http://www.realgametools.net/forums/attachments/FPS_sliding_collision.zip
[/url]
RemStart
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
¤PROGRAM¤ | FIRST PERSON SHOOTER WITH SMOOTH SLIDING COLLISION FOR ANY 3D OBJECT
¤DESCRIPTION¤ | Shows how to implement sliding collision with a 3D object game level
| It should work with walls at any angle and curved walls too!
| Uses native DB polygon collision and smoothing camera movement tricks
| Also shows how to implement standard mouse with W,A,S,D movement
|
| NOTE: To run, this example requires that you download the
| corresponding media.
|
¤AUTHOR¤ | David Yakobi aka Yarbles
¤DATE¤ | July 16,2002
¤CONTACT¤ | Email: dyakobi@hotmail.com
|
¤CREDITS¤ | Josh Mooney aka Moondog for the 3D models
|
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
RemEnd
rem ================
rem System Setup
rem ================
sync on
set display mode 800,600,16
hide mouse
sync rate 60
set global collision on
rem ================
rem Load Media
rem ================
rem create player collision object and hide
make object box 100,100,400,100
set object collision on 100
set object collision to spheres 100
hide object 100
rem load level visible object
load object "levelmod.x",2000
load image "inside02.jpg",1
set object collision on 2000
set object collision to polygons 2000
set object 2000,1,1,1,1,1,1,1
texture object 2000,1
rem =====================================
rem setup scene properties and constants
rem =====================================
color backdrop 1
fog on
fog distance 3000
set ambient light 70
ftimer = 3
set camera range 1,5000
set directional light 0,0,0,0
color light 0,rgb(0,0,0)
rem set distance travelled per step
distance=40
rem ================
rem Main Loop
rem ================
do
rem store original values
oldx# = newx#
oldz# = newz#
rem perform sliding collision
if collision>0
noCollision=0
rem slide perpendicular to the camera angle
newx# = newxvalue(newx#,wrapvalue(a#+90),2*distance)
newz# = newzvalue(newz#,wrapvalue(a#+90),2*distance)
position object 100,newx#,-100,newz#
rem test collision again to see if we went in the wrong direction
if object collision(100,2000)>0
rem reset position back to the original and slide in other direction
newx#=oldx#
newz#=oldz#
newx# = newxvalue(newx#,wrapvalue(a#-90),2*distance)
newz# = newzvalue(newz#,wrapvalue(a#-90),2*distance)
endif
else
rem the smoothing code here can certainly be tweaked further
rem if anyone finds ways to improve it, please post your updates
rem if no collision for a while, camera follows player collision object
if noCollision>4
colx#=newx#
colz#=newz#
endif
rem smooth the camera movement
x#=curvevalue(colx#,x#,5.0)
z#=curvevalue(colz#,z#,5.0)
rem uncomment these to see how it looks without smoothing
` x#=newx#
` z#=newz#
rem "smoothly" position camera and gun
position camera x#,0,z#
` position object 1,x#,0,z#
` position object 2,x#,0,z#
endif
rem reset the collision flags
collision=0
inc noCollision
rem rotate camera with mouse
cya#=wrapvalue(cya#+(mousemovex()))
cxa#=cxa#+(mousemovey()/2.0)
rem limit vertical angle
if cxa#45.0 then cxa#=45.0
rem perform rotation smoothing
crx#=curveangle(cxa#,crx#,1.0)
cry#=curveangle(cya#,cry#,4.0)
rotate camera crx#,cry#,0
rem Get movement input for standard WASD movement
rem W
if KEYSTATE(17)=1
rem check for key combinations and set angle accordingly
rem A
if KEYSTATE(30)=1
a# = wrapvalue(camera angle y()-45)
else
rem D
if KEYSTATE(32)=1
a# = wrapvalue(camera angle y()+45)
else
a# = camera angle y()
endif
endif
newx# = newxvalue(newx#,a#,distance)
newz# = newzvalue(newz#,a#,distance)
else
rem S
if KEYSTATE(31)=1
rem check for key combinations and set angle accordingly
rem A
if KEYSTATE(30)=1
a# = wrapvalue(camera angle y()-135)
else
rem D
if KEYSTATE(32)=1
a# = wrapvalue(camera angle y()+135)
else
a# = wrapvalue(camera angle y()+180)
endif
endif
newx# = newxvalue(newx#,a#,distance)
newz# = newzvalue(newz#,a#,distance)
else
rem A
if KEYSTATE(30)=1
rem check for key combinations and set angle accordingly
rem W
if KEYSTATE(17)=1
a# = wrapvalue(camera angle y()-45)
else
rem S
if KEYSTATE(31)=1
a# = wrapvalue(camera angle y()-135)
else
a# = wrapvalue(camera angle y()-90)
endif
endif
newx# = newxvalue(newx#,a#,distance)
newz# = newzvalue(newz#,a#,distance)
else
rem D
if KEYSTATE(32)=1
rem check for key combinations and set angle accordingly
rem W
if KEYSTATE(17)=1
a# = wrapvalue(camera angle y()+45)
else
rem S
if KEYSTATE(31)=1
a# = wrapvalue(camera angle y()+135)
else
a# = wrapvalue(camera angle y()+90)
endif
endif
newx# = newxvalue(newx#,a#,distance)
newz# = newzvalue(newz#,a#,distance)
endif
endif
endif
endif
rem perform movement on player collision object
position object 100,newx#,-100,newz#
rem test for initial collision
if object collision(100,2000)
collision=2000
rem reset position variables back
newx# = oldx#
newz# = oldz#
rem position player collision object back to original position
position object 100,newx#,-100,newz#
rem store current cam and gun position
colx#=newx#
colz#=newz#
endif
rem print stats
ink 0,0
set cursor 10,20
print screen fps()
sync
loop
The Yellow Jester does not play but gently pulls the strings
And smiles as the puppets dance in the court of the Crimson King.