Three swinging sticks kinetic energy

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
neil
Posts: 587
Joined: Mar 17, 2022 23:26

Three swinging sticks kinetic energy

Post by neil »

I am trying to simulate the effect of a kinetic energy sculpture.
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
Last edited by neil on Apr 08, 2024 3:47, edited 7 times in total.
neil
Posts: 587
Joined: Mar 17, 2022 23:26

Re: Three swinging sticks kinetic energy

Post by neil »

The code has been updated. I added a bob to all of the swinging sticks.
Last edited by neil on Apr 07, 2024 19:43, edited 3 times in total.
Lothar Schirm
Posts: 437
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Three swinging sticks kinetic energy

Post by Lothar Schirm »

Very nice! Some time ago I wrote nearly the same animation, only for one pendulum, solving the differential equation of motion by using the Runge-Kutta method, but your simple solution has a very clear code and is easy to understand (as well as your simulation of gravitation in an other post recently). I like your animations!
neil
Posts: 587
Joined: Mar 17, 2022 23:26

Re: Three swinging sticks kinetic energy

Post by neil »

This new version 3 has eight swinging pendulums using arrays.
You can now fine-tune the speed by adjusting the time step.
The default value for dt is 0.16. Change dt to 0.14 to slow down or 0.18 to speed up.

Code: Select all

' version 3
' eight swinging sticks kinetic energy using arrays by neil

Const W As Integer = 640
Const H As Integer = 640
Const As Double g = 9.81  ' acceleration due to gravity

' change dt to 0.14 to slow down or 0.18 to speed up
Const As Double dt = 0.16 ' time step

ScreenRes W, H, 32
Dim As Double x1, x2, y1, y2, b
Dim As Double L = 290 'length of sticks

Dim As Integer colr(8), i
Dim As Double accel(8),angle(8),vel(8)
Dim As Long fps

x1 = W / 2
y1 = H / 2

' put starting velocities in an array
for i = 0 to 7
vel(i) = 0.02
next

' this sets the starting positions of pendulums
' regular numbers positions them on the right side of the screen
' negative numbers positions them on the left side of the screen

' right side
angle(0) = 3
angle(1) = 2.6
angle(2) = 2.2
angle(3) = 1.9

'left side
angle(4) = -2.9
angle(5) = -2.5
angle(6) = -2.1
angle(7) = -1.8

' colors stored in an array
colr(0) = Rgb(&HFF, &HFF, &H00)
colr(1) = Rgb(&H4B, &HB0, &HFF)
colr(2) = Rgb(&HFF, &HB3, &H00)
colr(3) = Rgb(&HFF, &H00, &H00)
colr(4) = Rgb(&HFF, &H00, &HFF)
colr(5) = Rgb(&H00, &HFF, &H00)
colr(6) = Rgb(&HFF, &HFF, &HFF)
colr(7) = Rgb(&H00, &HFF, &HFF)

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
    For i As Integer = 0 To 7
      
      ' Calculate acceleration
        accel(i) = -g / L * Sin(angle(i))
      
      ' Update velocities
        vel(i) += accel(i) * dt
        
      ' Update angles  
        angle(i) += vel(i) * dt
  
      ' Circle for pivot
        Circle(x1,y1),10,colr(7),,,,F
               
      ' update stick positions
        x2 = x1 + L * Sin(angle(i))
        y2 = y1 + L * Cos(angle(i))
      
      ' Draw sticks
        Line(x1, y1) - (x2, y2), colr(i)
        Circle(x2, y2), 20, colr(i),,,,F
    Next
    ScreenUnlock
    Sleep regulate(60,fps),1

Loop Until len(inkey)
Last edited by neil on Apr 08, 2024 3:49, edited 7 times in total.
neil
Posts: 587
Joined: Mar 17, 2022 23:26

Re: Three swinging sticks kinetic energy

Post by neil »

The six swinging sticks version 2 code has been updated. I made adjustments to the angles.
neil
Posts: 587
Joined: Mar 17, 2022 23:26

Re: Three swinging sticks kinetic energy

Post by neil »

I updated both swinging stick versions. I added dodicat's speed regulator.
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Three swinging sticks kinetic energy

Post by Roland Chastain »

Nice!

Here is a Cairo version.

Code: Select all

