Dof6Spring2Setup.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 = 722;
  41. float yaw = 35;
  42. float targetPos[3]={4,2,-11};
  43. m_guiHelper->resetCamera(dist,pitch,yaw,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();localA.getOrigin() = btVector3(0,0,-5);
  141. localB.setIdentity();
  142. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_TranslateSpringBody, localA, localB EXTRAPARAMS);
  143. constraint->setLimit(0, 1,-1);
  144. constraint->setLimit(1, 0, 0);
  145. constraint->setLimit(2, 0, 0);
  146. constraint->setLimit(3, 0, 0);
  147. constraint->setLimit(4, 0, 0);
  148. constraint->setLimit(5, 0, 0);
  149. constraint->enableSpring(0, true);
  150. constraint->setStiffness(0, 100);
  151. #ifdef USE_6DOF2
  152. constraint->setDamping(0, 0);
  153. #else
  154. constraint->setDamping(0, 1);
  155. #endif
  156. constraint->setEquilibriumPoint(0, 0);
  157. constraint->setDbgDrawSize(btScalar(2.f));
  158. m_dynamicsWorld->addConstraint(constraint, true);
  159. }
  160. /////////// box with rotate spring, attached to static body
  161. /////////// box should swing (rotate) left-to-right forever
  162. {
  163. mass = 1.0;
  164. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  165. shape->calculateLocalInertia(mass,localInertia);
  166. bodyTransform.setIdentity();
  167. bodyTransform.getBasis().setEulerZYX(0,0,M_PI_2);
  168. motionState = new btDefaultMotionState(bodyTransform);
  169. m_data->m_RotateSpringBody = new btRigidBody(mass,motionState,shape,localInertia);
  170. m_data->m_RotateSpringBody->setActivationState(DISABLE_DEACTIVATION);
  171. m_dynamicsWorld->addRigidBody(m_data->m_RotateSpringBody);
  172. localA.setIdentity();localA.getOrigin() = btVector3(0,0,0);
  173. localB.setIdentity();localB.setOrigin(btVector3(0,0.5,0));
  174. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_RotateSpringBody, localA, localB EXTRAPARAMS);
  175. constraint->setLimit(0, 0, 0);
  176. constraint->setLimit(1, 0, 0);
  177. constraint->setLimit(2, 0, 0);
  178. constraint->setLimit(3, 0, 0);
  179. constraint->setLimit(4, 0, 0);
  180. constraint->setLimit(5, 1, -1);
  181. constraint->enableSpring(5, true);
  182. constraint->setStiffness(5, 100);
  183. #ifdef USE_6DOF2
  184. constraint->setDamping(5, 0);
  185. #else
  186. constraint->setDamping(5, 1);
  187. #endif
  188. constraint->setEquilibriumPoint(0, 0);
  189. constraint->setDbgDrawSize(btScalar(2.f));
  190. m_dynamicsWorld->addConstraint(constraint, true);
  191. }
  192. /////////// box with bouncing constraint, translation is bounced at the positive x limit, but not at the negative limit
  193. /////////// 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
  194. /////////// the box should move to the left (as an impulse will be applied to it periodically) until it reaches its limit, then bounce back
  195. {
  196. mass = 1.0;
  197. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  198. shape->calculateLocalInertia(mass,localInertia);
  199. bodyTransform.setIdentity();
  200. bodyTransform.setOrigin(btVector3(0,0,-3));
  201. motionState = new btDefaultMotionState(bodyTransform);
  202. m_data->m_BouncingTranslateBody = new btRigidBody(mass,motionState,shape,localInertia);
  203. m_data->m_BouncingTranslateBody->setActivationState(DISABLE_DEACTIVATION);
  204. m_data->m_BouncingTranslateBody->setDeactivationTime(btScalar(20000000));
  205. m_dynamicsWorld->addRigidBody(m_data->m_BouncingTranslateBody);
  206. localA.setIdentity();localA.getOrigin() = btVector3(0,0,0);
  207. localB.setIdentity();
  208. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_BouncingTranslateBody, localA, localB EXTRAPARAMS);
  209. constraint->setLimit(0, -2, SIMD_INFINITY);
  210. constraint->setLimit(1, 0, 0);
  211. constraint->setLimit(2, -3, -3);
  212. constraint->setLimit(3, 0, 0);
  213. constraint->setLimit(4, 0, 0);
  214. constraint->setLimit(5, 0, 0);
  215. #ifdef USE_6DOF2
  216. constraint->setBounce(0,0);
  217. #else //bounce is named restitution in 6dofspring, but not implemented for translational limit motor, so the following line has no effect
  218. constraint->getTranslationalLimitMotor()->m_restitution = 0.0;
  219. #endif
  220. constraint->setParam(BT_CONSTRAINT_STOP_ERP,0.995,0);
  221. constraint->setParam(BT_CONSTRAINT_STOP_CFM,0.0,0);
  222. constraint->setDbgDrawSize(btScalar(2.f));
  223. m_dynamicsWorld->addConstraint(constraint, true);
  224. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_BouncingTranslateBody, localA, localB EXTRAPARAMS);
  225. constraint->setLimit(0, -SIMD_INFINITY, 2);
  226. constraint->setLimit(1, 0, 0);
  227. constraint->setLimit(2, -3, -3);
  228. constraint->setLimit(3, 0, 0);
  229. constraint->setLimit(4, 0, 0);
  230. constraint->setLimit(5, 0, 0);
  231. #ifdef USE_6DOF2
  232. constraint->setBounce(0,1);
  233. #else //bounce is named restitution in 6dofspring, but not implemented for translational limit motor, so the following line has no effect
  234. constraint->getTranslationalLimitMotor()->m_restitution = 1.0;
  235. #endif
  236. constraint->setParam(BT_CONSTRAINT_STOP_ERP,0.995,0);
  237. constraint->setParam(BT_CONSTRAINT_STOP_CFM,0.0,0);
  238. constraint->setDbgDrawSize(btScalar(2.f));
  239. m_dynamicsWorld->addConstraint(constraint, true);
  240. }
  241. /////////// box with rotational motor, attached to static body
  242. /////////// the box should rotate around the y axis
  243. {
  244. mass = 1.0;
  245. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  246. shape->calculateLocalInertia(mass,localInertia);
  247. bodyTransform.setIdentity();
  248. bodyTransform.setOrigin(btVector3(4,0,0));
  249. motionState = new btDefaultMotionState(bodyTransform);
  250. m_data->m_MotorBody = new btRigidBody(mass,motionState,shape,localInertia);
  251. m_data->m_MotorBody->setActivationState(DISABLE_DEACTIVATION);
  252. m_dynamicsWorld->addRigidBody(m_data->m_MotorBody);
  253. localA.setIdentity();localA.getOrigin() = btVector3(4,0,0);
  254. localB.setIdentity();
  255. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_MotorBody, localA, localB EXTRAPARAMS);
  256. constraint->setLimit(0, 0, 0);
  257. constraint->setLimit(1, 0, 0);
  258. constraint->setLimit(2, 0, 0);
  259. constraint->setLimit(3, 0, 0);
  260. constraint->setLimit(4, 0, 0);
  261. constraint->setLimit(5, 1,-1);
  262. #ifdef USE_6DOF2
  263. constraint->enableMotor(5,true);
  264. constraint->setTargetVelocity(5,3.f);
  265. constraint->setMaxMotorForce(5,10.f);
  266. #else
  267. constraint->getRotationalLimitMotor(2)->m_enableMotor = true;
  268. constraint->getRotationalLimitMotor(2)->m_targetVelocity = 3.f;
  269. constraint->getRotationalLimitMotor(2)->m_maxMotorForce = 10;
  270. #endif
  271. constraint->setDbgDrawSize(btScalar(2.f));
  272. m_dynamicsWorld->addConstraint(constraint, true);
  273. }
  274. /////////// box with rotational servo motor, attached to static body
  275. /////////// the box should rotate around the y axis until it reaches its target
  276. /////////// the target will be negated periodically
  277. {
  278. mass = 1.0;
  279. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  280. shape->calculateLocalInertia(mass,localInertia);
  281. bodyTransform.setIdentity();
  282. bodyTransform.setOrigin(btVector3(7,0,0));
  283. motionState = new btDefaultMotionState(bodyTransform);
  284. m_data->m_ServoMotorBody = new btRigidBody(mass,motionState,shape,localInertia);
  285. m_data->m_ServoMotorBody->setActivationState(DISABLE_DEACTIVATION);
  286. m_dynamicsWorld->addRigidBody(m_data->m_ServoMotorBody);
  287. localA.setIdentity();localA.getOrigin() = btVector3(7,0,0);
  288. localB.setIdentity();
  289. constraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_ServoMotorBody, localA, localB EXTRAPARAMS);
  290. constraint->setLimit(0, 0, 0);
  291. constraint->setLimit(1, 0, 0);
  292. constraint->setLimit(2, 0, 0);
  293. constraint->setLimit(3, 0, 0);
  294. constraint->setLimit(4, 0, 0);
  295. constraint->setLimit(5, 1,-1);
  296. #ifdef USE_6DOF2
  297. constraint->enableMotor(5,true);
  298. constraint->setTargetVelocity(5,3.f);
  299. constraint->setMaxMotorForce(5,10.f);
  300. constraint->setServo(5,true);
  301. constraint->setServoTarget(5, M_PI_2);
  302. #else
  303. constraint->getRotationalLimitMotor(2)->m_enableMotor = true;
  304. constraint->getRotationalLimitMotor(2)->m_targetVelocity = 3.f;
  305. constraint->getRotationalLimitMotor(2)->m_maxMotorForce = 10;
  306. //servo motor is not implemented in 6dofspring constraint
  307. #endif
  308. constraint->setDbgDrawSize(btScalar(2.f));
  309. m_dynamicsWorld->addConstraint(constraint, true);
  310. m_data->m_ServoMotorConstraint = constraint;
  311. }
  312. ////////// chain of boxes linked together with fully limited rotational and translational constraints
  313. ////////// the chain will be pulled to the left and to the right periodically. They should strictly stick together.
  314. {
  315. btScalar limitConstraintStrength = 0.6;
  316. int bodycount = 10;
  317. btRigidBody* prevBody = 0;
  318. for(int i = 0; i < bodycount; ++i)
  319. {
  320. mass = 1.0;
  321. shape= new btBoxShape(btVector3(0.5,0.5,0.5));
  322. shape->calculateLocalInertia(mass,localInertia);
  323. bodyTransform.setIdentity();
  324. bodyTransform.setOrigin(btVector3(- i,0,3));
  325. motionState = new btDefaultMotionState(bodyTransform);
  326. btRigidBody* body = new btRigidBody(mass,motionState,shape,localInertia);
  327. body->setActivationState(DISABLE_DEACTIVATION);
  328. m_dynamicsWorld->addRigidBody(body);
  329. if(prevBody != 0)
  330. {
  331. localB.setIdentity();
  332. localB.setOrigin(btVector3(0.5,0,0));
  333. btTransform localA;
  334. localA.setIdentity();
  335. localA.setOrigin(btVector3(-0.5,0,0));
  336. CONSTRAINT_TYPE* constraint = new CONSTRAINT_TYPE(*prevBody, *body, localA, localB EXTRAPARAMS);
  337. constraint->setLimit(0, -0.01, 0.01);
  338. constraint->setLimit(1, 0, 0);
  339. constraint->setLimit(2, 0, 0);
  340. constraint->setLimit(3, 0, 0);
  341. constraint->setLimit(4, 0, 0);
  342. constraint->setLimit(5, 0, 0);
  343. for(int a = 0; a < 6; ++a)
  344. {
  345. constraint->setParam(BT_CONSTRAINT_STOP_ERP,0.9,a);
  346. constraint->setParam(BT_CONSTRAINT_STOP_CFM,0.0,a);
  347. }
  348. constraint->setDbgDrawSize(btScalar(1.f));
  349. m_dynamicsWorld->addConstraint(constraint, true);
  350. if(i < bodycount - 1)
  351. {
  352. localA.setIdentity();localA.getOrigin() = btVector3(0,0,3);
  353. localB.setIdentity();
  354. CONSTRAINT_TYPE* constraintZY = new CONSTRAINT_TYPE(*staticBody, *body, localA, localB EXTRAPARAMS);
  355. constraintZY->setLimit(0, 1, -1);
  356. constraintZY->setDbgDrawSize(btScalar(1.f));
  357. m_dynamicsWorld->addConstraint(constraintZY, true);
  358. }
  359. }
  360. else
  361. {
  362. localA.setIdentity();localA.getOrigin() = btVector3(bodycount,0,3);
  363. localB.setIdentity();
  364. localB.setOrigin(btVector3(0,0,0));
  365. m_data->m_ChainLeftBody = body;
  366. m_data->m_ChainLeftConstraint = new CONSTRAINT_TYPE(*staticBody, *body, localA, localB EXTRAPARAMS);
  367. m_data->m_ChainLeftConstraint->setLimit(3,0,0);
  368. m_data->m_ChainLeftConstraint->setLimit(4,0,0);
  369. m_data->m_ChainLeftConstraint->setLimit(5,0,0);
  370. for(int a = 0; a < 6; ++a)
  371. {
  372. m_data->m_ChainLeftConstraint->setParam(BT_CONSTRAINT_STOP_ERP,limitConstraintStrength,a);
  373. m_data->m_ChainLeftConstraint->setParam(BT_CONSTRAINT_STOP_CFM,0.0,a);
  374. }
  375. m_data->m_ChainLeftConstraint->setDbgDrawSize(btScalar(1.f));
  376. m_dynamicsWorld->addConstraint(m_data->m_ChainLeftConstraint, true);
  377. }
  378. prevBody = body;
  379. }
  380. m_data->m_ChainRightBody = prevBody;
  381. localA.setIdentity();localA.getOrigin() = btVector3(-bodycount,0,3);
  382. localB.setIdentity();
  383. localB.setOrigin(btVector3(0,0,0));
  384. m_data->m_ChainRightConstraint = new CONSTRAINT_TYPE(*staticBody, *m_data->m_ChainRightBody, localA, localB EXTRAPARAMS);
  385. m_data->m_ChainRightConstraint->setLimit(3,0,0);
  386. m_data->m_ChainRightConstraint->setLimit(4,0,0);
  387. m_data->m_ChainRightConstraint->setLimit(5,0,0);
  388. for(int a = 0; a < 6; ++a)
  389. {
  390. m_data->m_ChainRightConstraint->setParam(BT_CONSTRAINT_STOP_ERP,limitConstraintStrength,a);
  391. m_data->m_ChainRightConstraint->setParam(BT_CONSTRAINT_STOP_CFM,0.0,a);
  392. }
  393. }
  394. m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
  395. }
  396. void Dof6Spring2Setup::animate()
  397. {
  398. /////// servo motor: flip its target periodically
  399. #ifdef USE_6DOF2
  400. static float servoNextFrame = -1;
  401. btScalar pos = m_data->m_ServoMotorConstraint->getRotationalLimitMotor(2)->m_currentPosition;
  402. btScalar target = m_data->m_ServoMotorConstraint->getRotationalLimitMotor(2)->m_servoTarget;
  403. if(servoNextFrame < 0)
  404. {
  405. m_data->m_ServoMotorConstraint->getRotationalLimitMotor(2)->m_servoTarget *= -1;
  406. servoNextFrame = 3.0;
  407. }
  408. servoNextFrame -= m_data->mDt;
  409. #endif
  410. /////// constraint chain: pull the chain left and right periodically
  411. static float chainNextFrame = -1;
  412. static bool left = true;
  413. if(chainNextFrame < 0)
  414. {
  415. if(!left)
  416. {
  417. m_data->m_ChainRightBody->setActivationState(ACTIVE_TAG);
  418. m_dynamicsWorld->removeConstraint(m_data->m_ChainRightConstraint);
  419. m_data->m_ChainLeftConstraint->setDbgDrawSize(btScalar(2.f));
  420. m_dynamicsWorld->addConstraint(m_data->m_ChainLeftConstraint, true);
  421. }
  422. else
  423. {
  424. m_data->m_ChainLeftBody->setActivationState(ACTIVE_TAG);
  425. m_dynamicsWorld->removeConstraint(m_data->m_ChainLeftConstraint);
  426. m_data->m_ChainRightConstraint->setDbgDrawSize(btScalar(2.f));
  427. m_dynamicsWorld->addConstraint(m_data->m_ChainRightConstraint, true);
  428. }
  429. chainNextFrame = 3.0;
  430. left = !left;
  431. }
  432. chainNextFrame -= m_data->mDt;
  433. /////// bouncing constraint: push the box periodically
  434. m_data->m_BouncingTranslateBody->setActivationState(ACTIVE_TAG);
  435. static float bounceNextFrame = -1;
  436. if(bounceNextFrame < 0)
  437. {
  438. m_data->m_BouncingTranslateBody->applyCentralImpulse(btVector3(10,0,0));
  439. bounceNextFrame = 3.0;
  440. }
  441. bounceNextFrame -= m_data->mDt;
  442. m_data->frameID++;
  443. }
  444. void Dof6Spring2Setup::stepSimulation(float deltaTime)
  445. {
  446. animate();
  447. m_dynamicsWorld->stepSimulation(deltaTime);
  448. }
  449. class CommonExampleInterface* Dof6Spring2CreateFunc( CommonExampleOptions& options)
  450. {
  451. return new Dof6Spring2Setup(options.m_guiHelper);
  452. }