Author Topic: Double Pendulum Studies  (Read 2182 times)

0 Members and 1 Guest are viewing this topic.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
Double Pendulum Studies
« on: March 12, 2022, 03:08:58 pm »
REDACTED
« Last Edit: April 13, 2022, 09:33:56 am by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
Re: Double Pendulum Studies
« Reply #1 on: March 13, 2022, 12:42:32 pm »
it seems interesting, simple, but there are very complicated calculations behind it. Do you think it would be easy to connect additional wrists? or does it all have to be approached differently? I don't understand the "random" part, between mouse and random values. What does that do?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Double Pendulum Studies
« Reply #2 on: March 13, 2022, 01:04:26 pm »
Quote
Do you think it would be easy to connect additional wrists?

Yes we did this already with clocks each arm extended off the the arm before and even a 10th's of a sec arm extended off the sec arm extended off the minute arm extended off the hours hand. New times!

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
Re: Double Pendulum Studies
« Reply #3 on: March 13, 2022, 01:46:31 pm »
oh !! where can i see ?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Double Pendulum Studies
« Reply #4 on: March 13, 2022, 02:13:10 pm »
Apologies to STx if this is hi-jacking but it was your idea to begin with so... I don't feel too bad :-))
Damned if I can remember the topic or useful keyword for search... oh parametric clock might have dun it, too late!

