btSolverBody.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #ifndef BT_SOLVER_BODY_H
  14. #define BT_SOLVER_BODY_H
  15. class btRigidBody;
  16. #include "bullet/LinearMath/btVector3.h"
  17. #include "bullet/LinearMath/btMatrix3x3.h"
  18. #include "bullet/LinearMath/btAlignedAllocator.h"
  19. #include "bullet/LinearMath/btTransformUtil.h"
  20. ///Until we get other contributions, only use SIMD on Windows, when using Visual Studio 2008 or later, and not double precision
  21. #ifdef BT_USE_SSE
  22. #define USE_SIMD 1
  23. #endif //
  24. #ifdef USE_SIMD
  25. struct btSimdScalar
  26. {
  27. SIMD_FORCE_INLINE btSimdScalar()
  28. {
  29. }
  30. SIMD_FORCE_INLINE btSimdScalar(float fl)
  31. :m_vec128 (_mm_set1_ps(fl))
  32. {
  33. }
  34. SIMD_FORCE_INLINE btSimdScalar(__m128 v128)
  35. :m_vec128(v128)
  36. {
  37. }
  38. union
  39. {
  40. __m128 m_vec128;
  41. float m_floats[4];
  42. int m_ints[4];
  43. btScalar m_unusedPadding;
  44. };
  45. SIMD_FORCE_INLINE __m128 get128()
  46. {
  47. return m_vec128;
  48. }
  49. SIMD_FORCE_INLINE const __m128 get128() const
  50. {
  51. return m_vec128;
  52. }
  53. SIMD_FORCE_INLINE void set128(__m128 v128)
  54. {
  55. m_vec128 = v128;
  56. }
  57. SIMD_FORCE_INLINE operator __m128()
  58. {
  59. return m_vec128;
  60. }
  61. SIMD_FORCE_INLINE operator const __m128() const
  62. {
  63. return m_vec128;
  64. }
  65. SIMD_FORCE_INLINE operator float() const
  66. {
  67. return m_floats[0];
  68. }
  69. };
  70. ///@brief Return the elementwise product of two btSimdScalar
  71. SIMD_FORCE_INLINE btSimdScalar
  72. operator*(const btSimdScalar& v1, const btSimdScalar& v2)
  73. {
  74. return btSimdScalar(_mm_mul_ps(v1.get128(),v2.get128()));
  75. }
  76. ///@brief Return the elementwise product of two btSimdScalar
  77. SIMD_FORCE_INLINE btSimdScalar
  78. operator+(const btSimdScalar& v1, const btSimdScalar& v2)
  79. {
  80. return btSimdScalar(_mm_add_ps(v1.get128(),v2.get128()));
  81. }
  82. #else
  83. #define btSimdScalar btScalar
  84. #endif
  85. ///The btSolverBody is an internal datastructure for the constraint solver. Only necessary data is packed to increase cache coherence/performance.
  86. ATTRIBUTE_ALIGNED16 (struct) btSolverBody
  87. {
  88. BT_DECLARE_ALIGNED_ALLOCATOR();
  89. btTransform m_worldTransform;
  90. btVector3 m_deltaLinearVelocity;
  91. btVector3 m_deltaAngularVelocity;
  92. btVector3 m_angularFactor;
  93. btVector3 m_linearFactor;
  94. btVector3 m_invMass;
  95. btVector3 m_pushVelocity;
  96. btVector3 m_turnVelocity;
  97. btVector3 m_linearVelocity;
  98. btVector3 m_angularVelocity;
  99. btVector3 m_externalForceImpulse;
  100. btVector3 m_externalTorqueImpulse;
  101. btRigidBody* m_originalBody;
  102. void setWorldTransform(const btTransform& worldTransform)
  103. {
  104. m_worldTransform = worldTransform;
  105. }
  106. const btTransform& getWorldTransform() const
  107. {
  108. return m_worldTransform;
  109. }
  110. SIMD_FORCE_INLINE void getVelocityInLocalPointNoDelta(const btVector3& rel_pos, btVector3& velocity ) const
  111. {
  112. if (m_originalBody)
  113. velocity = m_linearVelocity + m_externalForceImpulse + (m_angularVelocity+m_externalTorqueImpulse).cross(rel_pos);
  114. else
  115. velocity.setValue(0,0,0);
  116. }
  117. SIMD_FORCE_INLINE void getVelocityInLocalPointObsolete(const btVector3& rel_pos, btVector3& velocity ) const
  118. {
  119. if (m_originalBody)
  120. velocity = m_linearVelocity+m_deltaLinearVelocity + (m_angularVelocity+m_deltaAngularVelocity).cross(rel_pos);
  121. else
  122. velocity.setValue(0,0,0);
  123. }
  124. SIMD_FORCE_INLINE void getAngularVelocity(btVector3& angVel) const
  125. {
  126. if (m_originalBody)
  127. angVel =m_angularVelocity+m_deltaAngularVelocity;
  128. else
  129. angVel.setValue(0,0,0);
  130. }
  131. //Optimization for the iterative solver: avoid calculating constant terms involving inertia, normal, relative position
  132. SIMD_FORCE_INLINE void applyImpulse(const btVector3& linearComponent, const btVector3& angularComponent,const btScalar impulseMagnitude)
  133. {
  134. if (m_originalBody)
  135. {
  136. m_deltaLinearVelocity += linearComponent*impulseMagnitude*m_linearFactor;
  137. m_deltaAngularVelocity += angularComponent*(impulseMagnitude*m_angularFactor);
  138. }
  139. }
  140. SIMD_FORCE_INLINE void internalApplyPushImpulse(const btVector3& linearComponent, const btVector3& angularComponent,btScalar impulseMagnitude)
  141. {
  142. if (m_originalBody)
  143. {
  144. m_pushVelocity += linearComponent*impulseMagnitude*m_linearFactor;
  145. m_turnVelocity += angularComponent*(impulseMagnitude*m_angularFactor);
  146. }
  147. }
  148. const btVector3& getDeltaLinearVelocity() const
  149. {
  150. return m_deltaLinearVelocity;
  151. }
  152. const btVector3& getDeltaAngularVelocity() const
  153. {
  154. return m_deltaAngularVelocity;
  155. }
  156. const btVector3& getPushVelocity() const
  157. {
  158. return m_pushVelocity;
  159. }
  160. const btVector3& getTurnVelocity() const
  161. {
  162. return m_turnVelocity;
  163. }
  164. ////////////////////////////////////////////////
  165. ///some internal methods, don't use them
  166. btVector3& internalGetDeltaLinearVelocity()
  167. {
  168. return m_deltaLinearVelocity;
  169. }
  170. btVector3& internalGetDeltaAngularVelocity()
  171. {
  172. return m_deltaAngularVelocity;
  173. }
  174. const btVector3& internalGetAngularFactor() const
  175. {
  176. return m_angularFactor;
  177. }
  178. const btVector3& internalGetInvMass() const
  179. {
  180. return m_invMass;
  181. }
  182. void internalSetInvMass(const btVector3& invMass)
  183. {
  184. m_invMass = invMass;
  185. }
  186. btVector3& internalGetPushVelocity()
  187. {
  188. return m_pushVelocity;
  189. }
  190. btVector3& internalGetTurnVelocity()
  191. {
  192. return m_turnVelocity;
  193. }
  194. SIMD_FORCE_INLINE void internalGetVelocityInLocalPointObsolete(const btVector3& rel_pos, btVector3& velocity ) const
  195. {
  196. velocity = m_linearVelocity+m_deltaLinearVelocity + (m_angularVelocity+m_deltaAngularVelocity).cross(rel_pos);
  197. }
  198. SIMD_FORCE_INLINE void internalGetAngularVelocity(btVector3& angVel) const
  199. {
  200. angVel = m_angularVelocity+m_deltaAngularVelocity;
  201. }
  202. //Optimization for the iterative solver: avoid calculating constant terms involving inertia, normal, relative position
  203. SIMD_FORCE_INLINE void internalApplyImpulse(const btVector3& linearComponent, const btVector3& angularComponent,const btScalar impulseMagnitude)
  204. {
  205. if (m_originalBody)
  206. {
  207. m_deltaLinearVelocity += linearComponent*impulseMagnitude*m_linearFactor;
  208. m_deltaAngularVelocity += angularComponent*(impulseMagnitude*m_angularFactor);
  209. }
  210. }
  211. void writebackVelocity()
  212. {
  213. if (m_originalBody)
  214. {
  215. m_linearVelocity +=m_deltaLinearVelocity;
  216. m_angularVelocity += m_deltaAngularVelocity;
  217. //m_originalBody->setCompanionId(-1);
  218. }
  219. }
  220. void writebackVelocityAndTransform(btScalar timeStep, btScalar splitImpulseTurnErp)
  221. {
  222. (void) timeStep;
  223. if (m_originalBody)
  224. {
  225. m_linearVelocity += m_deltaLinearVelocity;
  226. m_angularVelocity += m_deltaAngularVelocity;
  227. //correct the position/orientation based on push/turn recovery
  228. btTransform newTransform;
  229. if (m_pushVelocity[0]!=0.f || m_pushVelocity[1]!=0 || m_pushVelocity[2]!=0 || m_turnVelocity[0]!=0.f || m_turnVelocity[1]!=0 || m_turnVelocity[2]!=0)
  230. {
  231. // btQuaternion orn = m_worldTransform.getRotation();
  232. btTransformUtil::integrateTransform(m_worldTransform,m_pushVelocity,m_turnVelocity*splitImpulseTurnErp,timeStep,newTransform);
  233. m_worldTransform = newTransform;
  234. }
  235. //m_worldTransform.setRotation(orn);
  236. //m_originalBody->setCompanionId(-1);
  237. }
  238. }
  239. };
  240. #endif //BT_SOLVER_BODY_H