Dof6ConstraintTutorial.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. #include "Dof6ConstraintTutorial.h"
  2. #include "btBulletDynamicsCommon.h"
  3. #include "BulletDynamics/ConstraintSolver/btNNCGConstraintSolver.h"
  4. #include "BulletDynamics/MLCPSolvers/btMLCPSolver.h"
  5. #include "BulletDynamics/MLCPSolvers/btSolveProjectedGaussSeidel.h"
  6. #include "BulletDynamics/MLCPSolvers/btLemkeSolver.h"
  7. #include "BulletDynamics/MLCPSolvers/btDantzigSolver.h"
  8. #include "../RenderingExamples/TimeSeriesCanvas.h"
  9. #include "BulletDynamics/ConstraintSolver/btGeneric6DofSpring2Constraint.h"
  10. #ifndef M_PI
  11. #define M_PI 3.14159265358979323846
  12. #endif
  13. #ifndef M_PI_2
  14. #define M_PI_2 1.57079632679489661923
  15. #endif
  16. #ifndef M_PI_4
  17. #define M_PI_4 0.785398163397448309616
  18. #endif
  19. extern float g_additionalBodyMass;
  20. //comment this out to compare with original spring constraint
  21. #define USE_6DOF2
  22. #ifdef USE_6DOF2
  23. #define CONSTRAINT_TYPE btGeneric6DofSpring2Constraint
  24. #define EXTRAPARAMS
  25. #else
  26. #define CONSTRAINT_TYPE btGeneric6DofSpringConstraint
  27. #define EXTRAPARAMS ,true
  28. #endif
  29. #include "../CommonInterfaces/CommonRigidBodyBase.h"
  30. struct Dof6ConstraintTutorial : public CommonRigidBodyBase
  31. {
  32. struct Dof6ConstraintTutorialInternalData* m_data;
  33. Dof6ConstraintTutorial(struct GUIHelperInterface* helper);
  34. virtual ~Dof6ConstraintTutorial();
  35. virtual void initPhysics();
  36. virtual void stepSimulation(float deltaTime);
  37. void animate();
  38. virtual void resetCamera()
  39. {
  40. float dist = 5;
  41. float pitch = 722;
  42. float yaw = 35;
  43. float targetPos[3]={4,2,-11};
  44. m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
  45. }
  46. };
  47. struct Dof6ConstraintTutorialInternalData
  48. {
  49. btRigidBody* m_TranslateSpringBody;
  50. btRigidBody* m_TranslateSpringBody2;
  51. btRigidBody* m_RotateSpringBody;
  52. btRigidBody* m_RotateSpringBody2;
  53. btRigidBody* m_BouncingTranslateBody;
  54. btRigidBody* m_MotorBody;
  55. btRigidBody* m_ServoMotorBody;
  56. btRigidBody* m_ChainLeftBody;
  57. btRigidBody* m_ChainRightBody;
  58. CONSTRAINT_TYPE* m_ServoMotorConstraint;
  59. CONSTRAINT_TYPE* m_ChainLeftConstraint;
  60. CONSTRAINT_TYPE* m_ChainRightConstraint;
  61. TimeSeriesCanvas* m_timeSeriesCanvas;
  62. float mDt;
  63. unsigned int frameID;
  64. Dof6ConstraintTutorialInternalData()
  65. : mDt(1./60.),frameID(0)
  66. {
  67. }
  68. };
  69. Dof6ConstraintTutorial::Dof6ConstraintTutorial(struct GUIHelperInterface* helper)
  70. :CommonRigidBodyBase(helper)
  71. {
  72. m_data = new Dof6ConstraintTutorialInternalData;
  73. m_data->m_timeSeriesCanvas = new TimeSeriesCanvas(helper->get2dCanvasInterface(),256,256,"Position and Velocity");
  74. m_data->m_timeSeriesCanvas ->setupTimeSeries(20,100, 0);
  75. m_data->m_timeSeriesCanvas->addDataSource("X position (m)", 255,0,0);
  76. m_data->m_timeSeriesCanvas->addDataSource("X velocity (m/s)", 0,0,255);
  77. m_data->m_timeSeriesCanvas->addDataSource("dX/dt (m/s)", 0,0,0);
  78. }
  79. Dof6ConstraintTutorial::~Dof6ConstraintTutorial()
  80. {
  81. delete m_data->m_timeSeriesCanvas;
  82. m_data->m_timeSeriesCanvas = 0;
  83. exitPhysics();
  84. delete m_data;
  85. }
  86. void Dof6ConstraintTutorial::initPhysics()
  87. {
  88. // Setup the basic world
  89. m_guiHelper->setUpAxis(1);
  90. m_collisionConfiguration = new btDefaultCollisionConfiguration();
  91. m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
  92. btVector3 worldAabbMin(-10000,-10000,-10000);
  93. btVector3 worldAabbMax(10000,10000,10000);
  94. m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax);
  95. /////// uncomment the corresponding line to test a solver.
  96. //m_solver = new btSequentialImpulseConstraintSolver;
  97. m_solver = new btNNCGConstraintSolver;
  98. //m_solver = new btMLCPSolver(new btSolveProjectedGaussSeidel());
  99. //m_solver = new btMLCPSolver(new btDantzigSolver());
  100. //m_solver = new btMLCPSolver(new btLemkeSolver());
  101. m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
  102. m_dynamicsWorld->getDispatchInfo().m_useContinuous = true;
  103. m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
  104. m_dynamicsWorld->setGravity(btVector3(0,0,0));
  105. // Setup a big ground box
  106. if (0)
  107. {
  108. btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(5.),btScalar(200.)));
  109. btTransform groundTransform;
  110. groundTransform.setIdentity();
  111. groundTransform.setOrigin(btVector3(0,-10,0));
  112. #define CREATE_GROUND_COLLISION_OBJECT 1
  113. #ifdef CREATE_GROUND_COLLISION_OBJECT
  114. btCollisionObject* fixedGround = new btCollisionObject();
  115. fixedGround->setCollisionShape(groundShape);
  116. fixedGround->setWorldTransform(groundTransform);
  117. m_dynamicsWorld->addCollisionObject(fixedGround);
  118. #else
  119. localCreateRigidBody(btScalar(0.),groundTransform,groundShape);
  120. #endif //CREATE_GROUND_COLLISION_OBJECT
  121. }
  122. m_dynamicsWorld->getSolverInfo().m_numIterations = 100;
  123. btCollisionShape* shape;
  124. btVector3 localInertia(0,0,0);
  125. btDefaultMotionState* motionState;
  126. btTransform bodyTransform;
  127. btScalar mass;
  128. btTransform localA;
  129. btTransform localB;
  130. CONSTRAINT_TYPE* constraint;
  131. //static body centered in the origo
  132. mass = 0.0;
  133. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  134. localInertia = btVector3(0,0,0);
  135. bodyTransform.setIdentity();
  136. motionState = new btDefaultMotionState(bodyTransform);
  137. btRigidBody* staticBody = new btRigidBody(mass,motionState,shape,localInertia);
  138. m_dynamicsWorld->addRigidBody(staticBody);
  139. /////////// box with undamped translate spring attached to static body
  140. /////////// the box should oscillate left-to-right forever
  141. {
  142. mass = 1.0;
  143. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  144. shape->calculateLocalInertia(mass,localInertia);
  145. bodyTransform.setIdentity();
  146. bodyTransform.setOrigin(btVector3(-2,0,-5));
  147. motionState = new btDefaultMotionState(bodyTransform);
  148. m_data->m_TranslateSpringBody = new btRigidBody(mass,motionState,shape,localInertia);
  149. m_data->m_TranslateSpringBody->setActivationState(DISABLE_DEACTIVATION);
  150. m_dynamicsWorld->addRigidBody(m_data->m_TranslateSpringBody);
  151. localA.setIdentity();localA.getOrigin() = btVector3(0,0,-5);
  152. localB.setIdentity();
  153. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_TranslateSpringBody, localA, localB EXTRAPARAMS);
  154. constraint->setLimit(0, 1,-1);
  155. constraint->setLimit(1, 0, 0);
  156. constraint->setLimit(2, 0, 0);
  157. constraint->setLimit(3, 0, 0);
  158. constraint->setLimit(4, 0, 0);
  159. constraint->setLimit(5, 0, 0);
  160. constraint->enableSpring(0, true);
  161. constraint->setStiffness(0, 100);
  162. #ifdef USE_6DOF2
  163. constraint->setDamping(0, 0);
  164. #else
  165. constraint->setDamping(0, 1);
  166. #endif
  167. constraint->setEquilibriumPoint(0, 0);
  168. constraint->setDbgDrawSize(btScalar(2.f));
  169. m_dynamicsWorld->addConstraint(constraint, true);
  170. }
  171. #if 0
  172. /////////// box with rotate spring, attached to static body
  173. /////////// box should swing (rotate) left-to-right forever
  174. {
  175. mass = 1.0;
  176. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  177. shape->calculateLocalInertia(mass,localInertia);
  178. bodyTransform.setIdentity();
  179. bodyTransform.getBasis().setEulerZYX(0,0,M_PI_2);
  180. motionState = new btDefaultMotionState(bodyTransform);
  181. m_data->m_RotateSpringBody = new btRigidBody(mass,motionState,shape,localInertia);
  182. m_data->m_RotateSpringBody->setActivationState(DISABLE_DEACTIVATION);
  183. m_dynamicsWorld->addRigidBody(m_data->m_RotateSpringBody);
  184. localA.setIdentity();localA.getOrigin() = btVector3(0,0,0);
  185. localB.setIdentity();localB.setOrigin(btVector3(0,0.5,0));
  186. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_RotateSpringBody, localA, localB EXTRAPARAMS);
  187. constraint->setLimit(0, 0, 0);
  188. constraint->setLimit(1, 0, 0);
  189. constraint->setLimit(2, 0, 0);
  190. constraint->setLimit(3, 0, 0);
  191. constraint->setLimit(4, 0, 0);
  192. constraint->setLimit(5, 1, -1);
  193. constraint->enableSpring(5, true);
  194. constraint->setStiffness(5, 100);
  195. #ifdef USE_6DOF2
  196. constraint->setDamping(5, 0);
  197. #else
  198. constraint->setDamping(5, 1);
  199. #endif
  200. constraint->setEquilibriumPoint(0, 0);
  201. constraint->setDbgDrawSize(btScalar(2.f));
  202. m_dynamicsWorld->addConstraint(constraint, true);
  203. }
  204. /////////// box with bouncing constraint, translation is bounced at the positive x limit, but not at the negative limit
  205. /////////// bouncing can not be set independently at low and high limits, so two constraints will be created: one that defines the low (non bouncing) limit, and one that defines the high (bouncing) limit
  206. /////////// the box should move to the left (as an impulse will be applied to it periodically) until it reaches its limit, then bounce back
  207. {
  208. mass = 1.0;
  209. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  210. shape->calculateLocalInertia(mass,localInertia);
  211. bodyTransform.setIdentity();
  212. bodyTransform.setOrigin(btVector3(0,0,-3));
  213. motionState = new btDefaultMotionState(bodyTransform);
  214. m_data->m_BouncingTranslateBody = new btRigidBody(mass,motionState,shape,localInertia);
  215. m_data->m_BouncingTranslateBody->setActivationState(DISABLE_DEACTIVATION);
  216. m_data->m_BouncingTranslateBody->setDeactivationTime(btScalar(20000000));
  217. m_dynamicsWorld->addRigidBody(m_data->m_BouncingTranslateBody);
  218. localA.setIdentity();localA.getOrigin() = btVector3(0,0,0);
  219. localB.setIdentity();
  220. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_BouncingTranslateBody, localA, localB EXTRAPARAMS);
  221. constraint->setLimit(0, -2, SIMD_INFINITY);
  222. constraint->setLimit(1, 0, 0);
  223. constraint->setLimit(2, -3, -3);
  224. constraint->setLimit(3, 0, 0);
  225. constraint->setLimit(4, 0, 0);
  226. constraint->setLimit(5, 0, 0);
  227. #ifdef USE_6DOF2
  228. constraint->setBounce(0,0);
  229. #else //bounce is named restitution in 6dofspring, but not implemented for translational limit motor, so the following line has no effect
  230. constraint->getTranslationalLimitMotor()->m_restitution = 0.0;
  231. #endif
  232. constraint->setParam(BT_CONSTRAINT_STOP_ERP,0.995,0);
  233. constraint->setParam(BT_CONSTRAINT_STOP_CFM,0.0,0);
  234. constraint->setDbgDrawSize(btScalar(2.f));
  235. m_dynamicsWorld->addConstraint(constraint, true);
  236. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_BouncingTranslateBody, localA, localB EXTRAPARAMS);
  237. constraint->setLimit(0, -SIMD_INFINITY, 2);
  238. constraint->setLimit(1, 0, 0);
  239. constraint->setLimit(2, -3, -3);
  240. constraint->setLimit(3, 0, 0);
  241. constraint->setLimit(4, 0, 0);
  242. constraint->setLimit(5, 0, 0);
  243. #ifdef USE_6DOF2
  244. constraint->setBounce(0,1);
  245. #else //bounce is named restitution in 6dofspring, but not implemented for translational limit motor, so the following line has no effect
  246. constraint->getTranslationalLimitMotor()->m_restitution = 1.0;
  247. #endif
  248. constraint->setParam(BT_CONSTRAINT_STOP_ERP,0.995,0);
  249. constraint->setParam(BT_CONSTRAINT_STOP_CFM,0.0,0);
  250. constraint->setDbgDrawSize(btScalar(2.f));
  251. m_dynamicsWorld->addConstraint(constraint, true);
  252. }
  253. /////////// box with rotational motor, attached to static body
  254. /////////// the box should rotate around the y axis
  255. {
  256. mass = 1.0;
  257. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  258. shape->calculateLocalInertia(mass,localInertia);
  259. bodyTransform.setIdentity();
  260. bodyTransform.setOrigin(btVector3(4,0,0));
  261. motionState = new btDefaultMotionState(bodyTransform);
  262. m_data->m_MotorBody = new btRigidBody(mass,motionState,shape,localInertia);
  263. m_data->m_MotorBody->setActivationState(DISABLE_DEACTIVATION);
  264. m_dynamicsWorld->addRigidBody(m_data->m_MotorBody);
  265. localA.setIdentity();localA.getOrigin() = btVector3(4,0,0);
  266. localB.setIdentity();
  267. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_MotorBody, localA, localB EXTRAPARAMS);
  268. constraint->setLimit(0, 0, 0);
  269. constraint->setLimit(1, 0, 0);
  270. constraint->setLimit(2, 0, 0);
  271. constraint->setLimit(3, 0, 0);
  272. constraint->setLimit(4, 0, 0);
  273. constraint->setLimit(5, 1,-1);
  274. #ifdef USE_6DOF2
  275. constraint->enableMotor(5,true);
  276. constraint->setTargetVelocity(5,3.f);
  277. constraint->setMaxMotorForce(5,10.f);
  278. #else
  279. constraint->getRotationalLimitMotor(2)->m_enableMotor = true;
  280. constraint->getRotationalLimitMotor(2)->m_targetVelocity = 3.f;
  281. constraint->getRotationalLimitMotor(2)->m_maxMotorForce = 10;
  282. #endif
  283. constraint->setDbgDrawSize(btScalar(2.f));
  284. m_dynamicsWorld->addConstraint(constraint, true);
  285. }
  286. /////////// box with rotational servo motor, attached to static body
  287. /////////// the box should rotate around the y axis until it reaches its target
  288. /////////// the target will be negated periodically
  289. {
  290. mass = 1.0;
  291. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  292. shape->calculateLocalInertia(mass,localInertia);
  293. bodyTransform.setIdentity();
  294. bodyTransform.setOrigin(btVector3(7,0,0));
  295. motionState = new btDefaultMotionState(bodyTransform);
  296. m_data->m_ServoMotorBody = new btRigidBody(mass,motionState,shape,localInertia);
  297. m_data->m_ServoMotorBody->setActivationState(DISABLE_DEACTIVATION);
  298. m_dynamicsWorld->addRigidBody(m_data->m_ServoMotorBody);
  299. localA.setIdentity();localA.getOrigin() = btVector3(7,0,0);
  300. localB.setIdentity();
  301. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_ServoMotorBody, localA, localB EXTRAPARAMS);
  302. constraint->setLimit(0, 0, 0);
  303. constraint->setLimit(1, 0, 0);
  304. constraint->setLimit(2, 0, 0);
  305. constraint->setLimit(3, 0, 0);
  306. constraint->setLimit(4, 0, 0);
  307. constraint->setLimit(5, 1,-1);
  308. #ifdef USE_6DOF2
  309. constraint->enableMotor(5,true);
  310. constraint->setTargetVelocity(5,3.f);
  311. constraint->setMaxMotorForce(5,10.f);
  312. constraint->setServo(5,true);
  313. constraint->setServoTarget(5, M_PI_2);
  314. #else
  315. constraint->getRotationalLimitMotor(2)->m_enableMotor = true;
  316. constraint->getRotationalLimitMotor(2)->m_targetVelocity = 3.f;
  317. constraint->getRotationalLimitMotor(2)->m_maxMotorForce = 10;
  318. //servo motor is not implemented in 6dofspring constraint
  319. #endif
  320. constraint->setDbgDrawSize(btScalar(2.f));
  321. m_dynamicsWorld->addConstraint(constraint, true);
  322. m_data->m_ServoMotorConstraint = constraint;
  323. }
  324. ////////// chain of boxes linked together with fully limited rotational and translational constraints
  325. ////////// the chain will be pulled to the left and to the right periodically. They should strictly stick together.
  326. {
  327. btScalar limitConstraintStrength = 0.6;
  328. int bodycount = 10;
  329. btRigidBody* prevBody = 0;
  330. for(int i = 0; i < bodycount; ++i)
  331. {
  332. mass = 1.0;
  333. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  334. shape->calculateLocalInertia(mass,localInertia);
  335. bodyTransform.setIdentity();
  336. bodyTransform.setOrigin(btVector3(- i,0,3));
  337. motionState = new btDefaultMotionState(bodyTransform);
  338. btRigidBody* body = new btRigidBody(mass,motionState,shape,localInertia);
  339. body->setActivationState(DISABLE_DEACTIVATION);
  340. m_dynamicsWorld->addRigidBody(body);
  341. if(prevBody != 0)
  342. {
  343. localB.setIdentity();
  344. localB.setOrigin(btVector3(0.5,0,0));
  345. btTransform localA;
  346. localA.setIdentity();
  347. localA.setOrigin(btVector3(-0.5,0,0));
  348. CONSTRAINT_TYPE* constraint = new CONSTRAINT_TYPE(*prevBody, *body, localA, localB EXTRAPARAMS);
  349. constraint->setLimit(0, -0.01, 0.01);
  350. constraint->setLimit(1, 0, 0);
  351. constraint->setLimit(2, 0, 0);
  352. constraint->setLimit(3, 0, 0);
  353. constraint->setLimit(4, 0, 0);
  354. constraint->setLimit(5, 0, 0);
  355. for(int a = 0; a < 6; ++a)
  356. {
  357. constraint->setParam(BT_CONSTRAINT_STOP_ERP,0.9,a);
  358. constraint->setParam(BT_CONSTRAINT_STOP_CFM,0.0,a);
  359. }
  360. constraint->setDbgDrawSize(btScalar(1.f));
  361. m_dynamicsWorld->addConstraint(constraint, true);
  362. if(i < bodycount - 1)
  363. {
  364. localA.setIdentity();localA.getOrigin() = btVector3(0,0,3);
  365. localB.setIdentity();
  366. CONSTRAINT_TYPE* constraintZY = new CONSTRAINT_TYPE(*staticBody, *body, localA, localB EXTRAPARAMS);
  367. constraintZY->setLimit(0, 1, -1);
  368. constraintZY->setDbgDrawSize(btScalar(1.f));
  369. m_dynamicsWorld->addConstraint(constraintZY, true);
  370. }
  371. }
  372. else
  373. {
  374. localA.setIdentity();localA.getOrigin() = btVector3(bodycount,0,3);
  375. localB.setIdentity();
  376. localB.setOrigin(btVector3(0,0,0));
  377. m_data->m_ChainLeftBody = body;
  378. m_data->m_ChainLeftConstraint = new CONSTRAINT_TYPE(*staticBody, *body, localA, localB EXTRAPARAMS);
  379. m_data->m_ChainLeftConstraint->setLimit(3,0,0);
  380. m_data->m_ChainLeftConstraint->setLimit(4,0,0);
  381. m_data->m_ChainLeftConstraint->setLimit(5,0,0);
  382. for(int a = 0; a < 6; ++a)
  383. {
  384. m_data->m_ChainLeftConstraint->setParam(BT_CONSTRAINT_STOP_ERP,limitConstraintStrength,a);
  385. m_data->m_ChainLeftConstraint->setParam(BT_CONSTRAINT_STOP_CFM,0.0,a);
  386. }
  387. m_data->m_ChainLeftConstraint->setDbgDrawSize(btScalar(1.f));
  388. m_dynamicsWorld->addConstraint(m_data->m_ChainLeftConstraint, true);
  389. }
  390. prevBody = body;
  391. }
  392. m_data->m_ChainRightBody = prevBody;
  393. localA.setIdentity();localA.getOrigin() = btVector3(-bodycount,0,3);
  394. localB.setIdentity();
  395. localB.setOrigin(btVector3(0,0,0));
  396. m_data->m_ChainRightConstraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_ChainRightBody, localA, localB EXTRAPARAMS);
  397. m_data->m_ChainRightConstraint->setLimit(3,0,0);
  398. m_data->m_ChainRightConstraint->setLimit(4,0,0);
  399. m_data->m_ChainRightConstraint->setLimit(5,0,0);
  400. for(int a = 0; a < 6; ++a)
  401. {
  402. m_data->m_ChainRightConstraint->setParam(BT_CONSTRAINT_STOP_ERP,limitConstraintStrength,a);
  403. m_data->m_ChainRightConstraint->setParam(BT_CONSTRAINT_STOP_CFM,0.0,a);
  404. }
  405. }
  406. #endif
  407. m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
  408. }
  409. void Dof6ConstraintTutorial::animate()
  410. {
  411. /////// servo motor: flip its target periodically
  412. #ifdef USE_6DOF2
  413. static float servoNextFrame = -1;
  414. btScalar pos = m_data->m_ServoMotorConstraint->getRotationalLimitMotor(2)->m_currentPosition;
  415. btScalar target = m_data->m_ServoMotorConstraint->getRotationalLimitMotor(2)->m_servoTarget;
  416. if(servoNextFrame < 0)
  417. {
  418. m_data->m_ServoMotorConstraint->getRotationalLimitMotor(2)->m_servoTarget *= -1;
  419. servoNextFrame = 3.0;
  420. }
  421. servoNextFrame -= m_data->mDt;
  422. #endif
  423. /////// constraint chain: pull the chain left and right periodically
  424. static float chainNextFrame = -1;
  425. static bool left = true;
  426. if(chainNextFrame < 0)
  427. {
  428. if(!left)
  429. {
  430. m_data->m_ChainRightBody->setActivationState(ACTIVE_TAG);
  431. m_dynamicsWorld->removeConstraint(m_data->m_ChainRightConstraint);
  432. m_data->m_ChainLeftConstraint->setDbgDrawSize(btScalar(2.f));
  433. m_dynamicsWorld->addConstraint(m_data->m_ChainLeftConstraint, true);
  434. }
  435. else
  436. {
  437. m_data->m_ChainLeftBody->setActivationState(ACTIVE_TAG);
  438. m_dynamicsWorld->removeConstraint(m_data->m_ChainLeftConstraint);
  439. m_data->m_ChainRightConstraint->setDbgDrawSize(btScalar(2.f));
  440. m_dynamicsWorld->addConstraint(m_data->m_ChainRightConstraint, true);
  441. }
  442. chainNextFrame = 3.0;
  443. left = !left;
  444. }
  445. chainNextFrame -= m_data->mDt;
  446. /////// bouncing constraint: push the box periodically
  447. m_data->m_BouncingTranslateBody->setActivationState(ACTIVE_TAG);
  448. static float bounceNextFrame = -1;
  449. if(bounceNextFrame < 0)
  450. {
  451. m_data->m_BouncingTranslateBody->applyCentralImpulse(btVector3(10,0,0));
  452. bounceNextFrame = 3.0;
  453. }
  454. bounceNextFrame -= m_data->mDt;
  455. m_data->frameID++;
  456. }
  457. void Dof6ConstraintTutorial::stepSimulation(float deltaTime)
  458. {
  459. //animate();
  460. float time = m_data->m_timeSeriesCanvas->getCurrentTime();
  461. float prevPos = m_data->m_TranslateSpringBody->getWorldTransform().getOrigin().x();
  462. m_dynamicsWorld->stepSimulation(deltaTime);
  463. float xPos = m_data->m_TranslateSpringBody->getWorldTransform().getOrigin().x();
  464. m_data->m_timeSeriesCanvas->insertDataAtCurrentTime(xPos,0,true);
  465. m_data->m_timeSeriesCanvas->insertDataAtCurrentTime(m_data->m_TranslateSpringBody->getLinearVelocity().x(),1,true);
  466. if (deltaTime>0)
  467. {
  468. m_data->m_timeSeriesCanvas->insertDataAtCurrentTime((xPos-prevPos)/deltaTime,2,true);
  469. }
  470. prevPos = xPos;
  471. m_data->m_timeSeriesCanvas->nextTick();
  472. }
  473. class CommonExampleInterface* Dof6ConstraintTutorialCreateFunc( CommonExampleOptions& options)
  474. {
  475. return new Dof6ConstraintTutorial(options.m_guiHelper);
  476. }