TGC Codebase Backup



Sliding Collision by watermark

4th May 2004 23:31
Summary

Very simple sliding collision. This code only works for objects directly on the x and z axis, meaning rotated objects wont work correctly.



Description

Very simple sliding collision. This code only works for objects directly on the x and z axis, meaning rotated objects or round objects wont work correctly. But hay, this is DBC...I only have but so much to work with. Obviously the movement code is nearly a copy and paste from the "monster hunt" tutorial, I liked the way it worked and it makes this easier to understand. Focus on the foreshadow object and the subroutine at the end and where the sub is called. This is surprisingly simple, but for some reason I kept trying to be complicated.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `I appreciate credit where due
rem set up atmosphere
Sync On
Sync Rate 30
Hide mouse
Backdrop on
Set camera range 1,5000
Fog on
Fog distance 4000
Fog color RGB(128,128,128)
Color Backdrop RGB(128,128,128)

rem inilalize player origin
X#=0
Z#=0
Y#=0

Rem Make Cubes and place randomly
For x = 1 to 5
   Make object cube x,100
   Position object x,Rnd(2000),0,Rnd(2000)
   Set object collision to boxes x
Next i

rem uncomment this to see how a rotate object reacts with this method...it doesnt work so well
`rotate object 1,0,45,0

`-----------------------------------------------------------
rem make player forshadow collision box, this will be placed where the player would be if the location was "good"
make object cube 10,5
Set object collision to boxes 10
hide object 10
`-----------------------------------------------------------

Rem Main loop
Do
   oldcAY# = cAY#
   oldcAX# = cAX#

   cAY# = WrapValue(cAY#+MousemoveX()*0.2)
   cAX# = WrapValue(cAX#+MousemoveY()*0.2)
   caZ# = Camera angle Z()

   Rem Control input for camera
   If Upkey()=1
      XTest# = Newxvalue(X#,cAY#,10)
      ZTest# = Newzvalue(Z#,cAY#,10)
      gosub SlidingCollision
   else
      If Downkey()=1
         XTest# = Newxvalue(X#,Wrapvalue(cAY#-180),10)
         ZTest# = Newzvalue(Z#,Wrapvalue(cAY#-180),10)
         gosub SlidingCollision
      Endif
   Endif

   If Leftkey()=1
      XTest# = Newxvalue(X#,Wrapvalue(cAY#-90),10)
      ZTest# = Newzvalue(Z#,Wrapvalue(cAY#-90),10)
      gosub SlidingCollision
   else
      If Rightkey()=1
         XTest# = Newxvalue(X#,Wrapvalue(cAY#+90),10)
         ZTest# = Newzvalue(Z#,Wrapvalue(cAY#+90),10)
         gosub SlidingCollision
      Endif
   Endif

   Rem Rotate camera
   YRotate camera CurveAngle(cAY#,oldcAY#,24)
   XRotate camera CurveAngle(cAX#,oldcAX#,24)

   Rem Position Camera
   Position Camera X#,Y#+50,Z#

   Rem Refresh Screen
   Sync
Loop

Rem Heres the magic
SlidingCollision:
   position object 10,XTest#,Y#,Z#
   if object collision(10,0) = 0 then X# = XTest#
   position object 10,X#,Y#,ZTest#
   if object collision(10,0) = 0 then Z# = ZTest#
return