b3TypedConstraint.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 B3_TYPED_CONSTRAINT_H
  14. #define B3_TYPED_CONSTRAINT_H
  15. #include "Bullet3Common/b3Scalar.h"
  16. #include "b3SolverConstraint.h"
  17. class b3Serializer;
  18. //Don't change any of the existing enum values, so add enum types at the end for serialization compatibility
  19. enum b3TypedConstraintType
  20. {
  21. B3_POINT2POINT_CONSTRAINT_TYPE=3,
  22. B3_HINGE_CONSTRAINT_TYPE,
  23. B3_CONETWIST_CONSTRAINT_TYPE,
  24. B3_D6_CONSTRAINT_TYPE,
  25. B3_SLIDER_CONSTRAINT_TYPE,
  26. B3_CONTACT_CONSTRAINT_TYPE,
  27. B3_D6_SPRING_CONSTRAINT_TYPE,
  28. B3_GEAR_CONSTRAINT_TYPE,
  29. B3_FIXED_CONSTRAINT_TYPE,
  30. B3_MAX_CONSTRAINT_TYPE
  31. };
  32. enum b3ConstraintParams
  33. {
  34. B3_CONSTRAINT_ERP=1,
  35. B3_CONSTRAINT_STOP_ERP,
  36. B3_CONSTRAINT_CFM,
  37. B3_CONSTRAINT_STOP_CFM
  38. };
  39. #if 1
  40. #define b3AssertConstrParams(_par) b3Assert(_par)
  41. #else
  42. #define b3AssertConstrParams(_par)
  43. #endif
  44. B3_ATTRIBUTE_ALIGNED16(struct) b3JointFeedback
  45. {
  46. b3Vector3 m_appliedForceBodyA;
  47. b3Vector3 m_appliedTorqueBodyA;
  48. b3Vector3 m_appliedForceBodyB;
  49. b3Vector3 m_appliedTorqueBodyB;
  50. };
  51. struct b3RigidBodyData;
  52. ///TypedConstraint is the baseclass for Bullet constraints and vehicles
  53. B3_ATTRIBUTE_ALIGNED16(class) b3TypedConstraint : public b3TypedObject
  54. {
  55. int m_userConstraintType;
  56. union
  57. {
  58. int m_userConstraintId;
  59. void* m_userConstraintPtr;
  60. };
  61. b3Scalar m_breakingImpulseThreshold;
  62. bool m_isEnabled;
  63. bool m_needsFeedback;
  64. int m_overrideNumSolverIterations;
  65. b3TypedConstraint& operator=(b3TypedConstraint& other)
  66. {
  67. b3Assert(0);
  68. (void) other;
  69. return *this;
  70. }
  71. protected:
  72. int m_rbA;
  73. int m_rbB;
  74. b3Scalar m_appliedImpulse;
  75. b3Scalar m_dbgDrawSize;
  76. b3JointFeedback* m_jointFeedback;
  77. ///internal method used by the constraint solver, don't use them directly
  78. b3Scalar getMotorFactor(b3Scalar pos, b3Scalar lowLim, b3Scalar uppLim, b3Scalar vel, b3Scalar timeFact);
  79. public:
  80. B3_DECLARE_ALIGNED_ALLOCATOR();
  81. virtual ~b3TypedConstraint() {};
  82. b3TypedConstraint(b3TypedConstraintType type, int bodyA,int bodyB);
  83. struct b3ConstraintInfo1 {
  84. int m_numConstraintRows,nub;
  85. };
  86. struct b3ConstraintInfo2 {
  87. // integrator parameters: frames per second (1/stepsize), default error
  88. // reduction parameter (0..1).
  89. b3Scalar fps,erp;
  90. // for the first and second body, pointers to two (linear and angular)
  91. // n*3 jacobian sub matrices, stored by rows. these matrices will have
  92. // been initialized to 0 on entry. if the second body is zero then the
  93. // J2xx pointers may be 0.
  94. b3Scalar *m_J1linearAxis,*m_J1angularAxis,*m_J2linearAxis,*m_J2angularAxis;
  95. // elements to jump from one row to the next in J's
  96. int rowskip;
  97. // right hand sides of the equation J*v = c + cfm * lambda. cfm is the
  98. // "constraint force mixing" vector. c is set to zero on entry, cfm is
  99. // set to a constant value (typically very small or zero) value on entry.
  100. b3Scalar *m_constraintError,*cfm;
  101. // lo and hi limits for variables (set to -/+ infinity on entry).
  102. b3Scalar *m_lowerLimit,*m_upperLimit;
  103. // findex vector for variables. see the LCP solver interface for a
  104. // description of what this does. this is set to -1 on entry.
  105. // note that the returned indexes are relative to the first index of
  106. // the constraint.
  107. int *findex;
  108. // number of solver iterations
  109. int m_numIterations;
  110. //damping of the velocity
  111. b3Scalar m_damping;
  112. };
  113. int getOverrideNumSolverIterations() const
  114. {
  115. return m_overrideNumSolverIterations;
  116. }
  117. ///override the number of constraint solver iterations used to solve this constraint
  118. ///-1 will use the default number of iterations, as specified in SolverInfo.m_numIterations
  119. void setOverrideNumSolverIterations(int overideNumIterations)
  120. {
  121. m_overrideNumSolverIterations = overideNumIterations;
  122. }
  123. ///internal method used by the constraint solver, don't use them directly
  124. virtual void setupSolverConstraint(b3ConstraintArray& ca, int solverBodyA,int solverBodyB, b3Scalar timeStep)
  125. {
  126. (void)ca;
  127. (void)solverBodyA;
  128. (void)solverBodyB;
  129. (void)timeStep;
  130. }
  131. ///internal method used by the constraint solver, don't use them directly
  132. virtual void getInfo1 (b3ConstraintInfo1* info,const b3RigidBodyData* bodies)=0;
  133. ///internal method used by the constraint solver, don't use them directly
  134. virtual void getInfo2 (b3ConstraintInfo2* info, const b3RigidBodyData* bodies)=0;
  135. ///internal method used by the constraint solver, don't use them directly
  136. void internalSetAppliedImpulse(b3Scalar appliedImpulse)
  137. {
  138. m_appliedImpulse = appliedImpulse;
  139. }
  140. ///internal method used by the constraint solver, don't use them directly
  141. b3Scalar internalGetAppliedImpulse()
  142. {
  143. return m_appliedImpulse;
  144. }
  145. b3Scalar getBreakingImpulseThreshold() const
  146. {
  147. return m_breakingImpulseThreshold;
  148. }
  149. void setBreakingImpulseThreshold(b3Scalar threshold)
  150. {
  151. m_breakingImpulseThreshold = threshold;
  152. }
  153. bool isEnabled() const
  154. {
  155. return m_isEnabled;
  156. }
  157. void setEnabled(bool enabled)
  158. {
  159. m_isEnabled=enabled;
  160. }
  161. ///internal method used by the constraint solver, don't use them directly
  162. virtual void solveConstraintObsolete(b3SolverBody& /*bodyA*/,b3SolverBody& /*bodyB*/,b3Scalar /*timeStep*/) {};
  163. int getRigidBodyA() const
  164. {
  165. return m_rbA;
  166. }
  167. int getRigidBodyB() const
  168. {
  169. return m_rbB;
  170. }
  171. int getRigidBodyA()
  172. {
  173. return m_rbA;
  174. }
  175. int getRigidBodyB()
  176. {
  177. return m_rbB;
  178. }
  179. int getUserConstraintType() const
  180. {
  181. return m_userConstraintType ;
  182. }
  183. void setUserConstraintType(int userConstraintType)
  184. {
  185. m_userConstraintType = userConstraintType;
  186. };
  187. void setUserConstraintId(int uid)
  188. {
  189. m_userConstraintId = uid;
  190. }
  191. int getUserConstraintId() const
  192. {
  193. return m_userConstraintId;
  194. }
  195. void setUserConstraintPtr(void* ptr)
  196. {
  197. m_userConstraintPtr = ptr;
  198. }
  199. void* getUserConstraintPtr()
  200. {
  201. return m_userConstraintPtr;
  202. }
  203. void setJointFeedback(b3JointFeedback* jointFeedback)
  204. {
  205. m_jointFeedback = jointFeedback;
  206. }
  207. const b3JointFeedback* getJointFeedback() const
  208. {
  209. return m_jointFeedback;
  210. }
  211. b3JointFeedback* getJointFeedback()
  212. {
  213. return m_jointFeedback;
  214. }
  215. int getUid() const
  216. {
  217. return m_userConstraintId;
  218. }
  219. bool needsFeedback() const
  220. {
  221. return m_needsFeedback;
  222. }
  223. ///enableFeedback will allow to read the applied linear and angular impulse
  224. ///use getAppliedImpulse, getAppliedLinearImpulse and getAppliedAngularImpulse to read feedback information
  225. void enableFeedback(bool needsFeedback)
  226. {
  227. m_needsFeedback = needsFeedback;
  228. }
  229. ///getAppliedImpulse is an estimated total applied impulse.
  230. ///This feedback could be used to determine breaking constraints or playing sounds.
  231. b3Scalar getAppliedImpulse() const
  232. {
  233. b3Assert(m_needsFeedback);
  234. return m_appliedImpulse;
  235. }
  236. b3TypedConstraintType getConstraintType () const
  237. {
  238. return b3TypedConstraintType(m_objectType);
  239. }
  240. void setDbgDrawSize(b3Scalar dbgDrawSize)
  241. {
  242. m_dbgDrawSize = dbgDrawSize;
  243. }
  244. b3Scalar getDbgDrawSize()
  245. {
  246. return m_dbgDrawSize;
  247. }
  248. ///override the default global value of a parameter (such as ERP or CFM), optionally provide the axis (0..5).
  249. ///If no axis is provided, it uses the default axis for this constraint.
  250. virtual void setParam(int num, b3Scalar value, int axis = -1) = 0;
  251. ///return the local value of parameter
  252. virtual b3Scalar getParam(int num, int axis = -1) const = 0;
  253. // virtual int calculateSerializeBufferSize() const;
  254. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  255. //virtual const char* serialize(void* dataBuffer, b3Serializer* serializer) const;
  256. };
  257. // returns angle in range [-B3_2_PI, B3_2_PI], closest to one of the limits
  258. // all arguments should be normalized angles (i.e. in range [-B3_PI, B3_PI])
  259. B3_FORCE_INLINE b3Scalar b3AdjustAngleToLimits(b3Scalar angleInRadians, b3Scalar angleLowerLimitInRadians, b3Scalar angleUpperLimitInRadians)
  260. {
  261. if(angleLowerLimitInRadians >= angleUpperLimitInRadians)
  262. {
  263. return angleInRadians;
  264. }
  265. else if(angleInRadians < angleLowerLimitInRadians)
  266. {
  267. b3Scalar diffLo = b3Fabs(b3NormalizeAngle(angleLowerLimitInRadians - angleInRadians));
  268. b3Scalar diffHi = b3Fabs(b3NormalizeAngle(angleUpperLimitInRadians - angleInRadians));
  269. return (diffLo < diffHi) ? angleInRadians : (angleInRadians + B3_2_PI);
  270. }
  271. else if(angleInRadians > angleUpperLimitInRadians)
  272. {
  273. b3Scalar diffHi = b3Fabs(b3NormalizeAngle(angleInRadians - angleUpperLimitInRadians));
  274. b3Scalar diffLo = b3Fabs(b3NormalizeAngle(angleInRadians - angleLowerLimitInRadians));
  275. return (diffLo < diffHi) ? (angleInRadians - B3_2_PI) : angleInRadians;
  276. }
  277. else
  278. {
  279. return angleInRadians;
  280. }
  281. }
  282. ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
  283. struct b3TypedConstraintData
  284. {
  285. int m_bodyA;
  286. int m_bodyB;
  287. char *m_name;
  288. int m_objectType;
  289. int m_userConstraintType;
  290. int m_userConstraintId;
  291. int m_needsFeedback;
  292. float m_appliedImpulse;
  293. float m_dbgDrawSize;
  294. int m_disableCollisionsBetweenLinkedBodies;
  295. int m_overrideNumSolverIterations;
  296. float m_breakingImpulseThreshold;
  297. int m_isEnabled;
  298. };
  299. /*B3_FORCE_INLINE int b3TypedConstraint::calculateSerializeBufferSize() const
  300. {
  301. return sizeof(b3TypedConstraintData);
  302. }
  303. */
  304. class b3AngularLimit
  305. {
  306. private:
  307. b3Scalar
  308. m_center,
  309. m_halfRange,
  310. m_softness,
  311. m_biasFactor,
  312. m_relaxationFactor,
  313. m_correction,
  314. m_sign;
  315. bool
  316. m_solveLimit;
  317. public:
  318. /// Default constructor initializes limit as inactive, allowing free constraint movement
  319. b3AngularLimit()
  320. :m_center(0.0f),
  321. m_halfRange(-1.0f),
  322. m_softness(0.9f),
  323. m_biasFactor(0.3f),
  324. m_relaxationFactor(1.0f),
  325. m_correction(0.0f),
  326. m_sign(0.0f),
  327. m_solveLimit(false)
  328. {}
  329. /// Sets all limit's parameters.
  330. /// When low > high limit becomes inactive.
  331. /// When high - low > 2PI limit is ineffective too becouse no angle can exceed the limit
  332. void set(b3Scalar low, b3Scalar high, b3Scalar _softness = 0.9f, b3Scalar _biasFactor = 0.3f, b3Scalar _relaxationFactor = 1.0f);
  333. /// Checks conastaint angle against limit. If limit is active and the angle violates the limit
  334. /// correction is calculated.
  335. void test(const b3Scalar angle);
  336. /// Returns limit's softness
  337. inline b3Scalar getSoftness() const
  338. {
  339. return m_softness;
  340. }
  341. /// Returns limit's bias factor
  342. inline b3Scalar getBiasFactor() const
  343. {
  344. return m_biasFactor;
  345. }
  346. /// Returns limit's relaxation factor
  347. inline b3Scalar getRelaxationFactor() const
  348. {
  349. return m_relaxationFactor;
  350. }
  351. /// Returns correction value evaluated when test() was invoked
  352. inline b3Scalar getCorrection() const
  353. {
  354. return m_correction;
  355. }
  356. /// Returns sign value evaluated when test() was invoked
  357. inline b3Scalar getSign() const
  358. {
  359. return m_sign;
  360. }
  361. /// Gives half of the distance between min and max limit angle
  362. inline b3Scalar getHalfRange() const
  363. {
  364. return m_halfRange;
  365. }
  366. /// Returns true when the last test() invocation recognized limit violation
  367. inline bool isLimit() const
  368. {
  369. return m_solveLimit;
  370. }
  371. /// Checks given angle against limit. If limit is active and angle doesn't fit it, the angle
  372. /// returned is modified so it equals to the limit closest to given angle.
  373. void fit(b3Scalar& angle) const;
  374. /// Returns correction value multiplied by sign value
  375. b3Scalar getError() const;
  376. b3Scalar getLow() const;
  377. b3Scalar getHigh() const;
  378. };
  379. #endif //B3_TYPED_CONSTRAINT_H