Author Topic: Pendula  (Read 9268 times)

0 Members and 1 Guest are viewing this topic.

Offline DANILIN

  • Forum Regular
  • Posts: 128
    • View Profile
    • Danilin youtube
Re: Pendula
« Reply #15 on: September 29, 2020, 04:07:06 pm »
more  of pendulums: long read text

http://rosettacode.org/wiki/Animate_a_pendulum
rosettacode.org/wiki/Animate_a_pendulum

and me learn another language graphic by this page
Russia looks world from future. big data is peace data.
https://youtube.com/playlist?list=PLBBTP9oVY7IagpH0g9FNUQ8JqmHwxDDDB
i never recommend anything to anyone and always write only about myself

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Pendula
« Reply #16 on: September 30, 2020, 09:06:40 am »
My attempt to recreate this in 3D -

Code: QB64: [Select]
  1. 'Pendula in 3D : By Ashish in QB64 using OpenGL
  2. 'idea from here - https://www.qb64.org/forum/index.php?topic=3056.msg123288#msg123288
  3. _TITLE "Pendula in 3D"
  4. SCREEN _NEWIMAGE(800, 600, 32)
  5.  
  6. TYPE pendulum_type
  7.     ang AS SINGLE 'angle
  8.     ang_factor AS SINGLE
  9.     min_ang AS SINGLE
  10.     max_ang AS SINGLE
  11.     mass AS SINGLE
  12.     clr AS _UNSIGNED LONG 'color of the sphere
  13.     rad AS SINGLE 'radius of the sphere
  14.     rope_length AS SINGLE 'length of the rope on which the sphere is hanging
  15.  
  16.     SUB glutSolidSphere (BYVAL radius AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG) 'use to draw a sphere
  17.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#) 'for camera purpose
  18.  
  19. CONST MAX_PENDULUM = 15
  20.  
  21. DIM SHARED pendulum(1 TO MAX_PENDULUM) AS pendulum_type, init AS _BYTE
  22. DIM SHARED ix, iy, iz
  23.  
  24. ix = 0: iy = 1.5: iz = 2
  25.  
  26. FOR i = 1 TO MAX_PENDULUM
  27.     pendulum(i).rad = 0.25
  28.     pendulum(i).min_ang = _PI(1.25)
  29.     pendulum(i).max_ang = _PI(1.75)
  30.     pendulum(i).clr = _RGB(RND * 255, RND * 255, RND * 255)
  31.     pendulum(i).rope_length = 2
  32.     pendulum(i).mass = 3 + i * 0.5
  33.  
  34. init = 1
  35.     FOR i = 1 TO MAX_PENDULUM
  36.         pendulum(i).ang_factor = pendulum(i).ang_factor + 0.01 * pendulum(i).mass
  37.         pendulum(i).ang = map(SIN(pendulum(i).ang_factor), -1, 1, pendulum(i).min_ang, pendulum(i).max_ang)
  38.     NEXT
  39.     _LIMIT 60
  40.  
  41. SUB _GL ()
  42.     STATIC t
  43.     t = t + 0.01
  44.     IF init = 0 THEN EXIT SUB
  45.    
  46.     ' _glClearColor 0.3,0.3,0.3,1
  47.     _glClear _GL_COLOR_BUFFER_BIT OR _GL_DEPTH_BUFFER_BIT
  48.    
  49.     _glEnable _GL_LIGHTING
  50.     _glEnable _GL_LIGHT0
  51.     _glEnable _GL_DEPTH_TEST
  52.     _glEnable _GL_COLOR_MATERIAL
  53.    
  54.     DIM v(3) AS SINGLE
  55.     v(0) = 0: v(1) = 0: v(2) = 1: v(3) = 0
  56.    
  57.     _glLightfv _GL_LIGHT0, _GL_POSITION, _OFFSET(v())
  58.    
  59.     _glMatrixMode _GL_PROJECTION
  60.     _gluPerspective 50, _WIDTH / _HEIGHT, 0.1, 25
  61.    
  62.     _glMatrixMode _GL_MODELVIEW
  63.    
  64.     gluLookAt 0, 2, 5, 0, 0, 0, 0, 1, 0
  65.    
  66.     drawXZPlane 0, -2, 0, 8, 16, -1
  67.    
  68.     DIM z
  69.     z = iz
  70.     _glLineWidth 5.0
  71.     FOR i = 1 TO MAX_PENDULUM
  72.         _glColor3ub 255, 255, 0
  73.         _glBegin _GL_LINES
  74.         _glVertex3f ix, iy, z
  75.         _glVertex3f ix + pendulum(i).rope_length * COS(pendulum(i).ang), iy + pendulum(i).rope_length * SIN(pendulum(i).ang), z
  76.         _glEnd
  77.        
  78.         _glPushMatrix
  79.         _glTranslatef ix + (pendulum(i).rope_length + pendulum(i).rad) * COS(pendulum(i).ang), iy + (pendulum(i).rope_length + pendulum(i).rad) * SIN(pendulum(i).ang), z
  80.         _glColor3ub _RED(pendulum(i).clr), _GREEN(pendulum(i).clr), _BLUE(pendulum(i).clr)
  81.         glutSolidSphere pendulum(i).rad, 50, 50
  82.         _glPopMatrix
  83.        
  84.         z = z - 2.3 * pendulum(i).rad
  85.     NEXT
  86.     _glColor3ub 255, 255, 0
  87.     _glBegin _GL_LINES
  88.     _glVertex3f ix, iy, iz
  89.     _glVertex3f ix, iy, z
  90.     _glEnd
  91.    
  92.     _glFlush
  93.  
  94. SUB drawXZPlane (x, y, z, w, d, n) '(x,z)->location to draw, w->width, d->depth of the plane, n->normal direction in Y direction
  95.     _glBegin _GL_QUADS
  96.     _glTexCoord2f 1, 0
  97.     _glNormal3f 0, n, 0
  98.     _glVertex3f x - (w / 2), y, z - d / 2
  99.     _glTexCoord2f 1, 1
  100.     _glNormal3f 0, n, 0
  101.     _glVertex3f x + (w / 2), y, z - d / 2
  102.     _glTexCoord2f 0, 1
  103.     _glNormal3f 0, n, 0
  104.     _glVertex3f x + (w / 2), y, z + d / 2
  105.     _glTexCoord2f 0, 0
  106.     _glNormal3f 0, n, 0
  107.     _glVertex3f x - (w / 2), y, z + d / 2
  108.     _glEnd
  109.  
  110. 'from p5js.bas
  111. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  112.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  113.  
  114.  