Code: QB64: [Select]
  1. _Title "Jointed 4 arms clock #6: Any changes color, digits change hours: 0 = 12 or click mouse at 1, 2, 3... o'clock" 'b+ started 2020-11-22
  2. ' inspsired by STx Parametric clock specially the faces  https://www.qb64.org/forum/index.php?topic=3277.msg125579#msg125579
  3. ' I wish to see what a large circle joint at center would look like, first can I get similar face? yes sorta
  4. ' 2020-11-23 More work on clock face, less LOC for drawPully, add modes and color changes
  5. ' 2020-11-24 add stuff to make different arms
  6. ' 2020-11-25  OK 4 armed clocks
  7.  
  8. Const xmax = 710, ymax = 710, CX = xmax / 2, CY = ymax / 2, hhr0 = 20, hhr1 = 10, mhr1 = 5, shr1 = 3, thr = 0, hh = 180, mh = 110, sh = 36, th = 12
  9. Dim Shared face As Long, mode As Long, colr As _Unsigned Long, hourHand&, minHand&, secHand&, tenthsHand&
  10.  
  11. Screen _NewImage(xmax, ymax, 32)
  12. _Delay .25
  13.  
  14. Dim k$, a, t As Double, h, m, s, tenths, hha, mha, sha, tha, hhx, hhy, mhx, mhy, shx, shy, thx, thy
  15. face = _NewImage(_Width, _Height, 32)
  16. makeAFace
  17.     k$ = InKey$
  18.     If Len(k$) Then
  19.         If InStr("0123456789", k$) Then mode = Val(k$)
  20.         makeAFace
  21.     End If
  22.         a = _R2D(_Atan2(_MouseY - CY, _MouseX - CX)) + 90 + 15
  23.         If a < 0 Then a = a + 360
  24.         If a > 360 Then a = a - 360
  25.         a = Int(a / 30)
  26.         If a >= 0 And a <= 12 Then mode = a: makeAFace
  27.     End If
  28.     _PutImage , face&, 0
  29.     t = Timer(.001)
  30.     h = t / 3600 ' fix this for mode
  31.     If h > 12 Then h = h - 12
  32.     m = (h - Int(h)) * 60
  33.     s = t Mod 60
  34.     tenths = Int((t - Int(t)) * 10)
  35.     hha = h / mode * _Pi(2) - _Pi(.5)
  36.     mha = m / 60 * _Pi(2) - _Pi(.5)
  37.     sha = s / 60 * _Pi(2) - _Pi(.5)
  38.     tha = tenths / 10 * _Pi(2) - _Pi(.5)
  39.     hhx = CX + hh * Cos(hha): hhy = CY + hh * Sin(hha)
  40.     mhx = hhx + mh * Cos(mha): mhy = hhy + mh * Sin(mha)
  41.     shx = mhx + sh * Cos(sha): shy = mhy + sh * Sin(sha)
  42.     thx = shx + .35 * th * Cos(tha): thy = shy + .35 * th * Sin(tha) ' why so far away? move in .4 ???
  43.     RotoZoom3 CX, CY, hourHand&, 1, 1, hha
  44.     RotoZoom3 hhx, hhy, minHand&, 1, 1, mha
  45.     RotoZoom3 mhx, mhy, secHand&, 1, 1, sha
  46.     RotoZoom3 thx, thy, tenthsHand&, 1, 1, tha
  47.     _Display
  48.     _Limit 30
  49.  
  50. Sub drawPully (x1, y1, r1, x2, y2, r2, c As _Unsigned Long)
  51.     Dim a
  52.     a = _Atan2(y2 - y1, x2 - x1) + _Pi(.5)
  53.     Line (x1 + r1 * Cos(a), y1 + r1 * Sin(a))-(x2 + r2 * Cos(a), y2 + r2 * Sin(a)), c
  54.     Line (x1 + r1 * Cos(a - _Pi), y1 + r1 * Sin(a - _Pi))-(x2 + r2 * Cos(a - _Pi), y2 + r2 * Sin(a - _Pi)), c
  55.     Circle (x1, y1), r1, c
  56.     Circle (x2, y2), r2, c
  57.  
  58. Sub makeAFace
  59.     Dim cColr As _Unsigned Long, r, g, b, a, vi, h, hha, mha, sha, hhx, hhy, mhx, mhy, shx, shy, t, tha, thx, thy
  60.     colr = _RGB32((Rnd < .5) * -1 * (Rnd * 128 + 127), Rnd * 128 + 127, (Rnd < .5) * -1 * (Rnd * 128 + 127), &H23)
  61.     cAnalysis colr, r, g, b, a
  62.     cColr = _RGB32(255 - r, 255 - g, 255 - b, 2)
  63.     If mode = 0 Then mode = 12
  64.     Cls
  65.     For vi = 1 To mode * 3600
  66.         h = vi / 3600
  67.         hha = h / mode * _Pi(2) - _Pi(.5)
  68.         mha = (h - Int(h)) * _Pi(2) - _Pi(.5)
  69.         sha = (vi Mod 60) / 60 * _Pi(2) - _Pi(.5)
  70.         hhx = CX + hh * Cos(hha): hhy = CY + hh * Sin(hha)
  71.         mhx = hhx + mh * Cos(mha): mhy = hhy + mh * Sin(mha)
  72.         shx = mhx + sh * Cos(sha): shy = mhy + sh * Sin(sha)
  73.         drawPully mhx, mhy, mhr1, shx, shy, shr1, colr
  74.         For t = 0 To 9
  75.             tha = t / 10 * _Pi(2) - _Pi(.5)
  76.             thx = shx + th * Cos(tha): thy = shy + th * Sin(tha)
  77.             drawPully shx, shy, shr1, thx, thy, thr, cColr
  78.         Next
  79.     Next
  80.     _PutImage , 0, face
  81.     'arms look better with the draw color for the face on the edges, it hides raggity border edges.
  82.     ' otherwise we could just draw these once at the beginning of program.
  83.     makeArmImage hourHand&, hh, hhr0, hhr1, &HFFFFFFFF, &H88000000
  84.     makeArmImage minHand&, mh, hhr1, mhr1, &HFFFFFFFF, &H88000000
  85.     makeArmImage secHand&, sh, mhr1, shr1, &HFFFFFFFF, &H88000000
  86.     makeArmImage tenthsHand&, th, shr1, thr, &HFFFFFFFF, &H88000000
  87.  
  88. Sub makeArmImage (hdl&, length, r1, r2, c1 As _Unsigned Long, c2 As _Unsigned Long)
  89.     ' intend to use this with rotozoom so have to make image rotate-able in middle
  90.     ' arm image starts big in middle and points right to smaller radius r2
  91.     ' hdl& image handle to use
  92.     ' length  run of origins of half circles
  93.     ' c1 is color on left in middle = bigger joint , c2 is color on right
  94.     Dim width, height, wd2, hd2, x1, y1, x2, y2, a
  95.     width = 2 * (r2 + length) + 2: height = 2 * r1 + 2: wd2 = width / 2: hd2 = height / 2
  96.     hdl& = _NewImage(width + 2, height + 2, 32)
  97.     _Dest hdl&
  98.     _Source hdl&
  99.     x1 = wd2: y1 = hd2: x2 = wd2 + length: y2 = hd2: a = _Pi(.5)
  100.     Line (x1 + r1 * Cos(a), y1 + r1 * Sin(a))-(x2 + r2 * Cos(a), y2 + r2 * Sin(a)), colr
  101.     Line (x1 + r1 * Cos(a - _Pi), y1 + r1 * Sin(a - _Pi))-(x2 + r2 * Cos(a - _Pi), y2 + r2 * Sin(a - _Pi)), colr
  102.     arc x1, y1, r1, _Pi(.5), _Pi(1.5), colr
  103.     arc x2, y2, r2, _Pi(1.5), _Pi(.5), colr
  104.     paint4 x1, y1, c1, c2
  105.     _Dest 0
  106.     _Source 0
  107.  
  108. 'use radians
  109. Sub arc (x, y, r, raBegin, raEnd, c As _Unsigned Long) ' updated 2020-11-24
  110.     ' raStart is first angle clockwise from due East = 0 degrees
  111.     ' arc will start drawing there and clockwise until raStop angle reached
  112.     'x, y origin, r = radius, c = color
  113.     Dim raStart, raStop, dStart, dStop, al, a, lastx, lasty
  114.  
  115.     ' Last time I tried to use this SUB it hung the program, possible causes:
  116.     ' Make sure raStart and raStop are between 0 and 2pi.
  117.     ' This sub does not have to be recursive, use GOSUB to do drawing to execute arc in one call.
  118.  
  119.     'make copies before changing
  120.     raStart = raBegin: raStop = raEnd
  121.     While raStart < 0: raStart = raStart + _Pi(2): Wend
  122.     While raStart >= _Pi(2): raStart = raStart - _Pi(2): Wend
  123.     While raStop < 0: raStop = raStop + _Pi(2): Wend
  124.     While raStop >= _Pi(2): raStop = raStop - _Pi(2): Wend
  125.  
  126.     If raStop < raStart Then
  127.         dStart = raStart: dStop = _Pi(2) - .00001
  128.         GoSub drawArc
  129.         dStart = 0: dStop = raStop
  130.         GoSub drawArc
  131.     Else
  132.         dStart = raStart: dStop = raStop
  133.         GoSub drawArc
  134.     End If
  135.     Exit Sub
  136.     drawArc: ' I am going back to lines instead of pset
  137.     al = 2 * _Pi * r * (dStop - dStart) / _Pi(2)
  138.     For a = dStart To dStop Step 1 / al
  139.         If a > dStart Then Line (lastx, lasty)-(x + r * Cos(a), y + r * Sin(a)), c
  140.         lastx = x + r * Cos(a): lasty = y + r * Sin(a)
  141.     Next
  142.     Return
  143.  
  144. Sub cAnalysis (c As _Unsigned Long, outRed, outGrn, outBlu, outAlp)
  145.     outRed = _Red32(c): outGrn = _Green32(c): outBlu = _Blue32(c): outAlp = _Alpha32(c)
  146.  
  147. Function Ink~& (c1 As _Unsigned Long, c2 As _Unsigned Long, fr##)
  148.     Dim R1, G1, B1, A1, R2, G2, B2, A2
  149.     cAnalysis c1, R1, G1, B1, A1
  150.     cAnalysis c2, R2, G2, B2, A2
  151.     Ink~& = _RGB32(R1 + (R2 - R1) * fr##, G1 + (G2 - G1) * fr##, B1 + (B2 - B1) * fr##, A1 + (A2 - A1) * fr##)
  152.  
  153. Sub paint4 (x0, y0, c1 As _Unsigned Long, c2 As _Unsigned Long) ' needs max, min functions
  154.     Dim fillColor As _Unsigned Long, W, H, parentF, tick, ystart, ystop, xstart, xstop, x, y
  155.     fillColor = Point(x0, y0)
  156.     'PRINT fillColor
  157.     W = _Width - 1: H = _Height - 1
  158.     Dim temp(W, H)
  159.     temp(x0, y0) = 1: parentF = 1
  160.     PSet (x0, y0), Ink~&(c1, c2, Abs((y0 - _Height / 2) / (_Height / 2)))
  161.     While parentF = 1
  162.         parentF = 0: tick = tick + 1
  163.         ystart = max(y0 - tick, 0): ystop = min(y0 + tick, H)
  164.         y = ystart
  165.         While y <= ystop
  166.             xstart = max(x0 - tick, 0): xstop = min(x0 + tick, W)
  167.             x = xstart
  168.             While x <= xstop
  169.                 If Point(x, y) = fillColor And temp(x, y) = 0 Then
  170.                     If temp(max(0, x - 1), y) Then
  171.                         temp(x, y) = 1: parentF = 1: PSet (x, y), Ink~&(c1, c2, Abs((y - _Height / 2) / (_Height / 2)))
  172.                     ElseIf temp(min(x + 1, W), y) Then
  173.                         temp(x, y) = 1: parentF = 1: PSet (x, y), Ink~&(c1, c2, Abs((y - _Height / 2) / (_Height / 2)))
  174.                     ElseIf temp(x, max(y - 1, 0)) Then
  175.                         temp(x, y) = 1: parentF = 1: PSet (x, y), Ink~&(c1, c2, Abs((y - _Height / 2) / (_Height / 2)))
  176.                     ElseIf temp(x, min(y + 1, H)) Then
  177.                         temp(x, y) = 1: parentF = 1: PSet (x, y), Ink~&(c1, c2, Abs((y - _Height / 2) / (_Height / 2)))
  178.                     End If
  179.                 End If
  180.                 x = x + 1
  181.             Wend
  182.             y = y + 1
  183.         Wend
  184.     Wend
  185.  
  186. Function min (n1, n2)
  187.     If n1 > n2 Then min = n2 Else min = n1
  188.  
  189. Function max (n1, n2)
  190.     If n1 < n2 Then max = n2 Else max = n1
  191.  
  192. ' Description:
  193. ' Started from a mod of Galleon's in Wiki that both scales and rotates an image.
  194. ' This version scales the x-axis and y-axis independently allowing rotations of image just by changing X or Y Scales
  195. ' making this tightly coded routine a very powerful and versatile image tool.
  196. Sub RotoZoom3 (X As Long, Y As Long, Image As Long, xScale As Single, yScale As Single, radianRotation As Single)
  197.     ' This assumes you have set your drawing location with _DEST or default to screen.
  198.     ' X, Y - is where you want to put the middle of the image
  199.     ' Image - is the handle assigned with _LOADIMAGE
  200.     ' xScale, yScale - are shrinkage < 1 or magnification > 1 on the given axis, 1 just uses image size.
  201.     ' These are multipliers so .5 will create image .5 size on given axis and 2 for twice image size.
  202.     ' radianRotation is the Angle in Radian units to rotate the image
  203.     ' note: Radian units for rotation because it matches angle units of other Basic Trig functions
  204.     '       and saves a little time converting from degree.
  205.     '       Use the _D2R() function if you prefer to work in degree units for angles.
  206.  
  207.     Dim px(3) As Single: Dim py(3) As Single ' simple arrays for x, y to hold the 4 corners of image
  208.     Dim W&, H&, sinr!, cosr!, i&, x2&, y2& '   variables for image manipulation
  209.     W& = _Width(Image&): H& = _Height(Image&)
  210.     px(0) = -W& / 2: py(0) = -H& / 2 'left top corner
  211.     px(1) = -W& / 2: py(1) = H& / 2 ' left bottom corner
  212.     px(2) = W& / 2: py(2) = H& / 2 '  right bottom
  213.     px(3) = W& / 2: py(3) = -H& / 2 ' right top
  214.     sinr! = Sin(-radianRotation): cosr! = Cos(-radianRotation) ' rotation helpers
  215.     For i& = 0 To 3 ' calc new point locations with rotation and zoom
  216.         x2& = xScale * (px(i&) * cosr! + sinr! * py(i&)) + X: y2& = yScale * (py(i&) * cosr! - px(i&) * sinr!) + Y
  217.         px(i&) = x2&: py(i&) = y2&
  218.     Next
  219.     _MapTriangle _Seamless(0, 0)-(0, H& - 1)-(W& - 1, H& - 1), Image To(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  220.     _MapTriangle _Seamless(0, 0)-(W& - 1, 0)-(W& - 1, H& - 1), Image To(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  221.  
  222.  

Oh I have reference links right in code, how thoughtful of me!

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
Re: Double Pendulum Studies
« Reply #5 on: March 13, 2022, 02:23:24 pm »
I see, it's spectacular! But he doesn't really use physics. The above program, in turn, models the pendulum-like relationship of two masses. I don't see a connection between the two programs.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Double Pendulum Studies
« Reply #6 on: March 13, 2022, 02:26:57 pm »
I see, it's spectacular! But he doesn't really use physics. The above program, in turn, models the pendulum-like relationship of two masses. I don't see a connection between the two programs.

I don't know, do you want to see with your eyes, connected arms or see connection between application concepts, with your mind? sheesh! :)

Well the connection is the arm thing, the way they are connected. One arm or length or pendulum extends off the end of the last. I see the connection clear as that bell ringing, someone answer the phone.
« Last Edit: March 13, 2022, 02:32:13 pm by bplus »

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
Re: Double Pendulum Studies
« Reply #7 on: March 13, 2022, 02:47:28 pm »
I'm sorry, you're right. I expressed myself badly. I was thinking about the math behind the two programs. The watch has "A", it can be calculated from "B", from it it can be calculated from C .... more. But the pendulum also has backward calculations, and it can’t be so easily deduced. I was just wondering if additional arms could be added to the pendulum with a simple modification. You're right, I'm sorry again! There are connecting arms in both programs!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
Re: Double Pendulum Studies
« Reply #8 on: March 13, 2022, 02:52:50 pm »
REDACTED
« Last Edit: April 13, 2022, 09:34:44 am by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Double Pendulum Studies
« Reply #9 on: March 13, 2022, 03:06:51 pm »
Sure time has no relation to the viscosity of liquids unless we consider time in the mass viscosity of space.

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
Re: Double Pendulum Studies
« Reply #10 on: March 13, 2022, 03:41:47 pm »
It can be complicated. I will be curious to continue. And also that if you manage to write the equations, it will work with any number of arms?

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
Re: Double Pendulum Studies
« Reply #11 on: March 13, 2022, 07:50:05 pm »
REDACTED
« Last Edit: April 13, 2022, 09:34:37 am by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
Re: Double Pendulum Studies
« Reply #12 on: March 14, 2022, 02:50:49 pm »
I really like what you're experimenting with! Unfortunately, I didn't delve much into what you showed (I complicate the spider game) I like it because you can see the effect of 2 points back and forth on the screen. There is no problem moving a point. It could be its direction, its velocity vector. But if there are already at least two points between which there is some kind of connection that is flexible or rigid, new parameters are immediately created. angular velocity, rotation. The car game didn't really start as a game, but I wanted to model the collision, rotation and overturning of a body. Like yours. The combination of several points. However !! As the wheel damping followed, a real relationship between the car and the wheel was not actually achieved. So it doesn’t go beyond 90 degrees, so it’s good, but if it rolled over, the system I built on would be completely inoperable. I admire what you did because any number of arms would be good. since then it could already be combined with rigid-body physics and real, real physical modeling could be seen on the screen. All you have to do is define N bodies and define what the relationship is between them. How good it would be! For example, we connect two cubes with string and discard them. And it’s also interesting because a change of 1 point has an effect on the other 15 points as well. I see in yours that it works great.

just a thought:
You have 3 points in your program. ABC. working perfectly. If we included a 4th point or a fifth and grouped it in such a way that there were only 3 points in 1 group, you could work AB-C and you would have already calculated CD in front of it ... and so on. But it's a shame I can't put it right. If that weren't understandable, I'll try to make my question more understandable.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
Re: Double Pendulum Studies
« Reply #13 on: March 15, 2022, 11:06:09 pm »
Hey MasterGy

Just wanted to let you know I read your response thoroughly - thanks for your interest in all this. Ya know, I was going to study something altogether different next, but I think you've convinced me to stay on the pendulum case, and generalize the work to handle N arms instead of 2. If opportunity permits, I'll try to let the masses and pendulum lengths be variable rather than homogeneous.

On a completely separate note, there is another approach using the "method of constraints", which may end up being better, but our present tools are too blunt to do this nicely. That is, I would prefer to have a finely-polished package for inverting matrices and doing other maneuvers in linear algebra.

For now though, I'll see if I can extend what I've already done for N links. Stay tuned.
You're not done when it works, you're done when it's right.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
Re: Double Pendulum Studies
« Reply #14 on: March 18, 2022, 01:27:29 pm »
REDACTED
« Last Edit: April 13, 2022, 09:34:30 am by STxAxTIC »
You're not done when it works, you're done when it's right.