' https://www.freebasic.net/forum/viewtopic.php?p=302813#p302813
' Eight swinging sticks kinetic energy using arrays by neil
' Cairo version

#include once "cairo/cairo.bi"

const W as integer = 640
const H as integer = 640
const as double g = 9.81 ' acceleration due to gravity
const as double dt = 0.16 ' time step

screenres W, H, 32
dim as double x1, x2, y1, y2, b
dim as double L = 290 ' length of sticks

dim as integer colr(8), i
dim as double accel(8), angle(8), vel(8)
dim as long fps

x1 = W / 2
y1 = H / 2

' put starting velocities in an array
for i = 0 to 7
  vel(i) = 0.02
next

' this sets the starting positions of pendulums
' regular numbers positions them on the right side of the screen
' negative numbers positions them on the left side of the screen

' right side
angle(0) = 3
angle(1) = 2.6
angle(2) = 2.2
angle(3) = 1.9

'left side
angle(4) = -2.9
angle(5) = -2.5
angle(6) = -2.1
angle(7) = -1.8

for i = 0 to 7
  angle(i) = -3
next i

' colors stored in an array
colr(0) = rgb(&HFF, &HFF, &H00)
colr(1) = rgb(&H4B, &HB0, &HFF)
colr(2) = rgb(&HFF, &HB3, &H00)
colr(3) = rgb(&HFF, &H00, &H00)
colr(4) = rgb(&HFF, &H00, &HFF)
colr(5) = rgb(&H00, &HFF, &H00)
colr(6) = rgb(&HFF, &HFF, &HFF)
colr(7) = rgb(&H00, &HFF, &HFF)

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

dim as any ptr image = imagecreate(W, H)
dim as any ptr pixels

imageinfo(image, W, H,,, pixels)

dim as long stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, W) ' https://www.freebasic.net/forum/viewtopic.php?p=215065#p215065
dim as cairo_surface_t ptr cs = cairo_image_surface_create_for_data(pixels, CAIRO_FORMAT_ARGB32, W, H, stride)

dim as cairo_t ptr cr = cairo_create(cs)

const PI = 3.1415926535897932

#define CAIRO_COLOR(c) (c and &hFF0000) / &hFF0000, (c and &h00FF00) / &h00FF00, (c and &h0000FF) / &h0000FF

dim as integer skip = 7
dim as integer count = 0

do
' Paint image black
  cairo_set_source_rgb(cr, 0, 0, 0)
  cairo_paint(cr)
  
  for i as integer = 0 to 7 - skip
    
  ' Calculate acceleration
    accel(i) = -g / L * sin(angle(i))
  
  ' Update velocities
    vel(i) += accel(i) * dt
    
  ' Update angles  
    angle(i) += vel(i) * dt
  next
  
  for i as integer = 0 to 7
  ' Update stick positions
    x2 = x1 + L * sin(angle(i))
    y2 = y1 + L * cos(angle(i))
  
  ' Draw sticks
    cairo_set_source_rgba(cr, CAIRO_COLOR(colr(i)), 0.5)
    cairo_move_to(cr, x1, y1)
    cairo_line_to(cr, x2, y2)
    cairo_stroke(cr)
    cairo_arc(cr, x2, y2, 20, 0, 2 * PI)
    cairo_fill(cr)
  next
  
' Circle for pivot
  cairo_arc(cr, x1, y1, 10, 0, 2 * PI)
  cairo_set_source_rgba(cr, 0.5, 0.5, 0.5, 0.5)
  cairo_fill(cr)
  
  screenlock
  put (0, 0), image, pset
  screenunlock
  
  sleep regulate(60, fps), 1
  
  count += 1
  if count = 6 then
    if skip > 0 then
      skip -= 1
    end if
    count = 0
  end if
loop until len(inkey)

cairo_destroy(cr)
cairo_surface_destroy(cs)

imagedestroy image

sleep
Maybe we could use arrays, to avoid repetition of the same code?
Last edited by Roland Chastain on Apr 06, 2024 19:19, edited 3 times in total.
neil
Posts: 587
Joined: Mar 17, 2022 23:26

Re: Three swinging sticks kinetic energy

Post by neil »

@Roland Chastain

