Dof6Spring2Setup.cpp 18 KB

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