-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublePendulum.cpp
157 lines (126 loc) · 4.08 KB
/
DoublePendulum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "DoublePendulum.h"
DoublePendulum::DoublePendulum(
const Primitives& primitives,
glm::vec3 o ) {
_origin = o;
_angle1 = Globals::DEFAULT_ANGLE1 * DEG2RAD;
_angle2 = Globals::DEFAULT_ANGLE2 * DEG2RAD;
_length1 = Globals::LENGTH1;
_length2 = Globals::LENGTH2;
// Upper line index 0
_lines.push_back(
new Line(
primitives,
_origin,
_m1Pos,
Globals::ROPE_THICKNESS ) );
_circles.push_back(
new Circle( primitives, Globals::MASS1 ) );
// Lower line index 1
_lines.push_back(
new Line(
primitives,
_m1Pos,
_m2Pos,
Globals::ROPE_THICKNESS ) );
_circles.push_back(
new Circle( primitives, Globals::MASS2 ) );
_calculateBobPositions();
}
void DoublePendulum::_calculateBobPositions() {
glm::vec3 _m1Pos;
_m1Pos.x = _origin.x + _length1 * std::sin( _angle1 );
_m1Pos.y = _origin.y - _length1 * std::cos( _angle1 );
_m1Pos.z = 0.0f;
glm::vec3 _m2Pos;
_m2Pos.x = _m1Pos.x + _length2 * std::sin( _angle2 );
_m2Pos.y = _m1Pos.y - _length2 * std::cos( _angle2 );
_m2Pos.z = 0.0f;
if( _lines.size() == 2 && _circles.size() == 2 ) {
_lines[0]->SetEndPosition( _m1Pos );
_lines[1]->SetStartPosition( _m1Pos );
_lines[1]->SetEndPosition( _m2Pos );
_circles[0]->MoveTo( _m1Pos );
_circles[1]->MoveTo( _m2Pos );
}
//Utils::showGlmVec3( _lines[0]->GetEndPosition() );
//std::cout << "\nL2 : " << _lines[1]->GetLength();
}
void DoublePendulum::Draw( Shader& shader, Camera& camera ) {
for( int i = 0; i < _lines.size(); ++i ) {
_lines[i]->Draw( shader, camera );
_circles[i]->Draw( shader, camera );
}
}
void DoublePendulum::Draw(
Shader& shader1,
Shader& shader2,
Camera& camera ) {
for( int i = 0; i < _circles.size(); ++i ) {
_circles[i]->Draw( shader2, camera );
}
for( int i = 0; i < _lines.size(); ++i ) {
_lines[i]->Draw( shader1, camera );
}
}
void DoublePendulum::SetAngle1Deg( float value ) {
_angle1 = value * DEG2RAD;
_calculateBobPositions();
}
void DoublePendulum::SetAngle2Deg( float value ) {
_angle2 = value * DEG2RAD;
_calculateBobPositions();
}
void DoublePendulum::Update() {
// Calculate the angular acceleration
float a =
-Globals::GRAVITY * (
2 * Globals::MASS1 + Globals::MASS2 ) *
std::sin( _angle1 );
float b =
Globals::MASS2 *
Globals::GRAVITY *
std::sin( _angle1 - 2 * _angle2 );
float c = 2 * std::sin( _angle1 - _angle2 ) * Globals::MASS2;
float d =
_angularVel2 * _angularVel2 * _length2 +
_angularVel1 * _angularVel1 * _length1 *
std::cos( _angle1 - _angle2 );
float e =
2 * Globals::MASS1 + Globals::MASS2 -
Globals::MASS2 * std::cos( 2 * _angle1 - 2 * _angle2 );
float angularAcc1 = ( a - b - c * d ) / ( _length1 * e );
a = 2 * std::sin( _angle1 - _angle2 );
b =
_angularVel1 * _angularVel1 * _length1 *
( Globals::MASS1 + Globals::MASS2 );
c =
Globals::GRAVITY * (
Globals::MASS1 + Globals::MASS2 ) *
std::cos( _angle1 );
d =
_angularVel2 *
_angularVel2 *
_length2 *
Globals::MASS2 *
std::cos( _angle1 - _angle2 );
float angularAcc2 = ( a * ( b + c + d ) ) / ( _length2 * e );
// Add the angular acceleration to the angular velocity
_angularVel1 += angularAcc1;
_angularVel2 += angularAcc2;
_angularVel1 = Mathematics::Clamp(
_angularVel1,
-Globals::MAX_ANGULAR_VELOCITY,
Globals::MAX_ANGULAR_VELOCITY );
_angularVel2 = Mathematics::Clamp(
_angularVel2,
-Globals::MAX_ANGULAR_VELOCITY,
Globals::MAX_ANGULAR_VELOCITY );
// Add anglular velocity to the angle
_angle1 += _angularVel1;
_angle2 += _angularVel2;
_calculateBobPositions();
}
glm::vec3 DoublePendulum::GetBobPosition() {
return _circles[1]->translation;
}