With my latest update, version 3, using arrays, maybe you could make a Cairo version.
Last edited by neil on Apr 06, 2024 5:56, edited 1 time in total.
neil
Posts: 587
Joined: Mar 17, 2022 23:26

Re: Three swinging sticks kinetic energy

Post by neil »

I just updated my code again from six swinging sticks to eight swinging sticks, version 3. It is now using arrays.
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Three swinging sticks kinetic energy

Post by Roland Chastain »

neil wrote: Apr 04, 2024 22:40 With my latest update, version 3, using arrays, maybe you could make a Cairo version.
Thanks! I will look at it.
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Three swinging sticks kinetic energy

Post by Roland Chastain »

Yes, the code is better like this.

Updated the Cairo version.

I used transparent colors, to make more obvious the advantage of using Cairo.

I also tried another way to initialize angles. All pendulums start are at the same position, but they are releashed one after the other. Just comment out my code to restore the original behaviour.
neil
Posts: 587
Joined: Mar 17, 2022 23:26

Re: Three swinging sticks kinetic energy

Post by neil »

@Roland Chastain
Well done. Thank you!

Here's the Cairo version with 64 pendulums.
I used two for loops for the angle starting positions.

Code: Select all

' https://www.freebasic.net/forum/viewtopic.php?p=302813#p302813
' 64 swinging sticks kinetic energy using arrays by neil
' Cairo version

#include once "cairo/cairo.bi"

const W as integer = 640
const H as integer = 640
const as double g = 9.81 ' acceleration due to gravity
const as double dt = 0.18 ' time step

screenres W, H, 32
dim as double x1, x2, y1, y2, n
dim as double L = 290 ' length of sticks
dim as integer c1, c2, c3, c4, c5, c6 ,c7, c8, i
dim as integer colr(64)
dim as double accel(64), angle(64), vel(64)
dim as ubyte c
dim as long fps


x1 = W / 2
y1 = H / 2

' put starting velocities in an array
for i = 0 to 63
  vel(i) = 0.02
next

' this sets the starting positions of pendulums
' regular numbers positions them on the right side of the screen
' negative numbers positions them on the left side of the screen

n = 3
for i = 0 to 31
  angle(i) = n
  n -= 0.02
next i

n = -3
for i = 32 to 63
  angle(i) = n
  n += 0.04
next i

' colors assiged to a variable
c1 = rgb(&HFF, &HFF, &H00)
c2 = rgb(&H4B, &HB0, &HFF)
c3 = rgb(&HFF, &HB3, &H00)
c4 = rgb(&HFF, &H00, &H00)
c5 = rgb(&HFF, &H00, &HFF)
c6 = rgb(&H00, &HFF, &H00)
c7 = rgb(&HFF, &HFF, &HFF)
c8 = rgb(&H00, &HFF, &HFF)

' 64 colors stored in an array
' 8 of each are duplicates
c = 0
for i = 1 to 8
colr(c) = c1
colr(c + 1) = c2
colr(c + 2) = c3
colr(c + 3) = c4
colr(c + 4) = c5
colr(c + 5) = c6
colr(c + 6) = c7
colr(c + 7) = c8
c += 8
next i

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

dim as any ptr image = imagecreate(W, H)
dim as any ptr pixels

imageinfo(image, W, H,,, pixels)

dim as long stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, W) ' https://www.freebasic.net/forum/viewtopic.php?p=215065#p215065
dim as cairo_surface_t ptr cs = cairo_image_surface_create_for_data(pixels, CAIRO_FORMAT_ARGB32, W, H, stride)

dim as cairo_t ptr cr = cairo_create(cs)

const PI = 3.1415926535897932

#define CAIRO_COLOR(c) (c and &hFF0000) / &hFF0000, (c and &h00FF00) / &h00FF00, (c and &h0000FF) / &h0000FF

do
' Paint image black
  cairo_set_source_rgb(cr, 0, 0, 0)
  cairo_paint(cr)
  
  for i as integer = 0 to 63
    
  ' Calculate acceleration
    accel(i) = -g / L * sin(angle(i))
  
  ' Update velocities
    vel(i) += accel(i) * dt
    
  ' Update angles  
    angle(i) += vel(i) * dt
  
  ' Update stick positions
    x2 = x1 + L * sin(angle(i))
    y2 = y1 + L * cos(angle(i))
  
  ' Draw sticks
    cairo_set_source_rgba(cr, CAIRO_COLOR(colr(i)), 0.5)
    cairo_move_to(cr, x1, y1)
    cairo_line_to(cr, x2, y2)
    cairo_stroke(cr)
    cairo_arc(cr, x2, y2, 20, 0, 2 * PI)
    cairo_fill(cr)
  next
  
