To adjust speed change time step. Change dt to 0.14 to slow down or 0.18 to speed up.

Inspired by: https://www.youtube.com/watch?v=DhFHAEQ5x4c

Code: Select all

```
' version 1 with speed regulator
' three swinging sticks kinetic energy by neil
Screenres 640, 640, 32
' change dt to 0.14 to slow down or 0.18 to speed up
Const As Double dt = 0.16 'time step
Const g = 9.81 ' acceleration due to gravity
Const length = 290 ' length of sticks
Dim As Double angle1 = 3, angle2 = 2.8, angle3 = -1.9
Dim As Double vel1 = 0.02, vel2 = 0.02, vel3 = 0.02
Dim As Double accel1, accel2, accel3
Dim As Long fps
Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
Static As Double timervalue,_lastsleeptime,t3,frames
frames+=1
If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
Var sleeptime=_lastsleeptime+((1/myfps)-Timer+timervalue)*1000
If sleeptime<1 Then sleeptime=1
_lastsleeptime=sleeptime
timervalue=Timer
Return sleeptime
End Function
Do
ScreenLock
Cls
' Calculate acceleration
accel1 = -g / length * Sin(angle1)
accel2 = -g / length * Sin(angle2)
accel3 = -g / length * Sin(angle3)
' Update velocities
vel1 += accel1 * dt
vel2 += accel2 * dt
vel3 += accel3 * dt
' Update angles
angle1 += vel1 * dt
angle2 += vel2 * dt
angle3 += vel3 * dt
' circle for pivot
Circle(320,320),10,RGB(0 ,255, 255)
' Draw sticks
Line (320, 320)-(320 + length * Sin(angle1), 320 + length * Cos(angle1)), RGB(255, 0, 255)
Circle(320 + length * Sin(angle1), 320 + length * Cos(angle1)), 20, RGB(255, 0, 255),,,,F
Line (320, 320)-(320 + length * Sin(angle2), 320 + length * Cos(angle2)), RGB(255, 255, 0)
Circle(320 + length * Sin(angle2), 320 + length * Cos(angle2)), 20, RGB(255, 255, 0),,,,F
Line (320, 320)-(320 + length * Sin(angle3), 320 + length * Cos(angle3)), RGB(0, 255, 0)
Circle(320 + length * Sin(angle3), 320 + length * Cos(angle3)), 20, RGB(0, 255, 0),,,,F
ScreenUnlock
Sleep regulate(60,fps),1
Loop Until len(Inkey)
End
```