TGC Codebase Backup



Matrix Sliding Collision by Turtle Soup

13th Jul 2006 13:15
Summary

This code shows how a matrix and a few variables can be used to produce a sliding collision effect with both static and moving objects



Description

This code is actually created from several codebase entries which i have merged together with the matrix heigh collision part of the "ICED" demo in DarkBASIC
you control the camera
the area that the ball (enemy) occupies is translated to co-ordinates on the matrix, which are then raised to 1000 in height
the rest of the matrix is lowered to 0
and everytime the ball moves the matrix is updated along with the 1000 high sections that the enemy occupies
the ball also follows the camera to demonstate how the movement alters the matrix
if the player moves the camera towards the ball the matrix will shift the camera out of the way, thereby giving an effect of the camera sliding out of the way of the enemy
the high parts of the matrix do not have to be shown and can be hidden by updating the matrix before the co-ordinates are plotted

I hope this comes in use
=]



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    
`Matrix Slising Collision
`Created by Luke Brown
`With thanx to the "ICED" demo
`And Matt Henry
`For parts of code I used

sync on

rem create matrix
xseg = 45
zseg = 45
make matrix 1,2000,2000,xseg,zseg

rem make Ball
make object sphere 1,100
position object 1,1000,50,1000

rem this is the height
height = 1000

rem position camera
X#=150
Z#=150

rem begin loop
do

set cursor 1,1
print "x: ",camera position x()
print "y: ",camera position y()
print "z: ",camera position z()
Print "Newx: ",newx
Print "Newz: ",newz

x = object position x(1)
z = object position z(1)

rem thanks to Matt Henry for this part of the code which find the position of an x,z co-ordinate
rem and translates it, matrix grid style

rem "basically if its past the x or z of the matrix
rem devided by the X and Z segments then it adds one to its self
rem and vice versa vor being lower
rem it only does this while camera is inside the following:"
if x > 10 and x <=1990 and z>10 and z <=1990

repeat
if newx < X/xseg then newx = newx + 1
if newz < Z/zseg then newz = newz + 1
if (newx -2) = x/xseg then newx = newx -1
if (newz -2) = z/zseg then newz = newz -1
until (newx >= x/xseg) and (newz>=z/zseg)

rem this flattens the matrix
for q=1 to xseg
for w=1 to zseg
set matrix height 1,q,w,0
next q
next w

rem un-rem this line of code and delete the one belw if you want to hide the raised matrix
`update matrix 1

rem this raises the matrix effected by the Ball
set matrix height 1,newx,newz,height
rem this makes the area effected by the ball bigger. by 1 point in each direction
set matrix height 1,newx+1,newz,height
set matrix height 1,newx-1,newz,height
set matrix height 1,newx,newz+1,height
set matrix height 1,newx,newz-1,height
set matrix height 1,newx+1,newz+1,height
set matrix height 1,newx-1,newz-1,height
endif

rem updates the matix
rem AFTER the matrix heights have been updated
update matrix 1

rem simple A.I. for Ball
point object 1,x#,50,z#
move object 1,0.5


rem First Person Controls, Mouselook, Strafe, Etc
 OldCamAngleY# = CameraAngleY#
 OldCamAngleX# = CameraAngleX#

 CameraAngleY# = WrapValue(CameraAngleY#+MousemoveX()*0.2)
 CameraAngleX# = WrapValue(CameraAngleX#+MousemoveY()*0.2)

 If Upkey()=1
  XTest# = Newxvalue(X#,CameraAngleY#,10)
  ZTest# = Newzvalue(Z#,CameraAngleY#,10)
  If XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
   X#=XTest#
   Z#=ZTest#
  Endif
 Endif

 If Downkey()=1
  XTest# = Newxvalue(X#,Wrapvalue(CameraAngleY#-180),10)
  ZTest# = Newzvalue(Z#,Wrapvalue(CameraAngleY#-180),10)
  If XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
   X#=XTest#
   Z#=ZTest#
  Endif
 Endif

 If Leftkey()=1
  XTest# = Newxvalue(X#,Wrapvalue(CameraAngleY#-90),10)
  ZTest# = Newzvalue(Z#,Wrapvalue(CameraAngleY#-90),10)
  If XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
   X#=XTest#
   Z#=ZTest#
  Endif
 Endif

 If Rightkey()=1
  XTest# = Newxvalue(X#,Wrapvalue(CameraAngleY#+90),10)
  ZTest# = Newzvalue(Z#,Wrapvalue(CameraAngleY#+90),10)
  If XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
   X#=XTest#
   Z#=ZTest#
  Endif
 Endif


 Yrotate camera CurveAngle(CameraAngleY#,OldCamAngleY#,24)
 Xrotate camera CurveAngle(CameraAngleX#,OldCamAngleX#,24)

 Y# = Get ground height(1,X#,Z#)
 Position Camera X#,Y#+50,Z#

rem ensure player stays on floor. From the "ICED" demo
fx#=0 : fz#=0
for r=0 to 35
  ex#=newxvalue(x#,r*10,15)
  ez#=newzvalue(z#,r*10,15)
  eh#= get ground height(1,ex#+mx#,ez#+mz#)
  if eh#<-50.0 then eh#=-50.0
  force#=(eh#+50.0)/10.0
  fx#=fx#+((ex#-x#)*force#)
  fz#=fz#+((ez#-z#)*force#)
next r
x#=x#-(fx#/105.0)
z#=z#-(fz#/105.0)



sync
loop