@@ -31,6 +31,8 @@ void TW_CALL setTimeStep(const void *value, void *clientData);
31
31
void TW_CALL getTimeStep (void *value, void *clientData);
32
32
void TW_CALL setVelocityUpdateMethod (const void *value, void *clientData);
33
33
void TW_CALL getVelocityUpdateMethod (void *value, void *clientData);
34
+ void TW_CALL setMaxIterations (const void *value, void *clientData);
35
+ void TW_CALL getMaxIterations (void *value, void *clientData);
34
36
35
37
SimulationModel model;
36
38
TimeStepController sim;
@@ -60,12 +62,13 @@ int main( int argc, char **argv )
60
62
buildModel ();
61
63
62
64
MiniGL::setClientSceneFunc (render);
63
- MiniGL::setViewport (50 .0f , 0 .1f , 500 .0f , Vector3f (6 .0f , -2 .5f , 15 .0f ), Vector3f (6 .0f , 0 .0f , 0 .0f ));
65
+ MiniGL::setViewport (50 .0f , 0 .1f , 500 .0f , Vector3f (8 .0f , -2 .5f , 17 .0f ), Vector3f (8 .0f , 0 .0f , 0 .0f ));
64
66
65
67
TwAddVarRW (MiniGL::getTweakBar (), " Pause" , TW_TYPE_BOOLCPP, &doPause, " label='Pause' group=Simulation key=SPACE " );
66
68
TwAddVarCB (MiniGL::getTweakBar (), " TimeStepSize" , TW_TYPE_FLOAT, setTimeStep, getTimeStep, &model, " label='Time step size' min=0.0 max = 0.1 step=0.001 precision=4 group=Simulation " );
67
69
TwType enumType = TwDefineEnum (" VelocityUpdateMethodType" , NULL , 0 );
68
70
TwAddVarCB (MiniGL::getTweakBar (), " VelocityUpdateMethod" , enumType, setVelocityUpdateMethod, getVelocityUpdateMethod, &sim, " label='Velocity update method' enum='0 {First Order Update}, 1 {Second Order Update}' group=Simulation" );
71
+ TwAddVarCB (MiniGL::getTweakBar (), " MaxIter" , TW_TYPE_UINT32, setMaxIterations, getMaxIterations, &sim, " label='Max. iterations' min=1 step=1 group=Simulation " );
69
72
70
73
glutMainLoop ();
71
74
@@ -132,7 +135,17 @@ void timeStep ()
132
135
133
136
// Simulation code
134
137
for (unsigned int i = 0 ; i < 8 ; i++)
138
+ {
135
139
sim.step (model);
140
+
141
+ // set target angle of motors for an animation
142
+ const float currentTargetAngle = (float )M_PI * 0 .5f - (float )M_PI * 0 .5f * cos (0 .25f *TimeManager::getCurrent ()->getTime ());
143
+ SimulationModel::ConstraintVector &constraints = model.getConstraints ();
144
+ TargetAngleMotorHingeJoint &joint1 = (*(TargetAngleMotorHingeJoint*)constraints[8 ]);
145
+ TargetVelocityMotorHingeJoint &joint2 = (*(TargetVelocityMotorHingeJoint*)constraints[9 ]);
146
+ joint1.setTargetAngle (currentTargetAngle);
147
+ joint2.setTargetAngularVelocity (3 .5f );
148
+ }
136
149
}
137
150
138
151
void buildModel ()
@@ -170,6 +183,20 @@ void renderUniversalJoint(UniversalJoint &uj)
170
183
MiniGL::drawCylinder (uj.m_jointInfo .col (5 ) - 0.5 *uj.m_jointInfo .col (7 ), uj.m_jointInfo .col (5 ) + 0.5 *uj.m_jointInfo .col (7 ), jointColor, 0 .05f );
171
184
}
172
185
186
+ void renderTargetAngleMotorHingeJoint (TargetAngleMotorHingeJoint &hj)
187
+ {
188
+ MiniGL::drawSphere (hj.m_jointInfo .col (6 ) - 0.5 *hj.m_jointInfo .col (8 ), 0 .1f , jointColor);
189
+ MiniGL::drawSphere (hj.m_jointInfo .col (6 ) + 0.5 *hj.m_jointInfo .col (8 ), 0 .1f , jointColor);
190
+ MiniGL::drawCylinder (hj.m_jointInfo .col (6 ) - 0.5 *hj.m_jointInfo .col (8 ), hj.m_jointInfo .col (6 ) + 0.5 *hj.m_jointInfo .col (8 ), jointColor, 0 .05f );
191
+ }
192
+
193
+ void renderTargetVelocityMotorHingeJoint (TargetVelocityMotorHingeJoint &hj)
194
+ {
195
+ MiniGL::drawSphere (hj.m_jointInfo .col (6 ) - 0.5 *hj.m_jointInfo .col (8 ), 0 .1f , jointColor);
196
+ MiniGL::drawSphere (hj.m_jointInfo .col (6 ) + 0.5 *hj.m_jointInfo .col (8 ), 0 .1f , jointColor);
197
+ MiniGL::drawCylinder (hj.m_jointInfo .col (6 ) - 0.5 *hj.m_jointInfo .col (8 ), hj.m_jointInfo .col (6 ) + 0.5 *hj.m_jointInfo .col (8 ), jointColor, 0 .05f );
198
+ }
199
+
173
200
void render ()
174
201
{
175
202
MiniGL::coordinateSystem ();
@@ -219,13 +246,22 @@ void render ()
219
246
{
220
247
renderUniversalJoint (*(UniversalJoint*)constraints[i]);
221
248
}
249
+ else if (constraints[i]->getTypeId () == TargetAngleMotorHingeJoint::TYPE_ID)
250
+ {
251
+ renderTargetAngleMotorHingeJoint (*(TargetAngleMotorHingeJoint*)constraints[i]);
252
+ }
253
+ else if (constraints[i]->getTypeId () == TargetVelocityMotorHingeJoint::TYPE_ID)
254
+ {
255
+ renderTargetVelocityMotorHingeJoint (*(TargetVelocityMotorHingeJoint*)constraints[i]);
256
+ }
222
257
}
223
258
224
259
float textColor[4 ] = { 0 .0f , .2f , .4f , 1 };
225
260
MiniGL::drawStrokeText (-0 .5f , 1 .5f , 1 .0f , 0 .002f , " ball joint" , 11 , textColor);
226
261
MiniGL::drawStrokeText (3 .0f , 1 .5f , 1 .0f , 0 .002f , " ball-on-line joint" , 19 , textColor);
227
262
MiniGL::drawStrokeText (7 .3f , 1 .5f , 1 .0f , 0 .002f , " hinge joint" , 12 , textColor);
228
263
MiniGL::drawStrokeText (11 .2f , 1 .5f , 1 .0f , 0 .002f , " universal joint" , 15 , textColor);
264
+ MiniGL::drawStrokeText (15 .0f , 1 .5f , 1 .0f , 0 .002f , " motor hinge joint" , 17 , textColor);
229
265
230
266
MiniGL::drawTime ( TimeManager::getCurrent ()->getTime ());
231
267
}
@@ -247,9 +283,9 @@ void createBodyModel()
247
283
SimulationModel::RigidBodyVector &rb = model.getRigidBodies ();
248
284
249
285
// static body
250
- rb.resize (12 );
286
+ rb.resize (15 );
251
287
float startX = 0 .0f ;
252
- for (unsigned int i = 0 ; i < 4 ; i++)
288
+ for (unsigned int i = 0 ; i < 5 ; i++)
253
289
{
254
290
rb[3 *i] = new RigidBody ();
255
291
rb[3 *i]->initBody (0 .0f ,
@@ -286,6 +322,11 @@ void createBodyModel()
286
322
287
323
model.addUniversalJoint (9 , 10 , Eigen::Vector3f (12 .0f , 0 .75f , 1 .0f ), Eigen::Vector3f (1 .0f , 0 .0f , 0 .0f ), Eigen::Vector3f (0 .0f , 1 .0f , 0 .0f ));
288
324
model.addBallJoint (10 , 11 , Eigen::Vector3f (12 .25f , 0 .75f , 3 .0f ));
325
+
326
+ model.addTargetAngleMotorHingeJoint (12 , 13 , Eigen::Vector3f (16 .0f , 0 .75f , 1 .0f ), Eigen::Vector3f (1 .0f , 0 .0f , 0 .0f ));
327
+ ((TargetAngleMotorHingeJoint*)model.getConstraints ()[model.getConstraints ().size () - 1 ])->setMaxAngularMomentumPerStep (0 .5f );
328
+ model.addTargetVelocityMotorHingeJoint (13 , 14 , Eigen::Vector3f (16 .0f , 0 .75f , 3 .0f ), Eigen::Vector3f (0 .0f , 1 .0f , 0 .0f ));
329
+ ((TargetVelocityMotorHingeJoint*)model.getConstraints ()[model.getConstraints ().size () - 1 ])->setMaxAngularMomentumPerStep (25 .0f );
289
330
}
290
331
291
332
void TW_CALL setTimeStep (const void *value, void *clientData)
@@ -310,3 +351,13 @@ void TW_CALL getVelocityUpdateMethod(void *value, void *clientData)
310
351
*(short *)(value) = (short )((TimeStepController*)clientData)->getVelocityUpdateMethod ();
311
352
}
312
353
354
+ void TW_CALL setMaxIterations (const void *value, void *clientData)
355
+ {
356
+ const unsigned int val = *(const unsigned int *)(value);
357
+ ((TimeStepController*)clientData)->setMaxIterations (val);
358
+ }
359
+
360
+ void TW_CALL getMaxIterations (void *value, void *clientData)
361
+ {
362
+ *(unsigned int *)(value) = ((TimeStepController*)clientData)->getMaxIterations ();
363
+ }
0 commit comments