Code: Select all

```
REM Verlet rope thing
REM Works with BB4W
*FLOAT 64
ON ERROR PROCerror( REPORT$ + " at line " + STR$ERL )
MODE 8 : OFF
WinW% = @vdu%!208
WinH% = @vdu%!212
`Sleep` = SYS("Sleep")
nPts% = 50
DIM Point{( nPts% ) x#, y#, oldx#, oldy# }
theta = 0
pointSepDist% = 5
PROCGetHeadPos( theta, x0#, y0# )
PROCInitRope( Point{()}, nPts%, x0#, y0#, pointSepDist% )
*REFRESH OFF
REPEAT
CLS
PROCGetHeadPos( theta, headX#, headY# )
theta += 0.005125
PROCUpdateRopePoints( Point{()}, nPts%, headX#, headY#, 5, 0.01, 0.9995, 1 )
GCOL 6
VDU 23,23,2|
FOR I% = 0 TO nPts%-2
LINE 2*Point{(I%)}.x#, 2*Point{(I%)}.y#, 2*Point{(I%+1)}.x#, 2*Point{(I%+1)}.y#
NEXT I%
VDU 23,23,1|
GCOL 14 : CIRCLE FILL 2*Point{(0)}.x#, 2*Point{(0)}.y#, 12
*REFRESH
SYS `Sleep`, 1
UNTIL FALSE
END
DEF PROCUpdateRopePoints( p{()}, nPts%, headX#, headY#, sepDist%, grav#, fric#, nIter% )
LOCAL I%, J%, vx#, vy#, dx#, dy#, dist#, diff#, perc#, offX#, offY#
p{(0)}.x# = headX#
p{(0)}.y# = headY#
p{(0)}.oldx# = p{(0)}.x#
p{(0)}.oldy# = p{(0)}.y#
FOR I% = 0 TO nPts%-1
vx# = (p{(I%)}.x# - p{(I%)}.oldx#) * fric#
vy# = (p{(I%)}.y# - p{(I%)}.oldy#) * fric#
p{(I%)}.oldx# = p{(I%)}.x#
p{(I%)}.oldy# = p{(I%)}.y#
p{(I%)}.x# += vx#
p{(I%)}.y# += vy#
p{(I%)}.y# -= grav#
NEXT I%
FOR J% = 1 TO nIter%
FOR I% = 0 TO nPts%-2
dx# = p{( I%+1 )}.x# - p{( I% )}.x#
dy# = p{( I%+1 )}.y# - p{( I% )}.y#
dist# = SQR(dx#*dx# + dy#*dy#)
diff# = sepDist% - dist#
perc# = 0.5 * diff# / dist#
offX# = dx# * perc#
offY# = dy# * perc#
p{( I% )}.x# -= offX#
p{( I% )}.y# -= offY#
p{( I%+1 )}.x# += offX#
p{( I%+1 )}.y# += offY#
NEXT I%
NEXT J%
dx# = p{(0)}.x# - headX#
dy# = p{(0)}.y# - headY#
FOR I% = 0 TO nPts%-1
p{(I%)}.x# -= dx#
p{(I%)}.y# -= dy#
p{(I%)}.oldx# -= dx#
p{(I%)}.oldy# -= dy#
NEXT I%
ENDPROC
DEF PROCInitRope( p{()}, nPts%, x0%, y0%, sepDist% )
LOCAL I%
FOR I% = 0 TO nPts%-1
p{(I%)}.x# = x0%
p{(I%)}.y# = y0% - I%*sepDist%
p{(I%)}.oldx# = x0%
p{(I%)}.oldy# = y0% - I%*sepDist%
NEXT I%
ENDPROC
DEF PROCGetHeadPos( theta, RETURN x#, RETURN y# )
x# = 0.5*WinW% + 0.5*WinW%*SIN(0.9251*theta)*SIN(1.2563*theta + 0.45)*SIN(0.7162*theta - 0.712)
y# = 0.6*WinH% + 0.5*WinH%*COS(0.5126*theta)*COS(0.4525*theta - 1.53)*COS(1.4267*theta + 2.123)
ENDPROC
DEF PROCerror( s$ )
OSCLI "REFRESH ON" : CLS : ON : VDU 7
PRINT '" " + s$;
REPEAT : WAIT 2 : UNTIL FALSE
ENDPROC
```

**#**suffixes - it was originally in preparation for translation to C (my next task!).