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

0 Members and 1 Guest are viewing this topic.

#### 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.
8. TYPE pendlm
9.     POS AS vec3
10.     ang AS DOUBLE
11.     angInc AS DOUBLE
12.     angSize AS DOUBLE
13.
14.     SUB gluLookAt (BYVAL eyeX#, BYVAL eyeY#, BYVAL eyeZ#, BYVAL centerX#, BYVAL centerY#, BYVAL centerZ#, BYVAL upX#, BYVAL upY#, BYVAL upZ#)
15.     SUB glutSolidSphere (BYVAL radius AS DOUBLE, BYVAL slices AS LONG, BYVAL stack AS LONG)
16.
17. DIM SHARED glAllow AS _BYTE
18. DIM SHARED pendulum(1) AS pendlm, t1 AS vec3, t2 AS vec3
19. DIM SHARED tracer(3000) AS vec3, tracerSize AS _UNSIGNED LONG
20.
21. settings:
22. tracerSize = 0
23. g = 0
24.
25. pendulum(0).POS.x = 0
26. pendulum(0).POS.y = 0
27. pendulum(0).POS.z = 0
28. pendulum(0).r = p5random(.7, 1.1)
29. pendulum(0).angInc = p5random(0, _PI(2))
30. pendulum(0).angSize = p5random(_PI(.3), _PI(.6))
31.
32. pendulum(1).r = p5random(.25, .5)
33. pendulum(1).angInc = p5random(0, _PI(2))
34. pendulum(1).angSize = p5random(_PI(.3), _PI(1.1))
35.
36. glAllow = -1
37.     k& = _KEYHIT
38.     IF k& = ASC(" ") THEN GOTO settings
39.     pendulum(0).ang = SIN(pendulum(0).angInc) * pendulum(0).angSize + _PI(.5)
40.
41.     t1.x = COS(pendulum(0).ang) * pendulum(0).r + pendulum(0).POS.x
42.     t1.y = SIN(pendulum(0).ang) * pendulum(0).r + pendulum(0).POS.y
43.     t1.z = COS(pendulum(0).ang) * pendulum(0).r + pendulum(0).POS.z
44.
45.     pendulum(1).POS = t1
46.
47.     pendulum(1).ang = SIN(pendulum(1).angInc) * pendulum(1).angSize + pendulum(0).ang
48.
49.     t2.x = COS(pendulum(1).ang) * pendulum(1).r + pendulum(1).POS.x
50.     t2.y = SIN(pendulum(1).ang) * pendulum(1).r + pendulum(1).POS.y
51.     t2.z = SIN(pendulum(1).ang) * pendulum(1).r + pendulum(1).POS.z
52.
53.     pendulum(0).angInc = pendulum(0).angInc + .02
54.     pendulum(1).angInc = pendulum(1).angInc + .043
55.
56.     IF tracerSize < UBOUND(tracer) - 1 AND g > 40 THEN tracer(tracerSize) = t2
57.     IF g > 40 AND tracerSize < UBOUND(tracer) - 1 THEN tracerSize = tracerSize + 1
58.
59.     g = g + 1
60.     _LIMIT 60
61.
62. SUB _GL () STATIC
63.     IF NOT glAllow THEN EXIT SUB
64.
65.     IF NOT glInit THEN
66.         glInit = -1
67.         aspect# = _WIDTH / _HEIGHT
68.
69.     _glEnable _GL_BLEND
70.     _glEnable _GL_DEPTH_TEST
71.
72.
74.
75.     _glMatrixMode _GL_PROJECTION
76.     _gluPerspective 45.0, aspect#, 1.0, 1000.0
77.
78.     _glMatrixMode _GL_MODELVIEW
79.
80.     gluLookAt 0, 0, -4, 0, 1, 0, 0, -1, 0
81.
82.     _glRotatef clock# * 90, 0, 1, 0
83.     _glLineWidth 3.0
84.
85.
86.     _glColor4f 1, 1, 1, .7
87.
88.     _glBegin _GL_LINES
89.     _glVertex3f pendulum(0).POS.x, pendulum(0).POS.y, pendulum(0).POS.z
90.     _glVertex3f t1.x, t1.y, t1.z
91.
92.
93.     _glBegin _GL_LINES
94.     _glVertex3f t1.x, t1.y, t1.z
95.     _glVertex3f t2.x, t2.y, t2.z
96.
97.     IF tracerSize > 3 THEN
98.         _glBegin _GL_LINES
99.         FOR i = 0 TO tracerSize - 2
100.             _glColor3f 0, map(tracer(i).x, -1, 1, .5, 1), map(tracer(i).y, -1, 1, .5, 1)
101.             _glVertex3f tracer(i).x, tracer(i).y, tracer(i).z
102.             _glColor3f 0, map(tracer(i + 1).x, -1, 1, .5, 1), map(tracer(i + 1).y, -1, 1, .5, 1)
103.             _glVertex3f tracer(i + 1).x, tracer(i + 1).y, tracer(i + 1).z
104.
105.     _glEnable _GL_LIGHTING
106.     _glEnable _GL_LIGHT0
107.     _glTranslatef t1.x, t1.y, t1.z
108.
109.     _glColor3f .8, .8, .8
110.     glutSolidSphere .1, 15, 15
111.
112.     _glTranslatef t2.x, t2.y, t2.z
113.
114.     _glColor3f .8, .8, .8
115.     glutSolidSphere .1, 15, 15
116.
117.     clock# = clock# + .01
118.
119.
120.
121.
122. 'taken from p5js.bas
123. 'https://bit.y/p5jsbas
124. FUNCTION p5random! (mn!, mx!)
125.     IF mn! > mx! THEN
126.         SWAP mn!, mx!
127.     p5random! = RND * (mx! - mn!) + mn!
128.
129. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
130.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
131.

« Last Edit: March 27, 2020, 05:55:53 am by Qwerkey »