' Circle for pivot
  cairo_arc(cr, x1, y1, 18, 0, 2 * PI)
  cairo_set_source_rgba(cr, 0.5, 0.5, 0.5, 0.5)
  cairo_fill(cr)
  
  screenlock
  put (0, 0), image, pset
  screenunlock

  sleep regulate(60, fps), 1
 loop until len(inkey)

cairo_destroy(cr)
cairo_surface_destroy(cs)

imagedestroy image

sleep
Last edited by neil on Apr 08, 2024 0:59, edited 3 times in total.
neil
Posts: 587
Joined: Mar 17, 2022 23:26

Re: Three swinging sticks kinetic energy

Post by neil »

I posted another update for the Cairo version; it now has 64 pendulums.
neil
Posts: 587
Joined: Mar 17, 2022 23:26

Re: Three swinging sticks kinetic energy

Post by neil »

Here's a Cairo 128 circles worm effect.
Change these settings for a larger graphics screen.
const W as integer = 800
const H as integer = 800
const as double L = 360

For a different effect, change
angle (i) = -3 to angle (i) = -2
dt = 0.26 to dt = 0.12

Code: Select all

' https://www.freebasic.net/forum/viewtopic.php?p=302813#p302813
' 128 circles worm effect using arrays by neil
' Cairo version

#include once "cairo/cairo.bi"

const W as integer = 640
const H as integer = 640
const as double L = 290 ' length of sticks

const as double g = 9.81 ' acceleration due to gravity
const as double dt = 0.26 ' time step

screenres W, H, 32
dim as double x1, x2, y1, y2, n

dim as integer c1, c2, c3, c4, c5, c6 ,c7, c8, i

dim as integer colr(128)
dim as double accel(128), angle(128), vel(128)
dim as ubyte c
dim as long fps

x1 = W / 2
y1 = H / 2

' put starting velocities in an array
for i = 0 to 127
  vel(i) = 0.02
next

' this sets the starting positions of pendulums
' regular numbers positions them on the right side of the screen
' negative numbers positions them on the left side of the screen

for i = 0 to 127
  angle(i) = -3
next i

' colors assiged to a variable
c1 = rgb(&HFF, &HFF, &H00)
c2 = rgb(&H4B, &HB0, &HFF)
c3 = rgb(&HFF, &HB3, &H00)
c4 = rgb(&HFF, &H00, &H00)
c5 = rgb(&HFF, &H00, &HFF)
c6 = rgb(&H00, &HFF, &H00)
c7 = rgb(&HFF, &HFF, &HFF)
c8 = rgb(&H00, &HFF, &HFF)

' 128 colors stored in an array
' 16 of each are duplicates
c = 0
for i = 1 to 16
colr(c) = c1
colr(c + 1) = c2
colr(c + 2) = c3
colr(c + 3) = c4
colr(c + 4) = c5
colr(c + 5) = c6
colr(c + 6) = c7
colr(c + 7) = c8
c += 8
next i

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

dim as any ptr image = imagecreate(W, H)
dim as any ptr pixels

imageinfo(image, W, H,,, pixels)

dim as long stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, W) ' https://www.freebasic.net/forum/viewtopic.php?p=215065#p215065
dim as cairo_surface_t ptr cs = cairo_image_surface_create_for_data(pixels, CAIRO_FORMAT_ARGB32, W, H, stride)

dim as cairo_t ptr cr = cairo_create(cs)

const PI = 3.1415926535897932

#define CAIRO_COLOR(c) (c and &hFF0000) / &hFF0000, (c and &h00FF00) / &h00FF00, (c and &h0000FF) / &h0000FF

dim as integer skip = 127
dim as integer count = 0

