Author Topic: 3D Double Pendulum by Ashish  (Read 3435 times)

0 Members and 1 Guest are viewing this topic.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
3D Double Pendulum by Ashish
« on: March 12, 2020, 12:53:56 pm »
3D Double Pendulum

Author: @ Ashish
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=153.msg828#msg828
Version: 1
Tags: [3D], [Graphics], [Open_GL]

Description:
A double pendulum swinging in 3D.

Source Code:
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.  

 
3D Double Pendulum Screenshot.jpg
« Last Edit: March 27, 2020, 05:55:53 am by Qwerkey »