b3SolverBody.h 7.3 KB

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