do
' Paint image black
  cairo_set_source_rgb(cr, 0, 0, 0)
  cairo_paint(cr)
  
  for i as integer = 0 to 127 - skip
    
  ' Calculate acceleration
    accel(i) = -g / L * sin(angle(i))
  
  ' Update velocities
    vel(i) += accel(i) * dt
    
  ' Update angles  
    angle(i) += vel(i) * dt
  next
  
  for i as integer = 0 to 127
  ' Update stick positions
    x2 = x1 + L * sin(angle(i))
    y2 = y1 + L * cos(angle(i))
  
  ' Draw sticks
    cairo_set_source_rgba(cr, CAIRO_COLOR(colr(i)), 0.5)
  ' cairo_move_to(cr, x1, y1)
  ' cairo_line_to(cr, x2, y2)
  ' cairo_stroke(cr)
    cairo_arc(cr, x2, y2, 20, 0, 2 * PI)
    cairo_fill(cr)
  next
  
'  Circle for pivot
'  cairo_arc(cr, x1, y1, 18, 0, 2 * PI)
'  cairo_set_source_rgba(cr, 0.5, 0.5, 0.5, 0.5)
'  cairo_fill(cr)
  
  screenlock
  put (0, 0), image, pset
  screenunlock
  
  sleep regulate(60, fps), 1
  
  count += 1
  if count = 1 then
    if skip > 0 then
       skip -= 1
    end if
    count = 0
  end if
loop until len(inkey)

cairo_destroy(cr)
cairo_surface_destroy(cs)

imagedestroy image

sleep
neil
Posts: 587
Joined: Mar 17, 2022 23:26

Re: Three swinging sticks kinetic energy

Post by neil »

Here's a FreeBasic graphics version of 64 swinging pendulums.

Code: Select all

' version 4
' 64 swinging sticks kinetic energy using arrays by neil

Const W As Integer = 640
Const H As Integer = 640
Dim as Integer c1, c2, c3, c4, c5, c6 ,c7, c8, i
Const As Double g = 9.81  ' acceleration due to gravity

' change dt to 0.16 to slow down or 0.20 to speed up
Const As Double dt = 0.18 ' time step

ScreenRes W, H, 32
Dim As Double n, x1, x2, y1, y2
Dim As Double L = 290 'length of sticks

Dim As Integer colr(64)
Dim As Double accel(64),angle(64),vel(64)
dim as ubyte c
Dim As Long fps

x1 = W / 2
y1 = H / 2

' put starting velocities in an array
for i = 0 to 63
vel(i) = 0.02
next

' this sets the starting positions of pendulums
' regular numbers positions them on the right side of the screen
' negative numbers positions them on the left side of the screen

' 32 right side starting positions
n = 3
for i = 0 to 31
  angle(i) = n
  n -= 0.02
next i

' 32 left side starting positions
n = -3
for i = 32 to 63
  angle(i) = n
  n += 0.04
next i

' colors assiged to a variable
c1 = rgb(&HFF, &HFF, &H00)
c2 = rgb(&H4B, &HB0, &HFF)
c3 = rgb(&HFF, &HB3, &H00)
c4 = rgb(&HFF, &H00, &H00)
c5 = rgb(&HFF, &H00, &HFF)
c6 = rgb(&H00, &HFF, &H00)
c7 = rgb(&HFF, &HFF, &HFF)
c8 = rgb(&H00, &HFF, &HFF)

' 64 colors stored in an array
' 8 of each are duplicates
c = 0
for i = 1 to 8
colr(c) = c1
colr(c + 1) = c2
colr(c + 2) = c3
colr(c + 3) = c4
colr(c + 4) = c5
colr(c + 5) = c6
colr(c + 6) = c7
colr(c + 7) = c8
c += 8
next i

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
    For i As Integer = 0 To 63
      
      ' Calculate acceleration
        accel(i) = -g / L * Sin(angle(i))
      
      ' Update velocities
        vel(i) += accel(i) * dt
        
      ' Update angles  
        angle(i) += vel(i) * dt
  
      ' Circle for pivot
        Circle(x1,y1),10,colr(7),,,,F
               
      ' update stick positions
        x2 = x1 + L * Sin(angle(i))
        y2 = y1 + L * Cos(angle(i))
      
      ' Draw sticks
        Line(x1, y1) - (x2, y2), colr(i)
        Circle(x2, y2), 20, colr(i),,,,F
    Next
    ScreenUnlock
    Sleep regulate(60,fps),1

Loop Until len(inkey)
Post Reply