@@ -31,6 +31,8 @@ void TW_CALL setTimeStep(const void *value, void *clientData);
3131void TW_CALL getTimeStep (void *value, void *clientData);
3232void TW_CALL setVelocityUpdateMethod (const void *value, void *clientData);
3333void 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);
3436
3537SimulationModel model;
3638TimeStepController sim;
@@ -60,12 +62,13 @@ int main( int argc, char **argv )
6062 buildModel ();
6163
6264 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 ));
6466
6567 TwAddVarRW (MiniGL::getTweakBar (), " Pause" , TW_TYPE_BOOLCPP, &doPause, " label='Pause' group=Simulation key=SPACE " );
6668 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 " );
6769 TwType enumType = TwDefineEnum (" VelocityUpdateMethodType" , NULL , 0 );
6870 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 " );
6972
7073 glutMainLoop ();
7174
@@ -132,7 +135,17 @@ void timeStep ()
132135
133136 // Simulation code
134137 for (unsigned int i = 0 ; i < 8 ; i++)
138+ {
135139 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+ }
136149}
137150
138151void buildModel ()
@@ -170,6 +183,20 @@ void renderUniversalJoint(UniversalJoint &uj)
170183 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 );
171184}
172185
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+
173200void render ()
174201{
175202 MiniGL::coordinateSystem ();
@@ -219,13 +246,22 @@ void render ()
219246 {
220247 renderUniversalJoint (*(UniversalJoint*)constraints[i]);
221248 }
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+ }
222257 }
223258
224259 float textColor[4 ] = { 0 .0f , .2f , .4f , 1 };
225260 MiniGL::drawStrokeText (-0 .5f , 1 .5f , 1 .0f , 0 .002f , " ball joint" , 11 , textColor);
226261 MiniGL::drawStrokeText (3 .0f , 1 .5f , 1 .0f , 0 .002f , " ball-on-line joint" , 19 , textColor);
227262 MiniGL::drawStrokeText (7 .3f , 1 .5f , 1 .0f , 0 .002f , " hinge joint" , 12 , textColor);
228263 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);
229265
230266 MiniGL::drawTime ( TimeManager::getCurrent ()->getTime ());
231267}
@@ -247,9 +283,9 @@ void createBodyModel()
247283 SimulationModel::RigidBodyVector &rb = model.getRigidBodies ();
248284
249285 // static body
250- rb.resize (12 );
286+ rb.resize (15 );
251287 float startX = 0 .0f ;
252- for (unsigned int i = 0 ; i < 4 ; i++)
288+ for (unsigned int i = 0 ; i < 5 ; i++)
253289 {
254290 rb[3 *i] = new RigidBody ();
255291 rb[3 *i]->initBody (0 .0f ,
@@ -286,6 +322,11 @@ void createBodyModel()
286322
287323 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 ));
288324 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 );
289330}
290331
291332void TW_CALL setTimeStep (const void *value, void *clientData)
@@ -310,3 +351,13 @@ void TW_CALL getVelocityUpdateMethod(void *value, void *clientData)
310351 *(short *)(value) = (short )((TimeStepController*)clientData)->getVelocityUpdateMethod ();
311352}
312353
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