btHingeConstraint.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 "bullet/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. };
  34. /// hinge constraint between two rigidbodies each with a pivotpoint that descibes the axis location in local space
  35. /// axis defines the orientation of the hinge axis
  36. ATTRIBUTE_ALIGNED16(class) btHingeConstraint : public btTypedConstraint
  37. {
  38. #ifdef IN_PARALLELL_SOLVER
  39. public:
  40. #endif
  41. btJacobianEntry m_jac[3]; //3 orthogonal linear constraints
  42. btJacobianEntry m_jacAng[3]; //2 orthogonal angular constraints+ 1 for limit/motor
  43. btTransform m_rbAFrame; // constraint axii. Assumes z is hinge axis.
  44. btTransform m_rbBFrame;
  45. btScalar m_motorTargetVelocity;
  46. btScalar m_maxMotorImpulse;
  47. #ifdef _BT_USE_CENTER_LIMIT_
  48. btAngularLimit m_limit;
  49. #else
  50. btScalar m_lowerLimit;
  51. btScalar m_upperLimit;
  52. btScalar m_limitSign;
  53. btScalar m_correction;
  54. btScalar m_limitSoftness;
  55. btScalar m_biasFactor;
  56. btScalar m_relaxationFactor;
  57. bool m_solveLimit;
  58. #endif
  59. btScalar m_kHinge;
  60. btScalar m_accLimitImpulse;
  61. btScalar m_hingeAngle;
  62. btScalar m_referenceSign;
  63. bool m_angularOnly;
  64. bool m_enableAngularMotor;
  65. bool m_useSolveConstraintObsolete;
  66. bool m_useOffsetForConstraintFrame;
  67. bool m_useReferenceFrameA;
  68. btScalar m_accMotorImpulse;
  69. int m_flags;
  70. btScalar m_normalCFM;
  71. btScalar m_stopCFM;
  72. btScalar m_stopERP;
  73. public:
  74. BT_DECLARE_ALIGNED_ALLOCATOR();
  75. btHingeConstraint(btRigidBody& rbA,btRigidBody& rbB, const btVector3& pivotInA,const btVector3& pivotInB, const btVector3& axisInA,const btVector3& axisInB, bool useReferenceFrameA = false);
  76. btHingeConstraint(btRigidBody& rbA,const btVector3& pivotInA,const btVector3& axisInA, bool useReferenceFrameA = false);
  77. btHingeConstraint(btRigidBody& rbA,btRigidBody& rbB, const btTransform& rbAFrame, const btTransform& rbBFrame, bool useReferenceFrameA = false);
  78. btHingeConstraint(btRigidBody& rbA,const btTransform& rbAFrame, bool useReferenceFrameA = false);
  79. virtual void buildJacobian();
  80. virtual void getInfo1 (btConstraintInfo1* info);
  81. void getInfo1NonVirtual(btConstraintInfo1* info);
  82. virtual void getInfo2 (btConstraintInfo2* info);
  83. void getInfo2NonVirtual(btConstraintInfo2* info,const btTransform& transA,const btTransform& transB,const btVector3& angVelA,const btVector3& angVelB);
  84. void getInfo2Internal(btConstraintInfo2* info,const btTransform& transA,const btTransform& transB,const btVector3& angVelA,const btVector3& angVelB);
  85. void getInfo2InternalUsingFrameOffset(btConstraintInfo2* info,const btTransform& transA,const btTransform& transB,const btVector3& angVelA,const btVector3& angVelB);
  86. void updateRHS(btScalar timeStep);
  87. const btRigidBody& getRigidBodyA() const
  88. {
  89. return m_rbA;
  90. }
  91. const btRigidBody& getRigidBodyB() const
  92. {
  93. return m_rbB;
  94. }
  95. btRigidBody& getRigidBodyA()
  96. {
  97. return m_rbA;
  98. }
  99. btRigidBody& getRigidBodyB()
  100. {
  101. return m_rbB;
  102. }
  103. btTransform& getFrameOffsetA()
  104. {
  105. return m_rbAFrame;
  106. }
  107. btTransform& getFrameOffsetB()
  108. {
  109. return m_rbBFrame;
  110. }
  111. void setFrames(const btTransform& frameA, const btTransform& frameB);
  112. void setAngularOnly(bool angularOnly)
  113. {
  114. m_angularOnly = angularOnly;
  115. }
  116. void enableAngularMotor(bool enableMotor,btScalar targetVelocity,btScalar maxMotorImpulse)
  117. {
  118. m_enableAngularMotor = enableMotor;
  119. m_motorTargetVelocity = targetVelocity;
  120. m_maxMotorImpulse = maxMotorImpulse;
  121. }
  122. // extra motor API, including ability to set a target rotation (as opposed to angular velocity)
  123. // note: setMotorTarget sets angular velocity under the hood, so you must call it every tick to
  124. // maintain a given angular target.
  125. void enableMotor(bool enableMotor) { m_enableAngularMotor = enableMotor; }
  126. void setMaxMotorImpulse(btScalar maxMotorImpulse) { m_maxMotorImpulse = maxMotorImpulse; }
  127. void setMotorTarget(const btQuaternion& qAinB, btScalar dt); // qAinB is rotation of body A wrt body B.
  128. void setMotorTarget(btScalar targetAngle, btScalar dt);
  129. void setLimit(btScalar low,btScalar high,btScalar _softness = 0.9f, btScalar _biasFactor = 0.3f, btScalar _relaxationFactor = 1.0f)
  130. {
  131. #ifdef _BT_USE_CENTER_LIMIT_
  132. m_limit.set(low, high, _softness, _biasFactor, _relaxationFactor);
  133. #else
  134. m_lowerLimit = btNormalizeAngle(low);
  135. m_upperLimit = btNormalizeAngle(high);
  136. m_limitSoftness = _softness;
  137. m_biasFactor = _biasFactor;
  138. m_relaxationFactor = _relaxationFactor;
  139. #endif
  140. }
  141. void setAxis(btVector3& axisInA)
  142. {
  143. btVector3 rbAxisA1, rbAxisA2;
  144. btPlaneSpace1(axisInA, rbAxisA1, rbAxisA2);
  145. btVector3 pivotInA = m_rbAFrame.getOrigin();
  146. // m_rbAFrame.getOrigin() = pivotInA;
  147. m_rbAFrame.getBasis().setValue( rbAxisA1.getX(),rbAxisA2.getX(),axisInA.getX(),
  148. rbAxisA1.getY(),rbAxisA2.getY(),axisInA.getY(),
  149. rbAxisA1.getZ(),rbAxisA2.getZ(),axisInA.getZ() );
  150. btVector3 axisInB = m_rbA.getCenterOfMassTransform().getBasis() * axisInA;
  151. btQuaternion rotationArc = shortestArcQuat(axisInA,axisInB);
  152. btVector3 rbAxisB1 = quatRotate(rotationArc,rbAxisA1);
  153. btVector3 rbAxisB2 = axisInB.cross(rbAxisB1);
  154. m_rbBFrame.getOrigin() = m_rbB.getCenterOfMassTransform().inverse()(m_rbA.getCenterOfMassTransform()(pivotInA));
  155. m_rbBFrame.getBasis().setValue( rbAxisB1.getX(),rbAxisB2.getX(),axisInB.getX(),
  156. rbAxisB1.getY(),rbAxisB2.getY(),axisInB.getY(),
  157. rbAxisB1.getZ(),rbAxisB2.getZ(),axisInB.getZ() );
  158. m_rbBFrame.getBasis() = m_rbB.getCenterOfMassTransform().getBasis().inverse() * m_rbBFrame.getBasis();
  159. }
  160. btScalar getLowerLimit() const
  161. {
  162. #ifdef _BT_USE_CENTER_LIMIT_
  163. return m_limit.getLow();
  164. #else
  165. return m_lowerLimit;
  166. #endif
  167. }
  168. btScalar getUpperLimit() const
  169. {
  170. #ifdef _BT_USE_CENTER_LIMIT_
  171. return m_limit.getHigh();
  172. #else
  173. return m_upperLimit;
  174. #endif
  175. }
  176. btScalar getHingeAngle();
  177. btScalar getHingeAngle(const btTransform& transA,const btTransform& transB);
  178. void testLimit(const btTransform& transA,const btTransform& transB);
  179. const btTransform& getAFrame() const { return m_rbAFrame; };
  180. const btTransform& getBFrame() const { return m_rbBFrame; };
  181. btTransform& getAFrame() { return m_rbAFrame; };
  182. btTransform& getBFrame() { return m_rbBFrame; };
  183. inline int getSolveLimit()
  184. {
  185. #ifdef _BT_USE_CENTER_LIMIT_
  186. return m_limit.isLimit();
  187. #else
  188. return m_solveLimit;
  189. #endif
  190. }
  191. inline btScalar getLimitSign()
  192. {
  193. #ifdef _BT_USE_CENTER_LIMIT_
  194. return m_limit.getSign();
  195. #else
  196. return m_limitSign;
  197. #endif
  198. }
  199. inline bool getAngularOnly()
  200. {
  201. return m_angularOnly;
  202. }
  203. inline bool getEnableAngularMotor()
  204. {
  205. return m_enableAngularMotor;
  206. }
  207. inline btScalar getMotorTargetVelosity()
  208. {
  209. return m_motorTargetVelocity;
  210. }
  211. inline btScalar getMaxMotorImpulse()
  212. {
  213. return m_maxMotorImpulse;
  214. }
  215. // access for UseFrameOffset
  216. bool getUseFrameOffset() { return m_useOffsetForConstraintFrame; }
  217. void setUseFrameOffset(bool frameOffsetOnOff) { m_useOffsetForConstraintFrame = frameOffsetOnOff; }
  218. ///override the default global value of a parameter (such as ERP or CFM), optionally provide the axis (0..5).
  219. ///If no axis is provided, it uses the default axis for this constraint.
  220. virtual void setParam(int num, btScalar value, int axis = -1);
  221. ///return the local value of parameter
  222. virtual btScalar getParam(int num, int axis = -1) const;
  223. virtual int calculateSerializeBufferSize() const;
  224. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  225. virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
  226. };
  227. //only for backward compatibility
  228. #ifdef BT_BACKWARDS_COMPATIBLE_SERIALIZATION
  229. ///this structure is not used, except for loading pre-2.82 .bullet files
  230. struct btHingeConstraintDoubleData
  231. {
  232. btTypedConstraintData m_typeConstraintData;
  233. btTransformDoubleData m_rbAFrame; // constraint axii. Assumes z is hinge axis.
  234. btTransformDoubleData m_rbBFrame;
  235. int m_useReferenceFrameA;
  236. int m_angularOnly;
  237. int m_enableAngularMotor;
  238. float m_motorTargetVelocity;
  239. float m_maxMotorImpulse;
  240. float m_lowerLimit;
  241. float m_upperLimit;
  242. float m_limitSoftness;
  243. float m_biasFactor;
  244. float m_relaxationFactor;
  245. };
  246. #endif //BT_BACKWARDS_COMPATIBLE_SERIALIZATION
  247. struct btHingeConstraintFloatData
  248. {
  249. btTypedConstraintData m_typeConstraintData;
  250. btTransformFloatData m_rbAFrame; // constraint axii. Assumes z is hinge axis.
  251. btTransformFloatData m_rbBFrame;
  252. int m_useReferenceFrameA;
  253. int m_angularOnly;
  254. int m_enableAngularMotor;
  255. float m_motorTargetVelocity;
  256. float m_maxMotorImpulse;
  257. float m_lowerLimit;
  258. float m_upperLimit;
  259. float m_limitSoftness;
  260. float m_biasFactor;
  261. float m_relaxationFactor;
  262. };
  263. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  264. struct btHingeConstraintDoubleData2
  265. {
  266. btTypedConstraintDoubleData m_typeConstraintData;
  267. btTransformDoubleData m_rbAFrame; // constraint axii. Assumes z is hinge axis.
  268. btTransformDoubleData m_rbBFrame;
  269. int m_useReferenceFrameA;
  270. int m_angularOnly;
  271. int m_enableAngularMotor;
  272. double m_motorTargetVelocity;
  273. double m_maxMotorImpulse;
  274. double m_lowerLimit;
  275. double m_upperLimit;
  276. double m_limitSoftness;
  277. double m_biasFactor;
  278. double m_relaxationFactor;
  279. char m_padding1[4];
  280. };
  281. SIMD_FORCE_INLINE int btHingeConstraint::calculateSerializeBufferSize() const
  282. {
  283. return sizeof(btHingeConstraintData);
  284. }
  285. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  286. SIMD_FORCE_INLINE const char* btHingeConstraint::serialize(void* dataBuffer, btSerializer* serializer) const
  287. {
  288. btHingeConstraintData* hingeData = (btHingeConstraintData*)dataBuffer;
  289. btTypedConstraint::serialize(&hingeData->m_typeConstraintData,serializer);
  290. m_rbAFrame.serialize(hingeData->m_rbAFrame);
  291. m_rbBFrame.serialize(hingeData->m_rbBFrame);
  292. hingeData->m_angularOnly = m_angularOnly;
  293. hingeData->m_enableAngularMotor = m_enableAngularMotor;
  294. hingeData->m_maxMotorImpulse = float(m_maxMotorImpulse);
  295. hingeData->m_motorTargetVelocity = float(m_motorTargetVelocity);
  296. hingeData->m_useReferenceFrameA = m_useReferenceFrameA;
  297. #ifdef _BT_USE_CENTER_LIMIT_
  298. hingeData->m_lowerLimit = float(m_limit.getLow());
  299. hingeData->m_upperLimit = float(m_limit.getHigh());
  300. hingeData->m_limitSoftness = float(m_limit.getSoftness());
  301. hingeData->m_biasFactor = float(m_limit.getBiasFactor());
  302. hingeData->m_relaxationFactor = float(m_limit.getRelaxationFactor());
  303. #else
  304. hingeData->m_lowerLimit = float(m_lowerLimit);
  305. hingeData->m_upperLimit = float(m_upperLimit);
  306. hingeData->m_limitSoftness = float(m_limitSoftness);
  307. hingeData->m_biasFactor = float(m_biasFactor);
  308. hingeData->m_relaxationFactor = float(m_relaxationFactor);
  309. #endif
  310. return btHingeConstraintDataName;
  311. }
  312. #endif //BT_HINGECONSTRAINT_H