b3SolverBody.h 7.3 KB

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