Posted: 1st Feb 2004 18:36
Here we go again, this is a little code snippet that will introduce fast and easy sliding collision. Using math (if player x = wall x,etc) the sphere (player) is placed at the correct spot. It wont jitter,lag in corners.

It only works with 0,90,180,270 rotated walls (yrotated)

+ Code Snippet
rem Always use sync on. I prefer sync rate 0, so that the screen refreshes
rem as fast as possible
sync on
sync rate 0

rem The plains are the walls, Ive positioned them so they form a house :)
make object plain 2,20,20
position object 2,50,0,0
yrotate object 2,90

make object plain 3,20,20
position object 3,50,0,30
yrotate object 3,90

make object plain 4,20,20
position object 4,40,0,40

make object plain 5,20,20
position object 5,30,0,30
yrotate object 5,90

make object plain 6,20,20
position object 6,30,0,0
yrotate object 6,90

make object plain 7,20,20
position object 7,40,0,-10

rem Red is nicer
for a=2 to 7
color object a,rgb(200,0,0)
next a

rem We need a ground too!
make object plain 8,500,500
xrotate object 8,90

rem Green is nicer!
color object 8,rgb(0,200,0)


rem the sphere is the player
make object sphere 1,5
position object 1,0,0,0

rem Blue is nicer!
color object 1,rgb(0,0,200)


do

 rem When leftkey=1 then the sphere will rotate left
 if leftkey()=1 then yrotate object 1,wrapvalue(object angle y(1)-1)

 rem And when rightkey=1 it will rotate right
 if rightkey()=1 then yrotate object 1,wrapvalue(object angle y(1)+1)

 rem IF the player presses up, function MOV_PLAYER will be called
 if upkey()=1 then mov_player(1,1,5.0,2,7)
 rem 1 = the player object number
 rem 1 = the speed
 rem 5.0 = the size of the player, we made a sphere that had 5.0 as size
 rem 2 and 7 is used in a for loop to check for collision, we made 6 walls. If we made 20, and the object
 rem numbers were 100-119 we would use 100,119

 rem A little math to position the camera
 position camera object position x(1)+sin(object angle y(1)+180)*10,7,object position z(1)+cos(object angle y(1)+180)*10
 point camera object position x(1),2.5,object position z(1)

 rem Tells you where the sphere is
 text 10,10,str$(object position x(1))
 text 10,25,str$(object position z(1))

 sync

loop


rem This is the main code, it will check for collision
function mov_player(player,speed,size#,start,endw)
size#=size#/2

rem First, if the player WILL move, where would it be?
x#=object position x(player)+sin(object angle y(player))*speed
z#=object position z(player)+cos(object angle y(player))*speed

rem Now lets check for collision
for wall=start to endw

rem We wont waste cpu cycles, wont we?
if object exist(wall)=1

 rem The size of the walls / 2
 wall_size#=20.0 / 2.0

 rem If the wall is rotated, we need to use a different piece of code. 2 walls are NOT rotated, so they would
 rem use the code below "ELSE"
 if object angle y(wall)=90 or object angle y(wall)=270

  if z#<(object position z(wall)+size#)+wall_size# and z#>(object position z(wall)-size#)-wall_size#
   if x#>object position x(wall)-size# and x#<object position x(wall)+size# then x#=object position x(player)
  endif

 else

  if x#<(object position x(wall)+size#)+wall_size# and x#>(object position x(wall)-size#)-wall_size#
   if z#>object position z(wall)-size# and z#<object position z(wall)+size# then z#=object position z(player)
  endif

 endif

endif
next wall

rem Lets put the sphere where we want him to be!
position object player,x#,size#,z#
endfunction


Posted: 1st Feb 2004 19:01
Great... can I use it in my tutorial?

like this:


" This is a Great code my Emperal Baal"

I make sure I give you full credit!

Thank-You!
Posted: 1st Feb 2004 21:14
Sure, its free isnt it
Posted: 2nd Feb 2004 0:14
This code uses a position check. If walls are 0/90/180/270 yrotated this is possible. With different angles you would need way more complicated math. Im working on it.

You can use this in a 3D world, you place the buildings 0/90/180/270 yrotated, the player wouldnt notice it and the collision will work
Posted: 3rd Feb 2004 8:43
is this the system you're using for Velocity X-Treme?
Posted: 3rd Feb 2004 15:35
sort of, but velocity x-treme uses a cos/sin position check. Im not going to show you the code, because its more than 100 lines
Posted: 4th Feb 2004 2:55
Ya if its just 0 - 90 (180 and 270 are just the same on a cube ) then your in trouble if your going for circles

NJ tho