TGC Codebase Backup



Simple tile by DARKGuy

7th Sep 2004 16:51
Summary

This is a code snippet that could be used like a tile-based collision and movement system.



Description

Greetings!

This is a code snippet that could be used like a tile-based collision
and movement system. With just 4 objects (spheres) you can detect if
there's a wall at your front, back, left and right respectively.

I hope this code works useful for some of you.

As the sync rate was in 60, I had to change the speed of movement to 0.5
so it moved softly, and also to prove that also works without having the
player to move tile-by-tile.

If you use this code in your programs, please include my name in the
credits / source code, it's all that I ask :).

INFORMATION:

OBJECT 1: PLAYER

OBJECT 10-13: For detecting collisions in the 4 directions
OBJECT 10: COLLISION LEFT
OBJECT 11: COLLISION RIGHT
OBJECT 12: COLLISION DOWN
OBJECT 13: COLLISION UP



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    remstart
-------------------------------------------------------------------
                      SIMPLE TILED COLLISION
-------------------------------------------------------------------
                     CODE SNIPPET BY DARKGUY
-------------------------------------------------------------------
Greetings!

This is a code snippet that could be used like a tile-based collision
and movement system. With just 4 objects (spheres) you can detect if
there's a wall at your front, back, left and right respectively.

I hope this code works useful for some of you.

As the sync rate was in 60, I had to change the speed of movement to 0.5
so it moved softly, and also to prove that also works without having the
player to move tile-by-tile.

If you use this code in your programs, please include my name in the
credits / source code, it's all that I ask :).

INFORMATION:

OBJECT 1: PLAYER

OBJECT 10-13: For detecting collisions in the 4 directions
  OBJECT 10: COLLISION LEFT
  OBJECT 11: COLLISION RIGHT
  OBJECT 12: COLLISION DOWN
  OBJECT 13: COLLISION UP

remend

rem Set everything correct...like Sync rate and that collisions can be detected with ANY object
sync on
sync rate 60
set global collision on

rem The mouse is annoying... :P
hide mouse

rem This is our player object: a cone. Very original :P
make object cone 1,10

rem these will be our spheres for collision detection
make object sphere 10,2
make object sphere 11,2
make object sphere 12,2
make object sphere 13,2

rem make the wall blocks
make object cube 20,10
make object cube 21,10
make object cube 22,10
make object cube 23,10

rem position the walls
position object 20,-10,0,0
position object 21,10,0,0
position object 22,-10,0,-10
position object 23,-10,0,10

do

   rem Self-explanatory
   set cursor 0,0
   print "FPS: ",screen fps()

   rem I don't know why or how, but it works if I get the position of the player before and then after
   rem too. Don't know, perhaps I don't need to get the position here...who knows...
   x#=object position x(1)
   y#=object position y(1)
   z#=object position z(1)

   rem position the camera high in the sky :P
   position camera x#,y#+60,z#-60
   rem point the camera to the player
   point camera x#,y#,z#

   rem player speed
   spd#=0.5

   if upkey()=1
      if object collision(13,0)
         print "COLLISION UP - OBJECT 13"
      else
         rem If the sphere that detects collision when you press the up key has not collided with something...
         yrotate object 1,0
         move object 1,spd#
      endif
   endif
   if downkey()=1
      if object collision(12,0)
         print "COLLISION DOWN - OBJECT 12"
      else
         rem If the sphere that detects collision when you press the down key has not collided with something...
         yrotate object 1,180
         move object 1,spd#
      endif
   endif
   if leftkey()=1
      if object collision(10,0)
         print "COLLISION LEFT - OBJECT 10"
      else
         rem If the sphere...bla bla bla
         yrotate object 1,270
         move object 1,spd#
      endif
   endif
   if rightkey()=1
      if object collision(11,0)
         print "COLLISION RIGHT - OBJECT 11"
      else
         yrotate object 1,90
         move object 1,spd#
      endif
   endif

   rem Then I get the position again...
   x#=object position x(1)
   y#=object position y(1)
   z#=object position z(1)

   rem Here is the collision range...
   rng=5
   position object 10,x#-rng,y#,z#
   position object 11,x#+rng,y#,z#
   position object 12,x#,y#,z#-rng
   position object 13,x#,y#,z#+rng

sync
loop