btHingeConstraint.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 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. /* Hinge Constraint by Dirk Gregorius. Limits added by Marcus Hennix at Starbreeze Studios */
  14. #ifndef BT_HINGECONSTRAINT_H
  15. #define BT_HINGECONSTRAINT_H
  16. #define _BT_USE_CENTER_LIMIT_ 1
  17. #include "LinearMath/btVector3.h"
  18. #include "btJacobianEntry.h"
  19. #include "btTypedConstraint.h"
  20. class btRigidBody;
  21. #ifdef BT_USE_DOUBLE_PRECISION
  22. #define btHingeConstraintData btHingeConstraintDoubleData2 //rename to 2 for backwards compatibility, so we can still load the 'btHingeConstraintDoubleData' version
  23. #define btHingeConstraintDataName "btHingeConstraintDoubleData2"
  24. #else
  25. #define btHingeConstraintData btHingeConstraintFloatData
  26. #define btHingeConstraintDataName "btHingeConstraintFloatData"
  27. #endif //BT_USE_DOUBLE_PRECISION
  28. enum btHingeFlags
  29. {
  30. BT_HINGE_FLAGS_CFM_STOP = 1,
  31. BT_HINGE_FLAGS_ERP_STOP = 2,
  32. BT_HINGE_FLAGS_CFM_NORM = 4,
  33. BT_HINGE_FLAGS_ERP_NORM = 8
  34. };
  35. /// hinge constraint between two rigidbodies each with a pivotpoint that descibes the axis location in local space
  36. /// axis defines the orientation of the hinge axis
  37. ATTRIBUTE_ALIGNED16(class)
  38. btHingeConstraint : public btTypedConstraint
  39. {
  40. #ifdef IN_PARALLELL_SOLVER
  41. public:
  42. #endif
  43. btJacobianEntry m_jac[3]; //3 orthogonal linear constraints
  44. btJacobianEntry m_jacAng[3]; //2 orthogonal angular constraints+ 1 for limit/motor
  45. btTransform m_rbAFrame; // constraint axii. Assumes z is hinge axis.
  46. btTransform m_rbBFrame;
  47. btScalar m_motorTargetVelocity;
  48. btScalar m_maxMotorImpulse;
  49. #ifdef _BT_USE_CENTER_LIMIT_
  50. btAngularLimit m_limit;
  51. #else
  52. btScalar m_lowerLimit;
  53. btScalar m_upperLimit;
  54. btScalar m_limitSign;
  55. btScalar m_correction;
  56. btScalar m_limitSoftness;
  57. btScalar m_biasFactor;
  58. btScalar m_relaxationFactor;
  59. bool m_solveLimit;
  60. #endif
  61. btScalar m_kHinge;
  62. btScalar m_accLimitImpulse;
  63. btScalar m_hingeAngle;
  64. btScalar m_referenceSign;
  65. bool m_angularOnly;
  66. bool m_enableAngularMotor;
  67. bool m_useSolveConstraintObsolete;
  68. bool m_useOffsetForConstraintFrame;
  69. bool m_useReferenceFrameA;
  70. btScalar m_accMotorImpulse;
  71. int m_flags;
  72. btScalar m_normalCFM;
  73. btScalar m_normalERP;
  74. btScalar m_stopCFM;
  75. btScalar m_stopERP;
  76. public:
  77. BT_DECLARE_ALIGNED_ALLOCATOR();
  78. btHingeConstraint(btRigidBody & rbA, btRigidBody & rbB, const btVector3& pivotInA, const btVector3& pivotInB, const btVector3& axisInA, const btVector3& axisInB, bool useReferenceFrameA = false);
  79. btHingeConstraint(btRigidBody & rbA, const btVector3& pivotInA, const btVector3& axisInA, bool useReferenceFrameA = false);
  80. btHingeConstraint(btRigidBody & rbA, btRigidBody & rbB, const btTransform& rbAFrame, const btTransform& rbBFrame, bool useReferenceFrameA = false);
  81. btHingeConstraint(btRigidBody & rbA, const btTransform& rbAFrame, bool useReferenceFrameA = false);
  82. virtual void buildJacobian();
  83. virtual void getInfo1(btConstraintInfo1 * info);
  84. void getInfo1NonVirtual(btConstraintInfo1 * info);
  85. virtual void getInfo2(btConstraintInfo2 * info);
  86. void getInfo2NonVirtual(btConstraintInfo2 * info, const btTransform& transA, const btTransform& transB, const btVector3& angVelA, const btVector3& angVelB);
  87. void getInfo2Internal(btConstraintInfo2 * info, const btTransform& transA, const btTransform& transB, const btVector3& angVelA, const btVector3& angVelB);
  88. void getInfo2InternalUsingFrameOffset(btConstraintInfo2 * info, const btTransform& transA, const btTransform& transB, const btVector3& angVelA, const btVector3& angVelB);
  89. void updateRHS(btScalar timeStep);
  90. const btRigidBody& getRigidBodyA() const
  91. {
  92. return m_rbA;
  93. }
  94. const btRigidBody& getRigidBodyB() const
  95. {
  96. return m_rbB;
  97. }
  98. btRigidBody& getRigidBodyA()
  99. {
  100. return m_rbA;
  101. }
  102. btRigidBody& getRigidBodyB()
  103. {
  104. return m_rbB;
  105. }
  106. btTransform& getFrameOffsetA()
  107. {
  108. return m_rbAFrame;
  109. }
  110. btTransform& getFrameOffsetB()
  111. {
  112. return m_rbBFrame;
  113. }
  114. void setFrames(const btTransform& frameA, const btTransform& frameB);
  115. void setAngularOnly(bool angularOnly)
  116. {
  117. m_angularOnly = angularOnly;
  118. }
  119. void enableAngularMotor(bool enableMotor, btScalar targetVelocity, btScalar maxMotorImpulse)
  120. {
  121. m_enableAngularMotor = enableMotor;
  122. m_motorTargetVelocity = targetVelocity;
  123. m_maxMotorImpulse = maxMotorImpulse;
  124. }
  125. // extra motor API, including ability to set a target rotation (as opposed to angular velocity)
  126. // note: setMotorTarget sets angular velocity under the hood, so you must call it every tick to
  127. // maintain a given angular target.
  128. void enableMotor(bool enableMotor) { m_enableAngularMotor = enableMotor; }
  129. void setMaxMotorImpulse(btScalar maxMotorImpulse) { m_maxMotorImpulse = maxMotorImpulse; }
  130. void setMotorTargetVelocity(btScalar motorTargetVelocity) { m_motorTargetVelocity = motorTargetVelocity; }
  131. void setMotorTarget(const btQuaternion& qAinB, btScalar dt); // qAinB is rotation of body A wrt body B.
  132. void setMotorTarget(btScalar targetAngle, btScalar dt);
  133. void setLimit(btScalar low, btScalar high, btScalar _softness = 0.9f, btScalar _biasFactor = 0.3f, btScalar _relaxationFactor = 1.0f)
  134. {
  135. #ifdef _BT_USE_CENTER_LIMIT_
  136. m_limit.set(low, high, _softness, _biasFactor, _relaxationFactor);
  137. #else
  138. m_lowerLimit = btNormalizeAngle(low);
  139. m_upperLimit = btNormalizeAngle(high);
  140. m_limitSoftness = _softness;
  141. m_biasFactor = _biasFactor;
  142. m_relaxationFactor = _relaxationFactor;
  143. #endif
  144. }
  145. btScalar getLimitSoftness() const
  146. {
  147. #ifdef _BT_USE_CENTER_LIMIT_
  148. return m_limit.getSoftness();
  149. #else
  150. return m_limitSoftness;
  151. #endif
  152. }
  153. btScalar getLimitBiasFactor() const
  154. {
  155. #ifdef _BT_USE_CENTER_LIMIT_
  156. return m_limit.getBiasFactor();
  157. #else
  158. return m_biasFactor;
  159. #endif
  160. }
  161. btScalar getLimitRelaxationFactor() const
  162. {
  163. #ifdef _BT_USE_CENTER_LIMIT_
  164. return m_limit.getRelaxationFactor();
  165. #else
  166. return m_relaxationFactor;
  167. #endif
  168. }
  169. void setAxis(btVector3 & axisInA)
  170. {
  171. btVector3 rbAxisA1, rbAxisA2;
  172. btPlaneSpace1(axisInA, rbAxisA1, rbAxisA2);
  173. btVector3 pivotInA = m_rbAFrame.getOrigin();
  174. // m_rbAFrame.getOrigin() = pivotInA;
  175. m_rbAFrame.getBasis().setValue(rbAxisA1.getX(), rbAxisA2.getX(), axisInA.getX(),
  176. rbAxisA1.getY(), rbAxisA2.getY(), axisInA.getY(),
  177. rbAxisA1.getZ(), rbAxisA2.getZ(), axisInA.getZ());
  178. btVector3 axisInB = m_rbA.getCenterOfMassTransform().getBasis() * axisInA;
  179. btQuaternion rotationArc = shortestArcQuat(axisInA, axisInB);
  180. btVector3 rbAxisB1 = quatRotate(rotationArc, rbAxisA1);
  181. btVector3 rbAxisB2 = axisInB.cross(rbAxisB1);
  182. m_rbBFrame.getOrigin() = m_rbB.getCenterOfMassTransform().inverse()(m_rbA.getCenterOfMassTransform()(pivotInA));
  183. m_rbBFrame.getBasis().setValue(rbAxisB1.getX(), rbAxisB2.getX(), axisInB.getX(),
  184. rbAxisB1.getY(), rbAxisB2.getY(), axisInB.getY(),
  185. rbAxisB1.getZ(), rbAxisB2.getZ(), axisInB.getZ());
  186. m_rbBFrame.getBasis() = m_rbB.getCenterOfMassTransform().getBasis().inverse() * m_rbBFrame.getBasis();
  187. }
  188. bool hasLimit() const
  189. {
  190. #ifdef _BT_USE_CENTER_LIMIT_
  191. return m_limit.getHalfRange() > 0;
  192. #else
  193. return m_lowerLimit <= m_upperLimit;
  194. #endif
  195. }
  196. btScalar getLowerLimit() const
  197. {
  198. #ifdef _BT_USE_CENTER_LIMIT_
  199. return m_limit.getLow();
  200. #else
  201. return m_lowerLimit;
  202. #endif
  203. }
  204. btScalar getUpperLimit() const
  205. {
  206. #ifdef _BT_USE_CENTER_LIMIT_
  207. return m_limit.getHigh();
  208. #else
  209. return m_upperLimit;
  210. #endif
  211. }
  212. ///The getHingeAngle gives the hinge angle in range [-PI,PI]
  213. btScalar getHingeAngle();
  214. btScalar getHingeAngle(const btTransform& transA, const btTransform& transB);
  215. void testLimit(const btTransform& transA, const btTransform& transB);
  216. const btTransform& getAFrame() const { return m_rbAFrame; };
  217. const btTransform& getBFrame() const { return m_rbBFrame; };
  218. btTransform& getAFrame() { return m_rbAFrame; };
  219. btTransform& getBFrame() { return m_rbBFrame; };
  220. inline int getSolveLimit()
  221. {
  222. #ifdef _BT_USE_CENTER_LIMIT_
  223. return m_limit.isLimit();
  224. #else
  225. return m_solveLimit;
  226. #endif
  227. }
  228. inline btScalar getLimitSign()
  229. {
  230. #ifdef _BT_USE_CENTER_LIMIT_
  231. return m_limit.getSign();
  232. #else
  233. return m_limitSign;
  234. #endif
  235. }
  236. inline bool getAngularOnly()
  237. {
  238. return m_angularOnly;
  239. }
  240. inline bool getEnableAngularMotor()
  241. {
  242. return m_enableAngularMotor;
  243. }
  244. inline btScalar getMotorTargetVelocity()
  245. {
  246. return m_motorTargetVelocity;
  247. }
  248. inline btScalar getMaxMotorImpulse()
  249. {
  250. return m_maxMotorImpulse;
  251. }
  252. // access for UseFrameOffset
  253. bool getUseFrameOffset() { return m_useOffsetForConstraintFrame; }
  254. void setUseFrameOffset(bool frameOffsetOnOff) { m_useOffsetForConstraintFrame = frameOffsetOnOff; }
  255. // access for UseReferenceFrameA
  256. bool getUseReferenceFrameA() const { return m_useReferenceFrameA; }
  257. void setUseReferenceFrameA(bool useReferenceFrameA) { m_useReferenceFrameA = useReferenceFrameA; }
  258. ///override the default global value of a parameter (such as ERP or CFM), optionally provide the axis (0..5).
  259. ///If no axis is provided, it uses the default axis for this constraint.
  260. virtual void setParam(int num, btScalar value, int axis = -1);
  261. ///return the local value of parameter
  262. virtual btScalar getParam(int num, int axis = -1) const;
  263. virtual int getFlags() const
  264. {
  265. return m_flags;
  266. }
  267. virtual int calculateSerializeBufferSize() const;
  268. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  269. virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
  270. };
  271. //only for backward compatibility
  272. #ifdef BT_BACKWARDS_COMPATIBLE_SERIALIZATION
  273. ///this structure is not used, except for loading pre-2.82 .bullet files
  274. struct btHingeConstraintDoubleData
  275. {
  276. btTypedConstraintData m_typeConstraintData;
  277. btTransformDoubleData m_rbAFrame; // constraint axii. Assumes z is hinge axis.
  278. btTransformDoubleData m_rbBFrame;
  279. int m_useReferenceFrameA;
  280. int m_angularOnly;
  281. int m_enableAngularMotor;
  282. float m_motorTargetVelocity;
  283. float m_maxMotorImpulse;
  284. float m_lowerLimit;
  285. float m_upperLimit;
  286. float m_limitSoftness;
  287. float m_biasFactor;
  288. float m_relaxationFactor;
  289. };
  290. #endif //BT_BACKWARDS_COMPATIBLE_SERIALIZATION
  291. ///The getAccumulatedHingeAngle returns the accumulated hinge angle, taking rotation across the -PI/PI boundary into account
  292. ATTRIBUTE_ALIGNED16(class)
  293. btHingeAccumulatedAngleConstraint : public btHingeConstraint
  294. {
  295. protected:
  296. btScalar m_accumulatedAngle;
  297. public:
  298. BT_DECLARE_ALIGNED_ALLOCATOR();
  299. btHingeAccumulatedAngleConstraint(btRigidBody & rbA, btRigidBody & rbB, const btVector3& pivotInA, const btVector3& pivotInB, const btVector3& axisInA, const btVector3& axisInB, bool useReferenceFrameA = false)
  300. : btHingeConstraint(rbA, rbB, pivotInA, pivotInB, axisInA, axisInB, useReferenceFrameA)
  301. {
  302. m_accumulatedAngle = getHingeAngle();
  303. }
  304. btHingeAccumulatedAngleConstraint(btRigidBody & rbA, const btVector3& pivotInA, const btVector3& axisInA, bool useReferenceFrameA = false)
  305. : btHingeConstraint(rbA, pivotInA, axisInA, useReferenceFrameA)
  306. {
  307. m_accumulatedAngle = getHingeAngle();
  308. }
  309. btHingeAccumulatedAngleConstraint(btRigidBody & rbA, btRigidBody & rbB, const btTransform& rbAFrame, const btTransform& rbBFrame, bool useReferenceFrameA = false)
  310. : btHingeConstraint(rbA, rbB, rbAFrame, rbBFrame, useReferenceFrameA)
  311. {
  312. m_accumulatedAngle = getHingeAngle();
  313. }
  314. btHingeAccumulatedAngleConstraint(btRigidBody & rbA, const btTransform& rbAFrame, bool useReferenceFrameA = false)
  315. : btHingeConstraint(rbA, rbAFrame, useReferenceFrameA)
  316. {
  317. m_accumulatedAngle = getHingeAngle();
  318. }
  319. btScalar getAccumulatedHingeAngle();
  320. void setAccumulatedHingeAngle(btScalar accAngle);
  321. virtual void getInfo1(btConstraintInfo1 * info);
  322. };
  323. struct btHingeConstraintFloatData
  324. {
  325. btTypedConstraintData m_typeConstraintData;
  326. btTransformFloatData m_rbAFrame; // constraint axii. Assumes z is hinge axis.
  327. btTransformFloatData m_rbBFrame;
  328. int m_useReferenceFrameA;
  329. int m_angularOnly;
  330. int m_enableAngularMotor;
  331. float m_motorTargetVelocity;
  332. float m_maxMotorImpulse;
  333. float m_lowerLimit;
  334. float m_upperLimit;
  335. float m_limitSoftness;
  336. float m_biasFactor;
  337. float m_relaxationFactor;
  338. };
  339. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  340. struct btHingeConstraintDoubleData2
  341. {
  342. btTypedConstraintDoubleData m_typeConstraintData;
  343. btTransformDoubleData m_rbAFrame; // constraint axii. Assumes z is hinge axis.
  344. btTransformDoubleData m_rbBFrame;
  345. int m_useReferenceFrameA;
  346. int m_angularOnly;
  347. int m_enableAngularMotor;
  348. double m_motorTargetVelocity;
  349. double m_maxMotorImpulse;
  350. double m_lowerLimit;
  351. double m_upperLimit;
  352. double m_limitSoftness;
  353. double m_biasFactor;
  354. double m_relaxationFactor;
  355. char m_padding1[4];
  356. };
  357. SIMD_FORCE_INLINE int btHingeConstraint::calculateSerializeBufferSize() const
  358. {
  359. return sizeof(btHingeConstraintData);
  360. }
  361. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  362. SIMD_FORCE_INLINE const char* btHingeConstraint::serialize(void* dataBuffer, btSerializer* serializer) const
  363. {
  364. btHingeConstraintData* hingeData = (btHingeConstraintData*)dataBuffer;
  365. btTypedConstraint::serialize(&hingeData->m_typeConstraintData, serializer);
  366. m_rbAFrame.serialize(hingeData->m_rbAFrame);
  367. m_rbBFrame.serialize(hingeData->m_rbBFrame);
  368. hingeData->m_angularOnly = m_angularOnly;
  369. hingeData->m_enableAngularMotor = m_enableAngularMotor;
  370. hingeData->m_maxMotorImpulse = float(m_maxMotorImpulse);
  371. hingeData->m_motorTargetVelocity = float(m_motorTargetVelocity);
  372. hingeData->m_useReferenceFrameA = m_useReferenceFrameA;
  373. #ifdef _BT_USE_CENTER_LIMIT_
  374. hingeData->m_lowerLimit = float(m_limit.getLow());
  375. hingeData->m_upperLimit = float(m_limit.getHigh());
  376. hingeData->m_limitSoftness = float(m_limit.getSoftness());
  377. hingeData->m_biasFactor = float(m_limit.getBiasFactor());
  378. hingeData->m_relaxationFactor = float(m_limit.getRelaxationFactor());
  379. #else
  380. hingeData->m_lowerLimit = float(m_lowerLimit);
  381. hingeData->m_upperLimit = float(m_upperLimit);
  382. hingeData->m_limitSoftness = float(m_limitSoftness);
  383. hingeData->m_biasFactor = float(m_biasFactor);
  384. hingeData->m_relaxationFactor = float(m_relaxationFactor);
  385. #endif
  386. // Fill padding with zeros to appease msan.
  387. #ifdef BT_USE_DOUBLE_PRECISION
  388. hingeData->m_padding1[0] = 0;
  389. hingeData->m_padding1[1] = 0;
  390. hingeData->m_padding1[2] = 0;
  391. hingeData->m_padding1[3] = 0;
  392. #endif
  393. return btHingeConstraintDataName;
  394. }
  395. #endif //BT_HINGECONSTRAINT_H