2
0

btTypedConstraint.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2010 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_TYPED_CONSTRAINT_H
  14. #define BT_TYPED_CONSTRAINT_H
  15. #include "LinearMath/btScalar.h"
  16. #include "btSolverConstraint.h"
  17. #include "BulletDynamics/Dynamics/btRigidBody.h"
  18. #ifdef BT_USE_DOUBLE_PRECISION
  19. #define btTypedConstraintData2 btTypedConstraintDoubleData
  20. #define btTypedConstraintDataName "btTypedConstraintDoubleData"
  21. #else
  22. #define btTypedConstraintData2 btTypedConstraintFloatData
  23. #define btTypedConstraintDataName "btTypedConstraintFloatData"
  24. #endif //BT_USE_DOUBLE_PRECISION
  25. class btSerializer;
  26. //Don't change any of the existing enum values, so add enum types at the end for serialization compatibility
  27. enum btTypedConstraintType
  28. {
  29. POINT2POINT_CONSTRAINT_TYPE=3,
  30. HINGE_CONSTRAINT_TYPE,
  31. CONETWIST_CONSTRAINT_TYPE,
  32. D6_CONSTRAINT_TYPE,
  33. SLIDER_CONSTRAINT_TYPE,
  34. CONTACT_CONSTRAINT_TYPE,
  35. D6_SPRING_CONSTRAINT_TYPE,
  36. GEAR_CONSTRAINT_TYPE,
  37. FIXED_CONSTRAINT_TYPE,
  38. D6_SPRING_2_CONSTRAINT_TYPE,
  39. MAX_CONSTRAINT_TYPE
  40. };
  41. enum btConstraintParams
  42. {
  43. BT_CONSTRAINT_ERP=1,
  44. BT_CONSTRAINT_STOP_ERP,
  45. BT_CONSTRAINT_CFM,
  46. BT_CONSTRAINT_STOP_CFM
  47. };
  48. #if 1
  49. #define btAssertConstrParams(_par) btAssert(_par)
  50. #else
  51. #define btAssertConstrParams(_par)
  52. #endif
  53. ATTRIBUTE_ALIGNED16(struct) btJointFeedback
  54. {
  55. btVector3 m_appliedForceBodyA;
  56. btVector3 m_appliedTorqueBodyA;
  57. btVector3 m_appliedForceBodyB;
  58. btVector3 m_appliedTorqueBodyB;
  59. };
  60. ///TypedConstraint is the baseclass for Bullet constraints and vehicles
  61. ATTRIBUTE_ALIGNED16(class) btTypedConstraint : public btTypedObject
  62. {
  63. int m_userConstraintType;
  64. union
  65. {
  66. int m_userConstraintId;
  67. void* m_userConstraintPtr;
  68. };
  69. btScalar m_breakingImpulseThreshold;
  70. bool m_isEnabled;
  71. bool m_needsFeedback;
  72. int m_overrideNumSolverIterations;
  73. btTypedConstraint& operator=(btTypedConstraint& other)
  74. {
  75. btAssert(0);
  76. (void) other;
  77. return *this;
  78. }
  79. protected:
  80. btRigidBody& m_rbA;
  81. btRigidBody& m_rbB;
  82. btScalar m_appliedImpulse;
  83. btScalar m_dbgDrawSize;
  84. btJointFeedback* m_jointFeedback;
  85. ///internal method used by the constraint solver, don't use them directly
  86. btScalar getMotorFactor(btScalar pos, btScalar lowLim, btScalar uppLim, btScalar vel, btScalar timeFact);
  87. public:
  88. BT_DECLARE_ALIGNED_ALLOCATOR();
  89. virtual ~btTypedConstraint() {};
  90. btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA);
  91. btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA,btRigidBody& rbB);
  92. struct btConstraintInfo1 {
  93. int m_numConstraintRows,nub;
  94. };
  95. static btRigidBody& getFixedBody();
  96. struct btConstraintInfo2 {
  97. // integrator parameters: frames per second (1/stepsize), default error
  98. // reduction parameter (0..1).
  99. btScalar fps,erp;
  100. // for the first and second body, pointers to two (linear and angular)
  101. // n*3 jacobian sub matrices, stored by rows. these matrices will have
  102. // been initialized to 0 on entry. if the second body is zero then the
  103. // J2xx pointers may be 0.
  104. btScalar *m_J1linearAxis,*m_J1angularAxis,*m_J2linearAxis,*m_J2angularAxis;
  105. // elements to jump from one row to the next in J's
  106. int rowskip;
  107. // right hand sides of the equation J*v = c + cfm * lambda. cfm is the
  108. // "constraint force mixing" vector. c is set to zero on entry, cfm is
  109. // set to a constant value (typically very small or zero) value on entry.
  110. btScalar *m_constraintError,*cfm;
  111. // lo and hi limits for variables (set to -/+ infinity on entry).
  112. btScalar *m_lowerLimit,*m_upperLimit;
  113. // findex vector for variables. see the LCP solver interface for a
  114. // description of what this does. this is set to -1 on entry.
  115. // note that the returned indexes are relative to the first index of
  116. // the constraint.
  117. int *findex;
  118. // number of solver iterations
  119. int m_numIterations;
  120. //damping of the velocity
  121. btScalar m_damping;
  122. };
  123. int getOverrideNumSolverIterations() const
  124. {
  125. return m_overrideNumSolverIterations;
  126. }
  127. ///override the number of constraint solver iterations used to solve this constraint
  128. ///-1 will use the default number of iterations, as specified in SolverInfo.m_numIterations
  129. void setOverrideNumSolverIterations(int overideNumIterations)
  130. {
  131. m_overrideNumSolverIterations = overideNumIterations;
  132. }
  133. ///internal method used by the constraint solver, don't use them directly
  134. virtual void buildJacobian() {};
  135. ///internal method used by the constraint solver, don't use them directly
  136. virtual void setupSolverConstraint(btConstraintArray& ca, int solverBodyA,int solverBodyB, btScalar timeStep)
  137. {
  138. (void)ca;
  139. (void)solverBodyA;
  140. (void)solverBodyB;
  141. (void)timeStep;
  142. }
  143. ///internal method used by the constraint solver, don't use them directly
  144. virtual void getInfo1 (btConstraintInfo1* info)=0;
  145. ///internal method used by the constraint solver, don't use them directly
  146. virtual void getInfo2 (btConstraintInfo2* info)=0;
  147. ///internal method used by the constraint solver, don't use them directly
  148. void internalSetAppliedImpulse(btScalar appliedImpulse)
  149. {
  150. m_appliedImpulse = appliedImpulse;
  151. }
  152. ///internal method used by the constraint solver, don't use them directly
  153. btScalar internalGetAppliedImpulse()
  154. {
  155. return m_appliedImpulse;
  156. }
  157. btScalar getBreakingImpulseThreshold() const
  158. {
  159. return m_breakingImpulseThreshold;
  160. }
  161. void setBreakingImpulseThreshold(btScalar threshold)
  162. {
  163. m_breakingImpulseThreshold = threshold;
  164. }
  165. bool isEnabled() const
  166. {
  167. return m_isEnabled;
  168. }
  169. void setEnabled(bool enabled)
  170. {
  171. m_isEnabled=enabled;
  172. }
  173. ///internal method used by the constraint solver, don't use them directly
  174. virtual void solveConstraintObsolete(btSolverBody& /*bodyA*/,btSolverBody& /*bodyB*/,btScalar /*timeStep*/) {};
  175. const btRigidBody& getRigidBodyA() const
  176. {
  177. return m_rbA;
  178. }
  179. const btRigidBody& getRigidBodyB() const
  180. {
  181. return m_rbB;
  182. }
  183. btRigidBody& getRigidBodyA()
  184. {
  185. return m_rbA;
  186. }
  187. btRigidBody& getRigidBodyB()
  188. {
  189. return m_rbB;
  190. }
  191. int getUserConstraintType() const
  192. {
  193. return m_userConstraintType ;
  194. }
  195. void setUserConstraintType(int userConstraintType)
  196. {
  197. m_userConstraintType = userConstraintType;
  198. };
  199. void setUserConstraintId(int uid)
  200. {
  201. m_userConstraintId = uid;
  202. }
  203. int getUserConstraintId() const
  204. {
  205. return m_userConstraintId;
  206. }
  207. void setUserConstraintPtr(void* ptr)
  208. {
  209. m_userConstraintPtr = ptr;
  210. }
  211. void* getUserConstraintPtr()
  212. {
  213. return m_userConstraintPtr;
  214. }
  215. void setJointFeedback(btJointFeedback* jointFeedback)
  216. {
  217. m_jointFeedback = jointFeedback;
  218. }
  219. const btJointFeedback* getJointFeedback() const
  220. {
  221. return m_jointFeedback;
  222. }
  223. btJointFeedback* getJointFeedback()
  224. {
  225. return m_jointFeedback;
  226. }
  227. int getUid() const
  228. {
  229. return m_userConstraintId;
  230. }
  231. bool needsFeedback() const
  232. {
  233. return m_needsFeedback;
  234. }
  235. ///enableFeedback will allow to read the applied linear and angular impulse
  236. ///use getAppliedImpulse, getAppliedLinearImpulse and getAppliedAngularImpulse to read feedback information
  237. void enableFeedback(bool needsFeedback)
  238. {
  239. m_needsFeedback = needsFeedback;
  240. }
  241. ///getAppliedImpulse is an estimated total applied impulse.
  242. ///This feedback could be used to determine breaking constraints or playing sounds.
  243. btScalar getAppliedImpulse() const
  244. {
  245. btAssert(m_needsFeedback);
  246. return m_appliedImpulse;
  247. }
  248. btTypedConstraintType getConstraintType () const
  249. {
  250. return btTypedConstraintType(m_objectType);
  251. }
  252. void setDbgDrawSize(btScalar dbgDrawSize)
  253. {
  254. m_dbgDrawSize = dbgDrawSize;
  255. }
  256. btScalar getDbgDrawSize()
  257. {
  258. return m_dbgDrawSize;
  259. }
  260. ///override the default global value of a parameter (such as ERP or CFM), optionally provide the axis (0..5).
  261. ///If no axis is provided, it uses the default axis for this constraint.
  262. virtual void setParam(int num, btScalar value, int axis = -1) = 0;
  263. ///return the local value of parameter
  264. virtual btScalar getParam(int num, int axis = -1) const = 0;
  265. virtual int calculateSerializeBufferSize() const;
  266. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  267. virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
  268. };
  269. // returns angle in range [-SIMD_2_PI, SIMD_2_PI], closest to one of the limits
  270. // all arguments should be normalized angles (i.e. in range [-SIMD_PI, SIMD_PI])
  271. SIMD_FORCE_INLINE btScalar btAdjustAngleToLimits(btScalar angleInRadians, btScalar angleLowerLimitInRadians, btScalar angleUpperLimitInRadians)
  272. {
  273. if(angleLowerLimitInRadians >= angleUpperLimitInRadians)
  274. {
  275. return angleInRadians;
  276. }
  277. else if(angleInRadians < angleLowerLimitInRadians)
  278. {
  279. btScalar diffLo = btFabs(btNormalizeAngle(angleLowerLimitInRadians - angleInRadians));
  280. btScalar diffHi = btFabs(btNormalizeAngle(angleUpperLimitInRadians - angleInRadians));
  281. return (diffLo < diffHi) ? angleInRadians : (angleInRadians + SIMD_2_PI);
  282. }
  283. else if(angleInRadians > angleUpperLimitInRadians)
  284. {
  285. btScalar diffHi = btFabs(btNormalizeAngle(angleInRadians - angleUpperLimitInRadians));
  286. btScalar diffLo = btFabs(btNormalizeAngle(angleInRadians - angleLowerLimitInRadians));
  287. return (diffLo < diffHi) ? (angleInRadians - SIMD_2_PI) : angleInRadians;
  288. }
  289. else
  290. {
  291. return angleInRadians;
  292. }
  293. }
  294. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  295. struct btTypedConstraintFloatData
  296. {
  297. btRigidBodyFloatData *m_rbA;
  298. btRigidBodyFloatData *m_rbB;
  299. char *m_name;
  300. int m_objectType;
  301. int m_userConstraintType;
  302. int m_userConstraintId;
  303. int m_needsFeedback;
  304. float m_appliedImpulse;
  305. float m_dbgDrawSize;
  306. int m_disableCollisionsBetweenLinkedBodies;
  307. int m_overrideNumSolverIterations;
  308. float m_breakingImpulseThreshold;
  309. int m_isEnabled;
  310. };
  311. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  312. #define BT_BACKWARDS_COMPATIBLE_SERIALIZATION
  313. #ifdef BT_BACKWARDS_COMPATIBLE_SERIALIZATION
  314. ///this structure is not used, except for loading pre-2.82 .bullet files
  315. struct btTypedConstraintData
  316. {
  317. btRigidBodyData *m_rbA;
  318. btRigidBodyData *m_rbB;
  319. char *m_name;
  320. int m_objectType;
  321. int m_userConstraintType;
  322. int m_userConstraintId;
  323. int m_needsFeedback;
  324. float m_appliedImpulse;
  325. float m_dbgDrawSize;
  326. int m_disableCollisionsBetweenLinkedBodies;
  327. int m_overrideNumSolverIterations;
  328. float m_breakingImpulseThreshold;
  329. int m_isEnabled;
  330. };
  331. #endif //BACKWARDS_COMPATIBLE
  332. struct btTypedConstraintDoubleData
  333. {
  334. btRigidBodyDoubleData *m_rbA;
  335. btRigidBodyDoubleData *m_rbB;
  336. char *m_name;
  337. int m_objectType;
  338. int m_userConstraintType;
  339. int m_userConstraintId;
  340. int m_needsFeedback;
  341. double m_appliedImpulse;
  342. double m_dbgDrawSize;
  343. int m_disableCollisionsBetweenLinkedBodies;
  344. int m_overrideNumSolverIterations;
  345. double m_breakingImpulseThreshold;
  346. int m_isEnabled;
  347. char padding[4];
  348. };
  349. SIMD_FORCE_INLINE int btTypedConstraint::calculateSerializeBufferSize() const
  350. {
  351. return sizeof(btTypedConstraintData2);
  352. }
  353. class btAngularLimit
  354. {
  355. private:
  356. btScalar
  357. m_center,
  358. m_halfRange,
  359. m_softness,
  360. m_biasFactor,
  361. m_relaxationFactor,
  362. m_correction,
  363. m_sign;
  364. bool
  365. m_solveLimit;
  366. public:
  367. /// Default constructor initializes limit as inactive, allowing free constraint movement
  368. btAngularLimit()
  369. :m_center(0.0f),
  370. m_halfRange(-1.0f),
  371. m_softness(0.9f),
  372. m_biasFactor(0.3f),
  373. m_relaxationFactor(1.0f),
  374. m_correction(0.0f),
  375. m_sign(0.0f),
  376. m_solveLimit(false)
  377. {}
  378. /// Sets all limit's parameters.
  379. /// When low > high limit becomes inactive.
  380. /// When high - low > 2PI limit is ineffective too becouse no angle can exceed the limit
  381. void set(btScalar low, btScalar high, btScalar _softness = 0.9f, btScalar _biasFactor = 0.3f, btScalar _relaxationFactor = 1.0f);
  382. /// Checks conastaint angle against limit. If limit is active and the angle violates the limit
  383. /// correction is calculated.
  384. void test(const btScalar angle);
  385. /// Returns limit's softness
  386. inline btScalar getSoftness() const
  387. {
  388. return m_softness;
  389. }
  390. /// Returns limit's bias factor
  391. inline btScalar getBiasFactor() const
  392. {
  393. return m_biasFactor;
  394. }
  395. /// Returns limit's relaxation factor
  396. inline btScalar getRelaxationFactor() const
  397. {
  398. return m_relaxationFactor;
  399. }
  400. /// Returns correction value evaluated when test() was invoked
  401. inline btScalar getCorrection() const
  402. {
  403. return m_correction;
  404. }
  405. /// Returns sign value evaluated when test() was invoked
  406. inline btScalar getSign() const
  407. {
  408. return m_sign;
  409. }
  410. /// Gives half of the distance between min and max limit angle
  411. inline btScalar getHalfRange() const
  412. {
  413. return m_halfRange;
  414. }
  415. /// Returns true when the last test() invocation recognized limit violation
  416. inline bool isLimit() const
  417. {
  418. return m_solveLimit;
  419. }
  420. /// Checks given angle against limit. If limit is active and angle doesn't fit it, the angle
  421. /// returned is modified so it equals to the limit closest to given angle.
  422. void fit(btScalar& angle) const;
  423. /// Returns correction value multiplied by sign value
  424. btScalar getError() const;
  425. btScalar getLow() const;
  426. btScalar getHigh() const;
  427. };
  428. #endif //BT_TYPED_CONSTRAINT_H