Author Topic: 3D Double Pendulum  (Read 6578 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
3D Double Pendulum
« on: March 05, 2018, 04:49:22 am »
My previous Double Pendulum now comes in 3D!
Press Space for new Pattern.
I've fixed some bugs in the code. Code updated.
Code: QB64: [Select]
  1. 'Coded By Ashish on 4 March, 2018
  2.  
  3. _TITLE "3D Double Pendulum [Press Space for new settings]"
  4. SCREEN _NEWIMAGE(800, 600, 32)
  5.  
  6. TYPE vec3
  7.     x AS DOUBLE
  8.     y AS DOUBLE
  9.     z AS DOUBLE
  10.  
  11. TYPE pendlm
  12.     pos AS vec3
  13.     r AS DOUBLE
  14.     ang AS DOUBLE
  15.     angInc AS DOUBLE
  16.     angSize AS DOUBLE
  17.  
  18.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
  19.     SUB glutSolidSphere (BYVAL radius AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
  20.  
  21. DIM SHARED glAllow AS _BYTE
  22. DIM SHARED pendulum(1) AS pendlm, t1 AS vec3, t2 AS vec3
  23. DIM SHARED tracer(3000) AS vec3, tracerSize AS _UNSIGNED LONG
  24.  
  25. settings:
  26. tracerSize = 0
  27. g = 0
  28.  
  29. pendulum(0).pos.x = 0
  30. pendulum(0).pos.y = 0
  31. pendulum(0).pos.z = 0
  32. pendulum(0).r = p5random(.7, 1.1)
  33. pendulum(0).angInc = p5random(0, _PI(2))
  34. pendulum(0).angSize = p5random(_PI(.3), _PI(.6))
  35.  
  36. pendulum(1).r = p5random(.25, .5)
  37. pendulum(1).angInc = p5random(0, _PI(2))
  38. pendulum(1).angSize = p5random(_PI(.3), _PI(1.1))
  39.  
  40. glAllow = -1
  41.     k& = _KEYHIT
  42.     IF k& = ASC(" ") THEN GOTO settings
  43.     pendulum(0).ang = SIN(pendulum(0).angInc) * pendulum(0).angSize + _PI(.5)
  44.  
  45.     t1.x = COS(pendulum(0).ang) * pendulum(0).r + pendulum(0).pos.x
  46.     t1.y = SIN(pendulum(0).ang) * pendulum(0).r + pendulum(0).pos.y
  47.     t1.z = COS(pendulum(0).ang) * pendulum(0).r + pendulum(0).pos.z
  48.  
  49.     pendulum(1).pos = t1
  50.  
  51.     pendulum(1).ang = SIN(pendulum(1).angInc) * pendulum(1).angSize + pendulum(0).ang
  52.  
  53.     t2.x = COS(pendulum(1).ang) * pendulum(1).r + pendulum(1).pos.x
  54.     t2.y = SIN(pendulum(1).ang) * pendulum(1).r + pendulum(1).pos.y
  55.     t2.z = SIN(pendulum(1).ang) * pendulum(1).r + pendulum(1).pos.z
  56.  
  57.     pendulum(0).angInc = pendulum(0).angInc + .02
  58.     pendulum(1).angInc = pendulum(1).angInc + .043
  59.  
  60.     IF tracerSize < UBOUND(tracer) - 1 AND g > 40 THEN tracer(tracerSize) = t2
  61.     IF g > 40 AND tracerSize < UBOUND(tracer) - 1 THEN tracerSize = tracerSize + 1
  62.  
  63.     g = g + 1
  64.     _LIMIT 60
  65.  
  66. SUB _GL () STATIC
  67.     IF NOT glAllow THEN EXIT SUB
  68.  
  69.     IF NOT glInit THEN
  70.         glInit = -1
  71.         aspect# = _WIDTH / _HEIGHT
  72.         _glViewport 0, 0, _WIDTH, _HEIGHT
  73.     END IF
  74.  
  75.     _glEnable _GL_BLEND
  76.     _glEnable _GL_DEPTH_TEST
  77.  
  78.  
  79.     _glShadeModel _GL_SMOOTH
  80.  
  81.     _glMatrixMode _GL_PROJECTION
  82.     _gluPerspective 45.0, aspect#, 1.0, 1000.0
  83.  
  84.     _glMatrixMode _GL_MODELVIEW
  85.  
  86.     gluLookAt 0, 0, -4, 0, 1, 0, 0, -1, 0
  87.  
  88.     _glRotatef clock# * 90, 0, 1, 0
  89.     _glLineWidth 3.0
  90.  
  91.  
  92.     _glColor4f 1, 1, 1, .7
  93.  
  94.     _glBegin _GL_LINES
  95.     _glVertex3f pendulum(0).pos.x, pendulum(0).pos.y, pendulum(0).pos.z
  96.     _glVertex3f t1.x, t1.y, t1.z
  97.     _glEnd
  98.  
  99.  
  100.     _glBegin _GL_LINES
  101.     _glVertex3f t1.x, t1.y, t1.z
  102.     _glVertex3f t2.x, t2.y, t2.z
  103.     _glEnd
  104.  
  105.     IF tracerSize > 3 THEN
  106.         _glBegin _GL_LINES
  107.         FOR i = 0 TO tracerSize - 2
  108.             _glColor3f 0, map(tracer(i).x, -1, 1, .5, 1), map(tracer(i).y, -1, 1, .5, 1)
  109.             _glVertex3f tracer(i).x, tracer(i).y, tracer(i).z
  110.             _glColor3f 0, map(tracer(i + 1).x, -1, 1, .5, 1), map(tracer(i + 1).y, -1, 1, .5, 1)
  111.             _glVertex3f tracer(i + 1).x, tracer(i + 1).y, tracer(i + 1).z
  112.         NEXT
  113.         _glEnd
  114.     END IF
  115.  
  116.     _glEnable _GL_LIGHTING
  117.     _glEnable _GL_LIGHT0
  118.     _glTranslatef t1.x, t1.y, t1.z
  119.  
  120.     _glColor3f .8, .8, .8
  121.     glutSolidSphere .1, 15, 15
  122.  
  123.     _glTranslatef t2.x, t2.y, t2.z
  124.  
  125.     _glColor3f .8, .8, .8
  126.     glutSolidSphere .1, 15, 15
  127.  
  128.     clock# = clock# + .01
  129.  
  130.     _glFlush
  131.  
  132.  
  133.  
  134. 'taken from p5js.bas
  135. 'https://bit.y/p5jsbas
  136. FUNCTION p5random! (mn!, mx!)
  137.     IF mn! > mx! THEN
  138.         SWAP mn!, mx!
  139.     END IF
  140.     p5random! = RND * (mx! - mn!) + mn!
  141.  
  142. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  143.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  144.  
Screenshot_1.png
* Screenshot_1.png (Filesize: 31.18 KB, Dimensions: 804x624, Views: 678)
« Last Edit: March 05, 2018, 05:41:32 am by Ashish »
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: 3D Double Pendulum
« Reply #1 on: March 05, 2018, 09:09:12 am »
That looks really good, Ashish!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: 3D Double Pendulum
« Reply #2 on: March 05, 2018, 09:16:07 am »
That looks really good, Ashish!
Thanks ;D
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: 3D Double Pendulum
« Reply #3 on: March 05, 2018, 12:31:02 pm »
Admittedly, I'm not a fan of graphics programs, especially geometric ones.  I'm more of a practical math type. I do get the fascination factor, which in my opinion is a prime component of learning. I'm just blabbering on because I think I recall this poster is 14. I'm too old to be in touch with the programming skills that will be needed in the next decade but I'd feel pretty safe in stating if a 14yr old can put something like this together, it might be worth it for him to investigate what the future in computer programming holds.

Pete

PS I wasn't wild about the trail, but the pure 3-d pendulums were very professional looking. It certainly could be a modern screen saver app.
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: 3D Double Pendulum
« Reply #4 on: March 05, 2018, 11:26:16 pm »
Admittedly, I'm not a fan of graphics programs, especially geometric ones.  I'm more of a practical math type. I do get the fascination factor, which in my opinion is a prime component of learning. I'm just blabbering on because I think I recall this poster is 14. I'm too old to be in touch with the programming skills that will be needed in the next decade but I'd feel pretty safe in stating if a 14yr old can put something like this together, it might be worth it for him to investigate what the future in computer programming holds.

Pete

PS I wasn't wild about the trail, but the pure 3-d pendulums were very professional looking. It certainly could be a modern screen saver app.
Glad you like it. I've came to knew earlier that future programming is Artificial Intelligence. I have tried to learn it a year ago, but it was very complex and the video I watched was above my head. ;D
BTW, I will be able to learn AI before 18.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: 3D Double Pendulum
« Reply #5 on: March 07, 2018, 01:53:06 am »
I prefer natural intelligence over the artificial variety. When I come back, I want my brain taken out of the jar and re-installed. Yep, all original parts in this bad boy. You know this type of science keeps getting more and more expensive as it gets closer to reality but me being the astute businessman I am, well,  I was smart enough to get in on it before the price started increasing. That's right, in 2008 I had my brain removed and put in a jar. The only ill effects I noted that year were taking over the N54 QBasic Forum and voting for Obama. You know what, you may have something there with the whole AI concept after all.

Pete :D   
« Last Edit: March 07, 2018, 02:31:01 am by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline keybone

  • Forum Regular
  • Posts: 116
  • My name a Nursultan Tulyakbay.
    • View Profile
Re: 3D Double Pendulum
« Reply #6 on: March 07, 2018, 01:53:45 am »
Reminds me of an old Windows 3.x screensaver or Windows 9x for that matter.

Well done.
« Last Edit: March 07, 2018, 01:55:27 am by keybone »
I am from a Kazakhstan, we follow the hawk.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: 3D Double Pendulum
« Reply #7 on: March 07, 2018, 05:23:44 am »
Reminds me of an old Windows 3.x screensaver or Windows 9x for that matter.

Well done.
Thanks Keybone! ;D
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: 3D Double Pendulum
« Reply #8 on: March 03, 2019, 06:48:53 am »
Ashish, this is a remarkable achievement.  I may want to ask you some questions on this, if you are agreeable.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: 3D Double Pendulum
« Reply #9 on: March 03, 2019, 10:06:26 am »
Ashish, this is a remarkable achievement.  I may want to ask you some questions on this, if you are agreeable.
You don't need to take permission for asking any thing related to this topic. What are your questions?
if (Me.success) {Me.improve()} else {Me.tryAgain()}


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