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)
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
Quote: "
Amd 2500+ | 1024mb pc2700 | A7N8X-X | Geforce4 ti 4200 128mb"