REM ***********************************************
REM Title: Rope
REM Author: 
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
sync on
 
rem make joints
for a=1 to 10
   make object sphere a,0.5
   position object a,(sin(((a-1)*18)-90)*10.0),(sin((a-1)*18)*-10.0)+30.0,0
next a
 
rem make rope array
dim rope#(10,20,4)
g#=-9.80
makerope(1,1,10,0.001,1.0)
setjoint(1,10)
 
rem setup
color backdrop 0
position camera 10,20,-50
 
rem vars
time=timer()
dim clicked(0)
 
 
 
rem DO-LOOP
 
do
   text 0,0,str$(screen fps())
   dt#=(timer()-time)/1000.00
   time=timer()
   ropeupdate(1,g#,dt#,0.06,50.0)
 
   rem draw
   for a=1 to 9
      line object screen x(a),object screen y(a),object screen x(a+1),object screen y(a+1)
   next a
 
   rem mouse dragger
   if mouseclick()=1
      for a=1 to 10
         if ((object screen x(a)-mousex())*(object screen x(a)-mousex()))+((object screen y(a)-mousey())*(object screen y(a)-mousey()))<20 then clicked(0)=a
      next a
   endif
 
   if mouseclick()=0 then clicked(0)=0
 
   sync
loop
 
 
 
 
 
 
rem REMAIN JOINT
 
function setjoint(num,joint)
   rope#(num,joint,4)=1
endfunction
 
 
 
rem update rope function
 
function ropeupdate(num,gravity#,dt#,airf#,steps#)
 
   dt#=(1.00/steps#)*dt#
   steps=steps#
 
   for q=1 to 0+steps
 
      for a=rope#(num,0,0) to rope#(num,0,1)
 
         cur=(a-rope#(num,0,0))+1
 
         rem reset variables
         Fx#=0.0 : Fy#=rope#(num,cur,3)*gravity# : Fz#=0.0
 
         rem set variables
         xr#=object position x(a) : yr#=object position y(a) : zr#=object position z(a)
 
         rem get forces
         if a<>rope#(num,0,0)
            dx#=object position x(a-1)-xr#
            dy#=object position y(a-1)-yr#
            dz#=object position z(a-1)-zr#
            dist#=sqrt((dx#*dx#)+(dy#*dy#)+(dz#*dz#))
            F#=(dist#-rope#(num,0,3))/rope#(num,0,2)
            if dist#<>0.0
               Fx#=Fx#+((dx#/dist#)*F#)
               Fy#=Fy#+((dy#/dist#)*F#)
               Fx#=Fx#+((dz#/dist#)*F#)
            endif
         endif
 
         if a<>rope#(num,0,1)
            dx#=object position x(a+1)-xr#
            dy#=object position y(a+1)-yr#
            dz#=object position z(a+1)-zr#
            dist#=sqrt((dx#*dx#)+(dy#*dy#)+(dz#*dz#))
            F#=(dist#-rope#(num,0,3))/rope#(num,0,2)
            if dist#<>0.0
               Fx#=Fx#+((dx#/dist#)*F#)
               Fy#=Fy#+((dy#/dist#)*F#)
               Fx#=Fx#+((dz#/dist#)*F#)
            endif
         endif
 
         if clicked(0)=a
            dx#=mousex()-object screen x(a)
            dy#=(mousey()-object screen y(a))*-1.00
            dz#=0.0
            dist#=sqrt((dx#*dx#)+(dy#*dy#)+(dz#*dz#))
            F#=(dist#/rope#(num,0,2))*0.01
            if dist#<>0.0
               Fx#=Fx#+((dx#/dist#)*F#)
               Fy#=Fy#+((dy#/dist#)*F#)
               Fx#=Fx#+((dz#/dist#)*F#)
            endif
         endif
 
         rem friction
         if rope#(num,cur,0)<>0 then Fx#=Fx#-(rope#(num,cur,0)*rope#(num,cur,0)*airf#*(abs(rope#(num,cur,0))/rope#(num,cur,0)))
         if rope#(num,cur,1)<>0 then Fy#=Fy#-(rope#(num,cur,1)*rope#(num,cur,1)*airf#*(abs(rope#(num,cur,1))/rope#(num,cur,1)))
         if rope#(num,cur,2)<>0 then Fz#=Fz#-(rope#(num,cur,2)*rope#(num,cur,2)*airf#*(abs(rope#(num,cur,2))/rope#(num,cur,2)))
 
         ax#=Fx#/rope#(num,cur,3)
         ay#=Fy#/rope#(num,cur,3)
         az#=Fz#/rope#(num,cur,3)
 
         rem check for static joint
         if rope#(num,cur,4)=0
 
            rope#(num,cur,0)=rope#(num,cur,0)+(ax#*dt#)
            rope#(num,cur,1)=rope#(num,cur,1)+(ay#*dt#)
            rope#(num,cur,2)=rope#(num,cur,2)+(az#*dt#)
 
            position object a,object position x(a)+(rope#(num,cur,0)*dt#),object position y(a)+(rope#(num,cur,1)*dt#),object position z(a)+(rope#(num,cur,2)*dt#)
 
         endif
 
      next a
 
   next q
 
endfunction
 
 
 
rem MAKE ROPE FUNCTION
 
function makerope(num,bo,eo,elasticity#,mass#)
   rope#(num,0,0)=bo
   rope#(num,0,1)=eo
   rope#(num,0,2)=elasticity#
   x1#=object position x(bo) : y1#=object position y(bo) : z1#=object position z(bo)
   x2#=object position x(bo+1) : y2#=object position y(bo+1) : z2#=object position z(bo+1)
   rope#(num,0,3)=sqrt(((x1#-x2#)*(x1#-x2#))+((y1#-y2#)*(y1#-y2#))+((z1#-z2#)*(z1#-z2#)))
   for a=1 to (eo-bo)+1
      rope#(num,a,3)=mass#
   next a
endfunction