REM Project: 2d physics
REM Created: 9/12/2006 4:38:11 PM
REM
REM ***** Main Source File *****
REM
global dim world_lines#(1000,10)
global lines
global diffangle#
global x
global y#
global increase#
global bounce
global poin
cls rgb(255,0,255)
set image colorkey 255,0,255
ink 0,0
for i=1 to 10
   circle 10,10,i
next i
get image 1,0,0,21,21
x=8
for i=1 to 5
world_line(0,250+i,500,300+i)
next i
 
for i=1 to 5
world_line(550,325+i,350,400+i)
next i
 
for i=1 to 5
world_line(300,450+i,450,600+i)
next i
 
for i=1 to 5
world_line(500,700+i,350,900+i)
next i
 
 
 
 
sync on
sync
do
if rightkey()=1
   x=x+3
   if 0<manage_collision(x,y#) then x=x-3
endif
 
if leftkey()=1
   x=x-3
   if 0<manage_collision(x,y#) then x=x+3
endif
if upkey()=1
   y#=y#-3
   if 0<manage_collision(x,y#) then y#=y#+3
endif
if downkey()=1
   y#=y#+3
   if 0<manage_collision(x,y#) then y#=y#-3
endif
manage_lines()
paste image 1,x,y#,1
oldx#=x#
oldy#=y#
coll=manage_collision(x,y#)
if coll=0 then gravity(1)
if coll>0 then x= slide_collision(x,y#,coll)
text 0,0,str$(coll)
sync
wait 10
cls rgb(255,255,255)
loop
 
 
 
function world_line(x1#,y1#,x2#,y2#)
   lines=lines+1
   world_lines#(lines,0)=x1#
   world_lines#(lines,1)=x2#
   world_lines#(lines,2)=y1#
   world_lines#(lines,3)=y2#
   world_lines#(lines,4)=(y1#-y2#)/(x1#-x2#)
   world_lines#(lines,5)=y1#-(world_lines#(lines,4)*x1#)
   for i=0 to 5
      text 0,20*i+20,str$(world_lines#(lines,i))
   next i
   sync
   `suspend for key
endfunction
 
function manage_lines()
   for i=1 to lines
      line world_lines#(i,0),world_lines#(i,2),world_lines#(i,1),world_lines#(i,3)
   next i
endfunction
 
function manage_collision(x1,y1)
   y1=y1
   for i=1 to lines
      if y1-(world_lines#(i,4)*x1)-world_lines#(i,5)>-2 and y1-(world_lines#(i,4)*x1)-world_lines#(i,5)<2 and y1>world_lines#(i,2) and y1<world_lines#(i,3) then collision=i:goto collisionchecked
   next i
   y1=y1+20
   for i=1 to lines
      if y1-(world_lines#(i,4)*x1)-world_lines#(i,5)>-2 and y1-(world_lines#(i,4)*x1)-world_lines#(i,5)<2 and y1>world_lines#(i,2) and y1<world_lines#(i,3) then collision=i:goto collisionchecked
   next i
   y1=y1-20
   x1=x1+20
   for i=1 to lines
      if y1-(world_lines#(i,4)*x1)-world_lines#(i,5)>-2 and y1-(world_lines#(i,4)*x1)-world_lines#(i,5)<2 and y1>world_lines#(i,2) and y1<world_lines#(i,3) then collision=i:goto collisionchecked
   next i
   y1=y1+20
   for i=1 to lines
      if y1-(world_lines#(i,4)*x1)-world_lines#(i,5)>-2 and y1-(world_lines#(i,4)*x1)-world_lines#(i,5)<2 and y1>world_lines#(i,2) and y1<world_lines#(i,3) then collision=i:goto collisionchecked
   next i
   collisionchecked:
   bounce=0
   if collision>0 and increase#>8 then bounce=1
   if collision>0 and increase#>0 and bounce=0 then increase#=0
endfunction collision
 
function slide_collision(x1,y1,collision)
   if bounce=0
      neg=1
      regx=x1
      slide:
         if neg=1 then change=change+1:x1=regx+change
         if neg=-1 then x1=regx-change
         neg=neg*-1
         if 0<manage_collision(x1,y1) then goto slide
         if neg=-1 then x1=x1-change+1
         if neg=1 then x1=x1+change-1
         y#=y#+abs(2/change)
   endif
   if bounce=1
      angle#=atan(-1/world_lines#(i,4))
      angle2#=atan((oldy#-y#)/(oldx#-x#))
      diffangle#=tan((angle#-angle2#)-360)
      increase#=-100
   endif
endfunction x1
 
function gravity(image)
   if increase#<0
      increase#=increase#+1
      if diffangle#<0
         y#=y#+2*diffangle#
         x=x-2
      endif
      if diffangle#>0
         y#=y#-2*diffangle#
         x=x+2
      endif
      if increase#=0 then diffangle#=0
   endif
   if increase#<0.1 and increase#>-1 then increase#=1:y#=y#+increase#
   if increase#>0 and increase#<6 then increase#=increase#*1.01:y#=y#+increase#
   if increase#>=6 then y#=y#+increase#
endfunction