btMultiBodyLink.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2013 Erwin Coumans http://bulletphysics.org
  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_MULTIBODY_LINK_H
  14. #define BT_MULTIBODY_LINK_H
  15. #include "LinearMath/btQuaternion.h"
  16. #include "LinearMath/btVector3.h"
  17. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  18. enum btMultiBodyLinkFlags
  19. {
  20. BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION = 1
  21. };
  22. #define BT_MULTIBODYLINK_INCLUDE_PLANAR_JOINTS
  23. #define TEST_SPATIAL_ALGEBRA_LAYER
  24. //
  25. // Various spatial helper functions
  26. //
  27. //namespace {
  28. #ifdef TEST_SPATIAL_ALGEBRA_LAYER
  29. #include "LinearMath/btSpatialAlgebra.h"
  30. #endif
  31. //}
  32. //
  33. // Link struct
  34. //
  35. struct btMultibodyLink
  36. {
  37. BT_DECLARE_ALIGNED_ALLOCATOR();
  38. btScalar m_mass; // mass of link
  39. btVector3 m_inertiaLocal; // inertia of link (local frame; diagonal)
  40. int m_parent; // index of the parent link (assumed to be < index of this link), or -1 if parent is the base link.
  41. btQuaternion m_zeroRotParentToThis; // rotates vectors in parent-frame to vectors in local-frame (when q=0). constant.
  42. btVector3 m_dVector; // vector from the inboard joint pos to this link's COM. (local frame.) constant. set for revolute joints only.
  43. // m_eVector is constant, but depends on the joint type
  44. // prismatic: vector from COM of parent to COM of this link, WHEN Q = 0. (local frame.)
  45. // revolute: vector from parent's COM to the pivot point, in PARENT's frame.
  46. btVector3 m_eVector;
  47. #ifdef TEST_SPATIAL_ALGEBRA_LAYER
  48. btSpatialMotionVector m_absFrameTotVelocity, m_absFrameLocVelocity;
  49. #endif
  50. enum eFeatherstoneJointType
  51. {
  52. eRevolute = 0,
  53. ePrismatic = 1,
  54. eSpherical = 2,
  55. #ifdef BT_MULTIBODYLINK_INCLUDE_PLANAR_JOINTS
  56. ePlanar = 3,
  57. #endif
  58. eFixed = 4,
  59. eInvalid
  60. };
  61. // "axis" = spatial joint axis (Mirtich Defn 9 p104). (expressed in local frame.) constant.
  62. // for prismatic: m_axesTop[0] = zero;
  63. // m_axesBottom[0] = unit vector along the joint axis.
  64. // for revolute: m_axesTop[0] = unit vector along the rotation axis (u);
  65. // m_axesBottom[0] = u cross m_dVector (i.e. COM linear motion due to the rotation at the joint)
  66. //
  67. // for spherical: m_axesTop[0][1][2] (u1,u2,u3) form a 3x3 identity matrix (3 rotation axes)
  68. // m_axesBottom[0][1][2] cross u1,u2,u3 (i.e. COM linear motion due to the rotation at the joint)
  69. //
  70. // for planar: m_axesTop[0] = unit vector along the rotation axis (u); defines the plane of motion
  71. // m_axesTop[1][2] = zero
  72. // m_axesBottom[0] = zero
  73. // m_axesBottom[1][2] = unit vectors along the translational axes on that plane
  74. #ifndef TEST_SPATIAL_ALGEBRA_LAYER
  75. btVector3 m_axesTop[6];
  76. btVector3 m_axesBottom[6];
  77. void setAxisTop(int dof, const btVector3 &axis) { m_axesTop[dof] = axis; }
  78. void setAxisBottom(int dof, const btVector3 &axis) { m_axesBottom[dof] = axis; }
  79. void setAxisTop(int dof, const btScalar &x, const btScalar &y, const btScalar &z) { m_axesTop[dof].setValue(x, y, z); }
  80. void setAxisBottom(int dof, const btScalar &x, const btScalar &y, const btScalar &z) { m_axesBottom[dof].setValue(x, y, z); }
  81. const btVector3 & getAxisTop(int dof) const { return m_axesTop[dof]; }
  82. const btVector3 & getAxisBottom(int dof) const { return m_axesBottom[dof]; }
  83. #else
  84. btSpatialMotionVector m_axes[6];
  85. void setAxisTop(int dof, const btVector3 &axis) { m_axes[dof].m_topVec = axis; }
  86. void setAxisBottom(int dof, const btVector3 &axis) { m_axes[dof].m_bottomVec = axis; }
  87. void setAxisTop(int dof, const btScalar &x, const btScalar &y, const btScalar &z) { m_axes[dof].m_topVec.setValue(x, y, z); }
  88. void setAxisBottom(int dof, const btScalar &x, const btScalar &y, const btScalar &z) { m_axes[dof].m_bottomVec.setValue(x, y, z); }
  89. const btVector3 & getAxisTop(int dof) const { return m_axes[dof].m_topVec; }
  90. const btVector3 & getAxisBottom(int dof) const { return m_axes[dof].m_bottomVec; }
  91. #endif
  92. int m_dofOffset, m_cfgOffset;
  93. btQuaternion m_cachedRotParentToThis; // rotates vectors in parent frame to vectors in local frame
  94. btVector3 m_cachedRVector; // vector from COM of parent to COM of this link, in local frame.
  95. btVector3 m_appliedForce; // In WORLD frame
  96. btVector3 m_appliedTorque; // In WORLD frame
  97. btScalar m_jointPos[7];
  98. btScalar m_jointTorque[6]; //TODO
  99. class btMultiBodyLinkCollider* m_collider;
  100. int m_flags;
  101. int m_dofCount, m_posVarCount; //redundant but handy
  102. eFeatherstoneJointType m_jointType;
  103. // ctor: set some sensible defaults
  104. btMultibodyLink()
  105. : m_mass(1),
  106. m_parent(-1),
  107. m_zeroRotParentToThis(0, 0, 0, 1),
  108. m_cachedRotParentToThis(0, 0, 0, 1),
  109. m_collider(0),
  110. m_flags(0),
  111. m_dofCount(0),
  112. m_posVarCount(0),
  113. m_jointType(btMultibodyLink::eInvalid)
  114. {
  115. m_inertiaLocal.setValue(1, 1, 1);
  116. setAxisTop(0, 0., 0., 0.);
  117. setAxisBottom(0, 1., 0., 0.);
  118. m_dVector.setValue(0, 0, 0);
  119. m_eVector.setValue(0, 0, 0);
  120. m_cachedRVector.setValue(0, 0, 0);
  121. m_appliedForce.setValue( 0, 0, 0);
  122. m_appliedTorque.setValue(0, 0, 0);
  123. //
  124. m_jointPos[0] = m_jointPos[1] = m_jointPos[2] = m_jointPos[4] = m_jointPos[5] = m_jointPos[6] = 0.f;
  125. m_jointPos[3] = 1.f; //"quat.w"
  126. m_jointTorque[0] = m_jointTorque[1] = m_jointTorque[2] = m_jointTorque[3] = m_jointTorque[4] = m_jointTorque[5] = 0.f;
  127. }
  128. // routine to update m_cachedRotParentToThis and m_cachedRVector
  129. void updateCache()
  130. {
  131. //multidof
  132. if (m_jointType == eRevolute)
  133. {
  134. m_cachedRotParentToThis = btQuaternion(getAxisTop(0),-m_jointPos[0]) * m_zeroRotParentToThis;
  135. m_cachedRVector = m_dVector + quatRotate(m_cachedRotParentToThis,m_eVector);
  136. } else
  137. {
  138. // m_cachedRotParentToThis never changes, so no need to update
  139. m_cachedRVector = m_eVector + m_jointPos[0] * getAxisBottom(0);
  140. }
  141. }
  142. void updateCacheMultiDof(btScalar *pq = 0)
  143. {
  144. btScalar *pJointPos = (pq ? pq : &m_jointPos[0]);
  145. switch(m_jointType)
  146. {
  147. case eRevolute:
  148. {
  149. m_cachedRotParentToThis = btQuaternion(getAxisTop(0),-pJointPos[0]) * m_zeroRotParentToThis;
  150. m_cachedRVector = m_dVector + quatRotate(m_cachedRotParentToThis,m_eVector);
  151. break;
  152. }
  153. case ePrismatic:
  154. {
  155. // m_cachedRotParentToThis never changes, so no need to update
  156. m_cachedRVector = m_dVector + quatRotate(m_cachedRotParentToThis,m_eVector) + pJointPos[0] * getAxisBottom(0);
  157. break;
  158. }
  159. case eSpherical:
  160. {
  161. m_cachedRotParentToThis = btQuaternion(pJointPos[0], pJointPos[1], pJointPos[2], -pJointPos[3]) * m_zeroRotParentToThis;
  162. m_cachedRVector = m_dVector + quatRotate(m_cachedRotParentToThis,m_eVector);
  163. break;
  164. }
  165. #ifdef BT_MULTIBODYLINK_INCLUDE_PLANAR_JOINTS
  166. case ePlanar:
  167. {
  168. m_cachedRotParentToThis = btQuaternion(getAxisTop(0),-pJointPos[0]) * m_zeroRotParentToThis;
  169. m_cachedRVector = quatRotate(btQuaternion(getAxisTop(0),-pJointPos[0]), pJointPos[1] * getAxisBottom(1) + pJointPos[2] * getAxisBottom(2)) + quatRotate(m_cachedRotParentToThis,m_eVector);
  170. break;
  171. }
  172. #endif
  173. case eFixed:
  174. {
  175. m_cachedRotParentToThis = m_zeroRotParentToThis;
  176. m_cachedRVector = m_dVector + quatRotate(m_cachedRotParentToThis,m_eVector);
  177. break;
  178. }
  179. default:
  180. {
  181. //invalid type
  182. btAssert(0);
  183. }
  184. }
  185. }
  186. };
  187. #endif //BT_MULTIBODY_LINK_H