Screenshot_1.png
* Screenshot_1.png (Filesize: 74.48 KB, Dimensions: 801x625, Views: 297)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

FellippeHeitor

  • Guest
Re: Pendula
« Reply #17 on: September 30, 2020, 09:20:35 am »
My attempt to recreate this in 3D -

Whoooooooooa!
🤩

Marked as best answer by on Today at 07:40:29 am

Offline DANILIN

  • Forum Regular
  • Posts: 128
    • View Profile
    • Danilin youtube
Re: Pendula
« Reply #18 on: January 06, 2022, 12:38:38 am »
  • Undo Best Answer
  • improvement:

    for colorize:
    Randomize Timer

    thik lines:
    _glLineWidth 0.0

    or symbol ':
    '_glBegin _GL_LINES 
    Russia looks world from future. big data is peace data.
    https://youtube.com/playlist?list=PLBBTP9oVY7IagpH0g9FNUQ8JqmHwxDDDB
    i never recommend anything to anyone and always write only about myself

    Offline _vince

    • Seasoned Forum Regular
    • Posts: 422
      • View Profile
    Re: Pendula
    « Reply #19 on: January 07, 2022, 05:13:58 am »
    We've all seen the chaotic double pendulum.  I'll leave my simple Runge-Kutta one done with canned formulas from wikipedia at the end.

    Challenge:  Generalize it to n-pendulum!  Triple, quadruple, whatever the user wants.  The next ball is to be attached to the previous.  It's going to be a fairly tedious system of differential equations based on the lagrangian of the system (https://en.wikipedia.org/wiki/Double_pendulum#Analysis_and_interpretation).  Probably so tedious you're going to need a large matrix and some linear algebra.  But goddamn I'll be so impressed if you do it, you'll come out of it totally competent to program everything DEs.

    Here's the trivial case:
    Code: QB64: [Select]
    1. defsng a-z
    2. pi = 3.141593
    3. r = 100
    4. a1 = -pi/2
    5. a2 = -pi/2
    6. h=0.001
    7. m=1000
    8. g=1000
    9. sw = 640
    10. sh = 480
    11.  
    12. p1 = _newimage(sw,sh,12)
    13. screen _newimage(sw,sh,12)
    14.  
    15.         a1p = (6/(m*r^2))*(2*m*v1-3*cos(a1-a2)*m*v2)/(16-9*(cos(a1-a2))^2)
    16.         a1k1 = a1p
    17.         a1k2 = a1p + h*a1k1/2
    18.         a1k3 = a1p + h*a1k2/2
    19.         a1k4 = a1p + h*a1k3
    20.         a2p = (6/(m*r^2))*(8*m*v2-3*cos(a1-a2)*m*v1)/(16-9*(cos(a1-a2))^2)
    21.         a2k1 = a2p
    22.         a2k2 = a2p + h*a2k1/2
    23.         a2k3 = a2p + h*a2k2/2
    24.         a2k4 = a2p + h*a2k3
    25.         na1=a1+h*(a1k1+2*a1k2+2*a1k3+a1k4)/6
    26.         na2=a2+h*(a2k1+2*a2k2+2*a2k3+a2k4)/6
    27.         v1p = -0.5*r^2*(a1p*a2p*sin(a1-a2)+3*g*sin(a1)/r)
    28.         v1k1 = v1p
    29.         v1k2 = v1p + h*v1k1/2
    30.         v1k3 = v1p + h*v1k2/2
    31.         v1k4 = v1p + h*v1k3
    32.         v2p = -0.5*r^2*(-a1p*a2p*sin(a1-a2)+g*sin(a2)/r)
    33.         v2k1 = v2p
    34.         v2k2 = v2p + h*v2k1/2
    35.         v2k3 = v2p + h*v2k2/2
    36.         v2k4 = v2p + h*v2k3
    37.         nv1=v1+h*(v1k1+2*v1k2+2*v1k3+v1k4)/6
    38.         nv2=v2+h*(v2k1+2*v2k2+2*v2k3+v2k4)/6
    39.         a1=na1
    40.         a2=na2
    41.         v1=nv1
    42.         v2=nv2
    43.  
    44.         _dest p1
    45.         pset (sw/2+r*cos(a1-pi/2)+r*cos(a2-pi/2), sh/2-r*sin(a1-pi/2)-r*sin(a2-pi/2)),12
    46.         _dest 0
    47.         _putimage, p1
    48.  
    49.         line (sw/2, sh/2)-step(r*cos(a1-pi/2), -r*sin(a1-pi/2))
    50.         circle step(0,0),5
    51.         line -step(r*cos(a2-pi/2), -r*sin(a2-pi/2))
    52.         circle step(0,0),5
    53.  
    54.         _limit 200
    55.         _display
    56.  

    Offline STxAxTIC

    • Library Staff
    • Forum Resident
    • Posts: 1091
    • he lives
      • View Profile
    Re: Pendula
    « Reply #20 on: January 07, 2022, 10:14:12 am »
    That's an interesting challenge. Bonus points if all pendula are constrained to never self-intersect, so that the resulting object should be like a hanging chain and exhibit the same physics. I wonder if this is even *that* terrible to do, becau - ya know what? I'll shut up.... Nice challenge!
    You're not done when it works, you're done when it's right.