b3PgsJacobiSolver.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2012 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. //enable B3_SOLVER_DEBUG if you experience solver crashes
  14. //#define B3_SOLVER_DEBUG
  15. //#define COMPUTE_IMPULSE_DENOM 1
  16. //It is not necessary (redundant) to refresh contact manifolds, this refresh has been moved to the collision algorithms.
  17. //#define DISABLE_JOINTS
  18. #include "b3PgsJacobiSolver.h"
  19. #include "Bullet3Common/b3MinMax.h"
  20. #include "b3TypedConstraint.h"
  21. #include <new>
  22. #include "Bullet3Common/b3StackAlloc.h"
  23. //#include "b3SolverBody.h"
  24. //#include "b3SolverConstraint.h"
  25. #include "Bullet3Common/b3AlignedObjectArray.h"
  26. #include <string.h> //for memset
  27. //#include "../../dynamics/basic_demo/Stubs/AdlContact4.h"
  28. #include "Bullet3Collision/NarrowPhaseCollision/b3Contact4.h"
  29. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
  30. static b3Transform getWorldTransform(b3RigidBodyData* rb)
  31. {
  32. b3Transform newTrans;
  33. newTrans.setOrigin(rb->m_pos);
  34. newTrans.setRotation(rb->m_quat);
  35. return newTrans;
  36. }
  37. static const b3Matrix3x3& getInvInertiaTensorWorld(b3InertiaData* inertia)
  38. {
  39. return inertia->m_invInertiaWorld;
  40. }
  41. static const b3Vector3& getLinearVelocity(b3RigidBodyData* rb)
  42. {
  43. return rb->m_linVel;
  44. }
  45. static const b3Vector3& getAngularVelocity(b3RigidBodyData* rb)
  46. {
  47. return rb->m_angVel;
  48. }
  49. static b3Vector3 getVelocityInLocalPoint(b3RigidBodyData* rb, const b3Vector3& rel_pos)
  50. {
  51. //we also calculate lin/ang velocity for kinematic objects
  52. return getLinearVelocity(rb) + getAngularVelocity(rb).cross(rel_pos);
  53. }
  54. struct b3ContactPoint
  55. {
  56. b3Vector3 m_positionWorldOnA;
  57. b3Vector3 m_positionWorldOnB;
  58. b3Vector3 m_normalWorldOnB;
  59. b3Scalar m_appliedImpulse;
  60. b3Scalar m_distance;
  61. b3Scalar m_combinedRestitution;
  62. ///information related to friction
  63. b3Scalar m_combinedFriction;
  64. b3Vector3 m_lateralFrictionDir1;
  65. b3Vector3 m_lateralFrictionDir2;
  66. b3Scalar m_appliedImpulseLateral1;
  67. b3Scalar m_appliedImpulseLateral2;
  68. b3Scalar m_combinedRollingFriction;
  69. b3Scalar m_contactMotion1;
  70. b3Scalar m_contactMotion2;
  71. b3Scalar m_contactCFM1;
  72. b3Scalar m_contactCFM2;
  73. bool m_lateralFrictionInitialized;
  74. b3Vector3 getPositionWorldOnA()
  75. {
  76. return m_positionWorldOnA;
  77. }
  78. b3Vector3 getPositionWorldOnB()
  79. {
  80. return m_positionWorldOnB;
  81. }
  82. b3Scalar getDistance()
  83. {
  84. return m_distance;
  85. }
  86. };
  87. void getContactPoint(b3Contact4* contact, int contactIndex, b3ContactPoint& pointOut)
  88. {
  89. pointOut.m_appliedImpulse = 0.f;
  90. pointOut.m_appliedImpulseLateral1 = 0.f;
  91. pointOut.m_appliedImpulseLateral2 = 0.f;
  92. pointOut.m_combinedFriction = contact->getFrictionCoeff();
  93. pointOut.m_combinedRestitution = contact->getRestituitionCoeff();
  94. pointOut.m_combinedRollingFriction = 0.f;
  95. pointOut.m_contactCFM1 = 0.f;
  96. pointOut.m_contactCFM2 = 0.f;
  97. pointOut.m_contactMotion1 = 0.f;
  98. pointOut.m_contactMotion2 = 0.f;
  99. pointOut.m_distance = contact->getPenetration(contactIndex);//??0.01f
  100. b3Vector3 normalOnB = contact->m_worldNormalOnB;
  101. normalOnB.normalize();//is this needed?
  102. b3Vector3 l1,l2;
  103. b3PlaneSpace1(normalOnB,l1,l2);
  104. pointOut.m_normalWorldOnB = normalOnB;
  105. //printf("normalOnB = %f,%f,%f\n",normalOnB.getX(),normalOnB.getY(),normalOnB.getZ());
  106. pointOut.m_lateralFrictionDir1 = l1;
  107. pointOut.m_lateralFrictionDir2 = l2;
  108. pointOut.m_lateralFrictionInitialized = true;
  109. b3Vector3 worldPosB = contact->m_worldPosB[contactIndex];
  110. pointOut.m_positionWorldOnB = worldPosB;
  111. pointOut.m_positionWorldOnA = worldPosB+normalOnB*pointOut.m_distance;
  112. }
  113. int getNumContacts(b3Contact4* contact)
  114. {
  115. return contact->getNPoints();
  116. }
  117. b3PgsJacobiSolver::b3PgsJacobiSolver(bool usePgs)
  118. :m_usePgs(usePgs),
  119. m_numSplitImpulseRecoveries(0),
  120. m_btSeed2(0)
  121. {
  122. }
  123. b3PgsJacobiSolver::~b3PgsJacobiSolver()
  124. {
  125. }
  126. void b3PgsJacobiSolver::solveContacts(int numBodies, b3RigidBodyData* bodies, b3InertiaData* inertias, int numContacts, b3Contact4* contacts, int numConstraints, b3TypedConstraint** constraints)
  127. {
  128. b3ContactSolverInfo infoGlobal;
  129. infoGlobal.m_splitImpulse = false;
  130. infoGlobal.m_timeStep = 1.f/60.f;
  131. infoGlobal.m_numIterations = 4;//4;
  132. // infoGlobal.m_solverMode|=B3_SOLVER_USE_2_FRICTION_DIRECTIONS|B3_SOLVER_INTERLEAVE_CONTACT_AND_FRICTION_CONSTRAINTS|B3_SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION;
  133. //infoGlobal.m_solverMode|=B3_SOLVER_USE_2_FRICTION_DIRECTIONS|B3_SOLVER_INTERLEAVE_CONTACT_AND_FRICTION_CONSTRAINTS;
  134. infoGlobal.m_solverMode|=B3_SOLVER_USE_2_FRICTION_DIRECTIONS;
  135. //if (infoGlobal.m_solverMode & B3_SOLVER_INTERLEAVE_CONTACT_AND_FRICTION_CONSTRAINTS)
  136. //if ((infoGlobal.m_solverMode & B3_SOLVER_USE_2_FRICTION_DIRECTIONS) && (infoGlobal.m_solverMode & B3_SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION))
  137. solveGroup(bodies,inertias,numBodies,contacts,numContacts,constraints,numConstraints,infoGlobal);
  138. if (!numContacts)
  139. return;
  140. }
  141. /// b3PgsJacobiSolver Sequentially applies impulses
  142. b3Scalar b3PgsJacobiSolver::solveGroup(b3RigidBodyData* bodies,
  143. b3InertiaData* inertias,
  144. int numBodies,
  145. b3Contact4* manifoldPtr,
  146. int numManifolds,
  147. b3TypedConstraint** constraints,
  148. int numConstraints,
  149. const b3ContactSolverInfo& infoGlobal)
  150. {
  151. B3_PROFILE("solveGroup");
  152. //you need to provide at least some bodies
  153. solveGroupCacheFriendlySetup( bodies, inertias,numBodies, manifoldPtr, numManifolds,constraints, numConstraints,infoGlobal);
  154. solveGroupCacheFriendlyIterations(constraints, numConstraints,infoGlobal);
  155. solveGroupCacheFriendlyFinish(bodies, inertias,numBodies, infoGlobal);
  156. return 0.f;
  157. }
  158. #ifdef USE_SIMD
  159. #include <emmintrin.h>
  160. #define b3VecSplat(x, e) _mm_shuffle_ps(x, x, _MM_SHUFFLE(e,e,e,e))
  161. static inline __m128 b3SimdDot3( __m128 vec0, __m128 vec1 )
  162. {
  163. __m128 result = _mm_mul_ps( vec0, vec1);
  164. return _mm_add_ps( b3VecSplat( result, 0 ), _mm_add_ps( b3VecSplat( result, 1 ), b3VecSplat( result, 2 ) ) );
  165. }
  166. #endif//USE_SIMD
  167. // Project Gauss Seidel or the equivalent Sequential Impulse
  168. void b3PgsJacobiSolver::resolveSingleConstraintRowGenericSIMD(b3SolverBody& body1,b3SolverBody& body2,const b3SolverConstraint& c)
  169. {
  170. #ifdef USE_SIMD
  171. __m128 cpAppliedImp = _mm_set1_ps(c.m_appliedImpulse);
  172. __m128 lowerLimit1 = _mm_set1_ps(c.m_lowerLimit);
  173. __m128 upperLimit1 = _mm_set1_ps(c.m_upperLimit);
  174. __m128 deltaImpulse = _mm_sub_ps(_mm_set1_ps(c.m_rhs), _mm_mul_ps(_mm_set1_ps(c.m_appliedImpulse),_mm_set1_ps(c.m_cfm)));
  175. __m128 deltaVel1Dotn = _mm_add_ps(b3SimdDot3(c.m_contactNormal.mVec128,body1.internalGetDeltaLinearVelocity().mVec128), b3SimdDot3(c.m_relpos1CrossNormal.mVec128,body1.internalGetDeltaAngularVelocity().mVec128));
  176. __m128 deltaVel2Dotn = _mm_sub_ps(b3SimdDot3(c.m_relpos2CrossNormal.mVec128,body2.internalGetDeltaAngularVelocity().mVec128),b3SimdDot3((c.m_contactNormal).mVec128,body2.internalGetDeltaLinearVelocity().mVec128));
  177. deltaImpulse = _mm_sub_ps(deltaImpulse,_mm_mul_ps(deltaVel1Dotn,_mm_set1_ps(c.m_jacDiagABInv)));
  178. deltaImpulse = _mm_sub_ps(deltaImpulse,_mm_mul_ps(deltaVel2Dotn,_mm_set1_ps(c.m_jacDiagABInv)));
  179. b3SimdScalar sum = _mm_add_ps(cpAppliedImp,deltaImpulse);
  180. b3SimdScalar resultLowerLess,resultUpperLess;
  181. resultLowerLess = _mm_cmplt_ps(sum,lowerLimit1);
  182. resultUpperLess = _mm_cmplt_ps(sum,upperLimit1);
  183. __m128 lowMinApplied = _mm_sub_ps(lowerLimit1,cpAppliedImp);
  184. deltaImpulse = _mm_or_ps( _mm_and_ps(resultLowerLess, lowMinApplied), _mm_andnot_ps(resultLowerLess, deltaImpulse) );
  185. c.m_appliedImpulse = _mm_or_ps( _mm_and_ps(resultLowerLess, lowerLimit1), _mm_andnot_ps(resultLowerLess, sum) );
  186. __m128 upperMinApplied = _mm_sub_ps(upperLimit1,cpAppliedImp);
  187. deltaImpulse = _mm_or_ps( _mm_and_ps(resultUpperLess, deltaImpulse), _mm_andnot_ps(resultUpperLess, upperMinApplied) );
  188. c.m_appliedImpulse = _mm_or_ps( _mm_and_ps(resultUpperLess, c.m_appliedImpulse), _mm_andnot_ps(resultUpperLess, upperLimit1) );
  189. __m128 linearComponentA = _mm_mul_ps(c.m_contactNormal.mVec128,body1.internalGetInvMass().mVec128);
  190. __m128 linearComponentB = _mm_mul_ps((c.m_contactNormal).mVec128,body2.internalGetInvMass().mVec128);
  191. __m128 impulseMagnitude = deltaImpulse;
  192. body1.internalGetDeltaLinearVelocity().mVec128 = _mm_add_ps(body1.internalGetDeltaLinearVelocity().mVec128,_mm_mul_ps(linearComponentA,impulseMagnitude));
  193. body1.internalGetDeltaAngularVelocity().mVec128 = _mm_add_ps(body1.internalGetDeltaAngularVelocity().mVec128 ,_mm_mul_ps(c.m_angularComponentA.mVec128,impulseMagnitude));
  194. body2.internalGetDeltaLinearVelocity().mVec128 = _mm_sub_ps(body2.internalGetDeltaLinearVelocity().mVec128,_mm_mul_ps(linearComponentB,impulseMagnitude));
  195. body2.internalGetDeltaAngularVelocity().mVec128 = _mm_add_ps(body2.internalGetDeltaAngularVelocity().mVec128 ,_mm_mul_ps(c.m_angularComponentB.mVec128,impulseMagnitude));
  196. #else
  197. resolveSingleConstraintRowGeneric(body1,body2,c);
  198. #endif
  199. }
  200. // Project Gauss Seidel or the equivalent Sequential Impulse
  201. void b3PgsJacobiSolver::resolveSingleConstraintRowGeneric(b3SolverBody& body1,b3SolverBody& body2,const b3SolverConstraint& c)
  202. {
  203. b3Scalar deltaImpulse = c.m_rhs-b3Scalar(c.m_appliedImpulse)*c.m_cfm;
  204. const b3Scalar deltaVel1Dotn = c.m_contactNormal.dot(body1.internalGetDeltaLinearVelocity()) + c.m_relpos1CrossNormal.dot(body1.internalGetDeltaAngularVelocity());
  205. const b3Scalar deltaVel2Dotn = -c.m_contactNormal.dot(body2.internalGetDeltaLinearVelocity()) + c.m_relpos2CrossNormal.dot(body2.internalGetDeltaAngularVelocity());
  206. // const b3Scalar delta_rel_vel = deltaVel1Dotn-deltaVel2Dotn;
  207. deltaImpulse -= deltaVel1Dotn*c.m_jacDiagABInv;
  208. deltaImpulse -= deltaVel2Dotn*c.m_jacDiagABInv;
  209. const b3Scalar sum = b3Scalar(c.m_appliedImpulse) + deltaImpulse;
  210. if (sum < c.m_lowerLimit)
  211. {
  212. deltaImpulse = c.m_lowerLimit-c.m_appliedImpulse;
  213. c.m_appliedImpulse = c.m_lowerLimit;
  214. }
  215. else if (sum > c.m_upperLimit)
  216. {
  217. deltaImpulse = c.m_upperLimit-c.m_appliedImpulse;
  218. c.m_appliedImpulse = c.m_upperLimit;
  219. }
  220. else
  221. {
  222. c.m_appliedImpulse = sum;
  223. }
  224. body1.internalApplyImpulse(c.m_contactNormal*body1.internalGetInvMass(),c.m_angularComponentA,deltaImpulse);
  225. body2.internalApplyImpulse(-c.m_contactNormal*body2.internalGetInvMass(),c.m_angularComponentB,deltaImpulse);
  226. }
  227. void b3PgsJacobiSolver::resolveSingleConstraintRowLowerLimitSIMD(b3SolverBody& body1,b3SolverBody& body2,const b3SolverConstraint& c)
  228. {
  229. #ifdef USE_SIMD
  230. __m128 cpAppliedImp = _mm_set1_ps(c.m_appliedImpulse);
  231. __m128 lowerLimit1 = _mm_set1_ps(c.m_lowerLimit);
  232. __m128 upperLimit1 = _mm_set1_ps(c.m_upperLimit);
  233. __m128 deltaImpulse = _mm_sub_ps(_mm_set1_ps(c.m_rhs), _mm_mul_ps(_mm_set1_ps(c.m_appliedImpulse),_mm_set1_ps(c.m_cfm)));
  234. __m128 deltaVel1Dotn = _mm_add_ps(b3SimdDot3(c.m_contactNormal.mVec128,body1.internalGetDeltaLinearVelocity().mVec128), b3SimdDot3(c.m_relpos1CrossNormal.mVec128,body1.internalGetDeltaAngularVelocity().mVec128));
  235. __m128 deltaVel2Dotn = _mm_sub_ps(b3SimdDot3(c.m_relpos2CrossNormal.mVec128,body2.internalGetDeltaAngularVelocity().mVec128),b3SimdDot3((c.m_contactNormal).mVec128,body2.internalGetDeltaLinearVelocity().mVec128));
  236. deltaImpulse = _mm_sub_ps(deltaImpulse,_mm_mul_ps(deltaVel1Dotn,_mm_set1_ps(c.m_jacDiagABInv)));
  237. deltaImpulse = _mm_sub_ps(deltaImpulse,_mm_mul_ps(deltaVel2Dotn,_mm_set1_ps(c.m_jacDiagABInv)));
  238. b3SimdScalar sum = _mm_add_ps(cpAppliedImp,deltaImpulse);
  239. b3SimdScalar resultLowerLess,resultUpperLess;
  240. resultLowerLess = _mm_cmplt_ps(sum,lowerLimit1);
  241. resultUpperLess = _mm_cmplt_ps(sum,upperLimit1);
  242. __m128 lowMinApplied = _mm_sub_ps(lowerLimit1,cpAppliedImp);
  243. deltaImpulse = _mm_or_ps( _mm_and_ps(resultLowerLess, lowMinApplied), _mm_andnot_ps(resultLowerLess, deltaImpulse) );
  244. c.m_appliedImpulse = _mm_or_ps( _mm_and_ps(resultLowerLess, lowerLimit1), _mm_andnot_ps(resultLowerLess, sum) );
  245. __m128 linearComponentA = _mm_mul_ps(c.m_contactNormal.mVec128,body1.internalGetInvMass().mVec128);
  246. __m128 linearComponentB = _mm_mul_ps((c.m_contactNormal).mVec128,body2.internalGetInvMass().mVec128);
  247. __m128 impulseMagnitude = deltaImpulse;
  248. body1.internalGetDeltaLinearVelocity().mVec128 = _mm_add_ps(body1.internalGetDeltaLinearVelocity().mVec128,_mm_mul_ps(linearComponentA,impulseMagnitude));
  249. body1.internalGetDeltaAngularVelocity().mVec128 = _mm_add_ps(body1.internalGetDeltaAngularVelocity().mVec128 ,_mm_mul_ps(c.m_angularComponentA.mVec128,impulseMagnitude));
  250. body2.internalGetDeltaLinearVelocity().mVec128 = _mm_sub_ps(body2.internalGetDeltaLinearVelocity().mVec128,_mm_mul_ps(linearComponentB,impulseMagnitude));
  251. body2.internalGetDeltaAngularVelocity().mVec128 = _mm_add_ps(body2.internalGetDeltaAngularVelocity().mVec128 ,_mm_mul_ps(c.m_angularComponentB.mVec128,impulseMagnitude));
  252. #else
  253. resolveSingleConstraintRowLowerLimit(body1,body2,c);
  254. #endif
  255. }
  256. // Project Gauss Seidel or the equivalent Sequential Impulse
  257. void b3PgsJacobiSolver::resolveSingleConstraintRowLowerLimit(b3SolverBody& body1,b3SolverBody& body2,const b3SolverConstraint& c)
  258. {
  259. b3Scalar deltaImpulse = c.m_rhs-b3Scalar(c.m_appliedImpulse)*c.m_cfm;
  260. const b3Scalar deltaVel1Dotn = c.m_contactNormal.dot(body1.internalGetDeltaLinearVelocity()) + c.m_relpos1CrossNormal.dot(body1.internalGetDeltaAngularVelocity());
  261. const b3Scalar deltaVel2Dotn = -c.m_contactNormal.dot(body2.internalGetDeltaLinearVelocity()) + c.m_relpos2CrossNormal.dot(body2.internalGetDeltaAngularVelocity());
  262. deltaImpulse -= deltaVel1Dotn*c.m_jacDiagABInv;
  263. deltaImpulse -= deltaVel2Dotn*c.m_jacDiagABInv;
  264. const b3Scalar sum = b3Scalar(c.m_appliedImpulse) + deltaImpulse;
  265. if (sum < c.m_lowerLimit)
  266. {
  267. deltaImpulse = c.m_lowerLimit-c.m_appliedImpulse;
  268. c.m_appliedImpulse = c.m_lowerLimit;
  269. }
  270. else
  271. {
  272. c.m_appliedImpulse = sum;
  273. }
  274. body1.internalApplyImpulse(c.m_contactNormal*body1.internalGetInvMass(),c.m_angularComponentA,deltaImpulse);
  275. body2.internalApplyImpulse(-c.m_contactNormal*body2.internalGetInvMass(),c.m_angularComponentB,deltaImpulse);
  276. }
  277. void b3PgsJacobiSolver::resolveSplitPenetrationImpulseCacheFriendly(
  278. b3SolverBody& body1,
  279. b3SolverBody& body2,
  280. const b3SolverConstraint& c)
  281. {
  282. if (c.m_rhsPenetration)
  283. {
  284. m_numSplitImpulseRecoveries++;
  285. b3Scalar deltaImpulse = c.m_rhsPenetration-b3Scalar(c.m_appliedPushImpulse)*c.m_cfm;
  286. const b3Scalar deltaVel1Dotn = c.m_contactNormal.dot(body1.internalGetPushVelocity()) + c.m_relpos1CrossNormal.dot(body1.internalGetTurnVelocity());
  287. const b3Scalar deltaVel2Dotn = -c.m_contactNormal.dot(body2.internalGetPushVelocity()) + c.m_relpos2CrossNormal.dot(body2.internalGetTurnVelocity());
  288. deltaImpulse -= deltaVel1Dotn*c.m_jacDiagABInv;
  289. deltaImpulse -= deltaVel2Dotn*c.m_jacDiagABInv;
  290. const b3Scalar sum = b3Scalar(c.m_appliedPushImpulse) + deltaImpulse;
  291. if (sum < c.m_lowerLimit)
  292. {
  293. deltaImpulse = c.m_lowerLimit-c.m_appliedPushImpulse;
  294. c.m_appliedPushImpulse = c.m_lowerLimit;
  295. }
  296. else
  297. {
  298. c.m_appliedPushImpulse = sum;
  299. }
  300. body1.internalApplyPushImpulse(c.m_contactNormal*body1.internalGetInvMass(),c.m_angularComponentA,deltaImpulse);
  301. body2.internalApplyPushImpulse(-c.m_contactNormal*body2.internalGetInvMass(),c.m_angularComponentB,deltaImpulse);
  302. }
  303. }
  304. void b3PgsJacobiSolver::resolveSplitPenetrationSIMD(b3SolverBody& body1,b3SolverBody& body2,const b3SolverConstraint& c)
  305. {
  306. #ifdef USE_SIMD
  307. if (!c.m_rhsPenetration)
  308. return;
  309. m_numSplitImpulseRecoveries++;
  310. __m128 cpAppliedImp = _mm_set1_ps(c.m_appliedPushImpulse);
  311. __m128 lowerLimit1 = _mm_set1_ps(c.m_lowerLimit);
  312. __m128 upperLimit1 = _mm_set1_ps(c.m_upperLimit);
  313. __m128 deltaImpulse = _mm_sub_ps(_mm_set1_ps(c.m_rhsPenetration), _mm_mul_ps(_mm_set1_ps(c.m_appliedPushImpulse),_mm_set1_ps(c.m_cfm)));
  314. __m128 deltaVel1Dotn = _mm_add_ps(b3SimdDot3(c.m_contactNormal.mVec128,body1.internalGetPushVelocity().mVec128), b3SimdDot3(c.m_relpos1CrossNormal.mVec128,body1.internalGetTurnVelocity().mVec128));
  315. __m128 deltaVel2Dotn = _mm_sub_ps(b3SimdDot3(c.m_relpos2CrossNormal.mVec128,body2.internalGetTurnVelocity().mVec128),b3SimdDot3((c.m_contactNormal).mVec128,body2.internalGetPushVelocity().mVec128));
  316. deltaImpulse = _mm_sub_ps(deltaImpulse,_mm_mul_ps(deltaVel1Dotn,_mm_set1_ps(c.m_jacDiagABInv)));
  317. deltaImpulse = _mm_sub_ps(deltaImpulse,_mm_mul_ps(deltaVel2Dotn,_mm_set1_ps(c.m_jacDiagABInv)));
  318. b3SimdScalar sum = _mm_add_ps(cpAppliedImp,deltaImpulse);
  319. b3SimdScalar resultLowerLess,resultUpperLess;
  320. resultLowerLess = _mm_cmplt_ps(sum,lowerLimit1);
  321. resultUpperLess = _mm_cmplt_ps(sum,upperLimit1);
  322. __m128 lowMinApplied = _mm_sub_ps(lowerLimit1,cpAppliedImp);
  323. deltaImpulse = _mm_or_ps( _mm_and_ps(resultLowerLess, lowMinApplied), _mm_andnot_ps(resultLowerLess, deltaImpulse) );
  324. c.m_appliedPushImpulse = _mm_or_ps( _mm_and_ps(resultLowerLess, lowerLimit1), _mm_andnot_ps(resultLowerLess, sum) );
  325. __m128 linearComponentA = _mm_mul_ps(c.m_contactNormal.mVec128,body1.internalGetInvMass().mVec128);
  326. __m128 linearComponentB = _mm_mul_ps((c.m_contactNormal).mVec128,body2.internalGetInvMass().mVec128);
  327. __m128 impulseMagnitude = deltaImpulse;
  328. body1.internalGetPushVelocity().mVec128 = _mm_add_ps(body1.internalGetPushVelocity().mVec128,_mm_mul_ps(linearComponentA,impulseMagnitude));
  329. body1.internalGetTurnVelocity().mVec128 = _mm_add_ps(body1.internalGetTurnVelocity().mVec128 ,_mm_mul_ps(c.m_angularComponentA.mVec128,impulseMagnitude));
  330. body2.internalGetPushVelocity().mVec128 = _mm_sub_ps(body2.internalGetPushVelocity().mVec128,_mm_mul_ps(linearComponentB,impulseMagnitude));
  331. body2.internalGetTurnVelocity().mVec128 = _mm_add_ps(body2.internalGetTurnVelocity().mVec128 ,_mm_mul_ps(c.m_angularComponentB.mVec128,impulseMagnitude));
  332. #else
  333. resolveSplitPenetrationImpulseCacheFriendly(body1,body2,c);
  334. #endif
  335. }
  336. unsigned long b3PgsJacobiSolver::b3Rand2()
  337. {
  338. m_btSeed2 = (1664525L*m_btSeed2 + 1013904223L) & 0xffffffff;
  339. return m_btSeed2;
  340. }
  341. //See ODE: adam's all-int straightforward(?) dRandInt (0..n-1)
  342. int b3PgsJacobiSolver::b3RandInt2 (int n)
  343. {
  344. // seems good; xor-fold and modulus
  345. const unsigned long un = static_cast<unsigned long>(n);
  346. unsigned long r = b3Rand2();
  347. // note: probably more aggressive than it needs to be -- might be
  348. // able to get away without one or two of the innermost branches.
  349. if (un <= 0x00010000UL) {
  350. r ^= (r >> 16);
  351. if (un <= 0x00000100UL) {
  352. r ^= (r >> 8);
  353. if (un <= 0x00000010UL) {
  354. r ^= (r >> 4);
  355. if (un <= 0x00000004UL) {
  356. r ^= (r >> 2);
  357. if (un <= 0x00000002UL) {
  358. r ^= (r >> 1);
  359. }
  360. }
  361. }
  362. }
  363. }
  364. return (int) (r % un);
  365. }
  366. void b3PgsJacobiSolver::initSolverBody(int bodyIndex, b3SolverBody* solverBody, b3RigidBodyData* rb)
  367. {
  368. solverBody->m_deltaLinearVelocity.setValue(0.f,0.f,0.f);
  369. solverBody->m_deltaAngularVelocity.setValue(0.f,0.f,0.f);
  370. solverBody->internalGetPushVelocity().setValue(0.f,0.f,0.f);
  371. solverBody->internalGetTurnVelocity().setValue(0.f,0.f,0.f);
  372. if (rb)
  373. {
  374. solverBody->m_worldTransform = getWorldTransform(rb);
  375. solverBody->internalSetInvMass(b3MakeVector3(rb->m_invMass,rb->m_invMass,rb->m_invMass));
  376. solverBody->m_originalBodyIndex = bodyIndex;
  377. solverBody->m_angularFactor = b3MakeVector3(1,1,1);
  378. solverBody->m_linearFactor = b3MakeVector3(1,1,1);
  379. solverBody->m_linearVelocity = getLinearVelocity(rb);
  380. solverBody->m_angularVelocity = getAngularVelocity(rb);
  381. } else
  382. {
  383. solverBody->m_worldTransform.setIdentity();
  384. solverBody->internalSetInvMass(b3MakeVector3(0,0,0));
  385. solverBody->m_originalBodyIndex = bodyIndex;
  386. solverBody->m_angularFactor.setValue(1,1,1);
  387. solverBody->m_linearFactor.setValue(1,1,1);
  388. solverBody->m_linearVelocity.setValue(0,0,0);
  389. solverBody->m_angularVelocity.setValue(0,0,0);
  390. }
  391. }
  392. b3Scalar b3PgsJacobiSolver::restitutionCurve(b3Scalar rel_vel, b3Scalar restitution)
  393. {
  394. b3Scalar rest = restitution * -rel_vel;
  395. return rest;
  396. }
  397. void b3PgsJacobiSolver::setupFrictionConstraint(b3RigidBodyData* bodies,b3InertiaData* inertias, b3SolverConstraint& solverConstraint, const b3Vector3& normalAxis,int solverBodyIdA,int solverBodyIdB,b3ContactPoint& cp,const b3Vector3& rel_pos1,const b3Vector3& rel_pos2,b3RigidBodyData* colObj0,b3RigidBodyData* colObj1, b3Scalar relaxation, b3Scalar desiredVelocity, b3Scalar cfmSlip)
  398. {
  399. solverConstraint.m_contactNormal = normalAxis;
  400. b3SolverBody& solverBodyA = m_tmpSolverBodyPool[solverBodyIdA];
  401. b3SolverBody& solverBodyB = m_tmpSolverBodyPool[solverBodyIdB];
  402. b3RigidBodyData* body0 = &bodies[solverBodyA.m_originalBodyIndex];
  403. b3RigidBodyData* body1 = &bodies[solverBodyB.m_originalBodyIndex];
  404. solverConstraint.m_solverBodyIdA = solverBodyIdA;
  405. solverConstraint.m_solverBodyIdB = solverBodyIdB;
  406. solverConstraint.m_friction = cp.m_combinedFriction;
  407. solverConstraint.m_originalContactPoint = 0;
  408. solverConstraint.m_appliedImpulse = 0.f;
  409. solverConstraint.m_appliedPushImpulse = 0.f;
  410. {
  411. b3Vector3 ftorqueAxis1 = rel_pos1.cross(solverConstraint.m_contactNormal);
  412. solverConstraint.m_relpos1CrossNormal = ftorqueAxis1;
  413. solverConstraint.m_angularComponentA = body0 ? getInvInertiaTensorWorld(&inertias[solverBodyA.m_originalBodyIndex])*ftorqueAxis1 : b3MakeVector3(0,0,0);
  414. }
  415. {
  416. b3Vector3 ftorqueAxis1 = rel_pos2.cross(-solverConstraint.m_contactNormal);
  417. solverConstraint.m_relpos2CrossNormal = ftorqueAxis1;
  418. solverConstraint.m_angularComponentB = body1 ? getInvInertiaTensorWorld(&inertias[solverBodyB.m_originalBodyIndex])*ftorqueAxis1 : b3MakeVector3(0,0,0);
  419. }
  420. b3Scalar scaledDenom;
  421. {
  422. b3Vector3 vec;
  423. b3Scalar denom0 = 0.f;
  424. b3Scalar denom1 = 0.f;
  425. if (body0)
  426. {
  427. vec = ( solverConstraint.m_angularComponentA).cross(rel_pos1);
  428. denom0 = body0->m_invMass + normalAxis.dot(vec);
  429. }
  430. if (body1)
  431. {
  432. vec = ( -solverConstraint.m_angularComponentB).cross(rel_pos2);
  433. denom1 = body1->m_invMass + normalAxis.dot(vec);
  434. }
  435. b3Scalar denom;
  436. if (m_usePgs)
  437. {
  438. scaledDenom = denom = relaxation/(denom0+denom1);
  439. } else
  440. {
  441. denom = relaxation/(denom0+denom1);
  442. b3Scalar countA = body0->m_invMass ? b3Scalar(m_bodyCount[solverBodyA.m_originalBodyIndex]): 1.f;
  443. b3Scalar countB = body1->m_invMass ? b3Scalar(m_bodyCount[solverBodyB.m_originalBodyIndex]): 1.f;
  444. scaledDenom = relaxation/(denom0*countA+denom1*countB);
  445. }
  446. solverConstraint.m_jacDiagABInv = denom;
  447. }
  448. {
  449. b3Scalar rel_vel;
  450. b3Scalar vel1Dotn = solverConstraint.m_contactNormal.dot(body0?solverBodyA.m_linearVelocity:b3MakeVector3(0,0,0))
  451. + solverConstraint.m_relpos1CrossNormal.dot(body0?solverBodyA.m_angularVelocity:b3MakeVector3(0,0,0));
  452. b3Scalar vel2Dotn = -solverConstraint.m_contactNormal.dot(body1?solverBodyB.m_linearVelocity:b3MakeVector3(0,0,0))
  453. + solverConstraint.m_relpos2CrossNormal.dot(body1?solverBodyB.m_angularVelocity:b3MakeVector3(0,0,0));
  454. rel_vel = vel1Dotn+vel2Dotn;
  455. // b3Scalar positionalError = 0.f;
  456. b3SimdScalar velocityError = desiredVelocity - rel_vel;
  457. b3SimdScalar velocityImpulse = velocityError * b3SimdScalar(scaledDenom);//solverConstraint.m_jacDiagABInv);
  458. solverConstraint.m_rhs = velocityImpulse;
  459. solverConstraint.m_cfm = cfmSlip;
  460. solverConstraint.m_lowerLimit = 0;
  461. solverConstraint.m_upperLimit = 1e10f;
  462. }
  463. }
  464. b3SolverConstraint& b3PgsJacobiSolver::addFrictionConstraint(b3RigidBodyData* bodies,b3InertiaData* inertias, const b3Vector3& normalAxis,int solverBodyIdA,int solverBodyIdB,int frictionIndex,b3ContactPoint& cp,const b3Vector3& rel_pos1,const b3Vector3& rel_pos2,b3RigidBodyData* colObj0,b3RigidBodyData* colObj1, b3Scalar relaxation, b3Scalar desiredVelocity, b3Scalar cfmSlip)
  465. {
  466. b3SolverConstraint& solverConstraint = m_tmpSolverContactFrictionConstraintPool.expandNonInitializing();
  467. solverConstraint.m_frictionIndex = frictionIndex;
  468. setupFrictionConstraint(bodies,inertias,solverConstraint, normalAxis, solverBodyIdA, solverBodyIdB, cp, rel_pos1, rel_pos2,
  469. colObj0, colObj1, relaxation, desiredVelocity, cfmSlip);
  470. return solverConstraint;
  471. }
  472. void b3PgsJacobiSolver::setupRollingFrictionConstraint(b3RigidBodyData* bodies,b3InertiaData* inertias, b3SolverConstraint& solverConstraint, const b3Vector3& normalAxis1,int solverBodyIdA,int solverBodyIdB,
  473. b3ContactPoint& cp,const b3Vector3& rel_pos1,const b3Vector3& rel_pos2,
  474. b3RigidBodyData* colObj0,b3RigidBodyData* colObj1, b3Scalar relaxation,
  475. b3Scalar desiredVelocity, b3Scalar cfmSlip)
  476. {
  477. b3Vector3 normalAxis=b3MakeVector3(0,0,0);
  478. solverConstraint.m_contactNormal = normalAxis;
  479. b3SolverBody& solverBodyA = m_tmpSolverBodyPool[solverBodyIdA];
  480. b3SolverBody& solverBodyB = m_tmpSolverBodyPool[solverBodyIdB];
  481. b3RigidBodyData* body0 = &bodies[m_tmpSolverBodyPool[solverBodyIdA].m_originalBodyIndex];
  482. b3RigidBodyData* body1 = &bodies[m_tmpSolverBodyPool[solverBodyIdB].m_originalBodyIndex];
  483. solverConstraint.m_solverBodyIdA = solverBodyIdA;
  484. solverConstraint.m_solverBodyIdB = solverBodyIdB;
  485. solverConstraint.m_friction = cp.m_combinedRollingFriction;
  486. solverConstraint.m_originalContactPoint = 0;
  487. solverConstraint.m_appliedImpulse = 0.f;
  488. solverConstraint.m_appliedPushImpulse = 0.f;
  489. {
  490. b3Vector3 ftorqueAxis1 = -normalAxis1;
  491. solverConstraint.m_relpos1CrossNormal = ftorqueAxis1;
  492. solverConstraint.m_angularComponentA = body0 ? getInvInertiaTensorWorld(&inertias[solverBodyA.m_originalBodyIndex])*ftorqueAxis1 : b3MakeVector3(0,0,0);
  493. }
  494. {
  495. b3Vector3 ftorqueAxis1 = normalAxis1;
  496. solverConstraint.m_relpos2CrossNormal = ftorqueAxis1;
  497. solverConstraint.m_angularComponentB = body1 ? getInvInertiaTensorWorld(&inertias[solverBodyB.m_originalBodyIndex])*ftorqueAxis1 : b3MakeVector3(0,0,0);
  498. }
  499. {
  500. b3Vector3 iMJaA = body0?getInvInertiaTensorWorld(&inertias[solverBodyA.m_originalBodyIndex])*solverConstraint.m_relpos1CrossNormal:b3MakeVector3(0,0,0);
  501. b3Vector3 iMJaB = body1?getInvInertiaTensorWorld(&inertias[solverBodyB.m_originalBodyIndex])*solverConstraint.m_relpos2CrossNormal:b3MakeVector3(0,0,0);
  502. b3Scalar sum = 0;
  503. sum += iMJaA.dot(solverConstraint.m_relpos1CrossNormal);
  504. sum += iMJaB.dot(solverConstraint.m_relpos2CrossNormal);
  505. solverConstraint.m_jacDiagABInv = b3Scalar(1.)/sum;
  506. }
  507. {
  508. b3Scalar rel_vel;
  509. b3Scalar vel1Dotn = solverConstraint.m_contactNormal.dot(body0?solverBodyA.m_linearVelocity:b3MakeVector3(0,0,0))
  510. + solverConstraint.m_relpos1CrossNormal.dot(body0?solverBodyA.m_angularVelocity:b3MakeVector3(0,0,0));
  511. b3Scalar vel2Dotn = -solverConstraint.m_contactNormal.dot(body1?solverBodyB.m_linearVelocity:b3MakeVector3(0,0,0))
  512. + solverConstraint.m_relpos2CrossNormal.dot(body1?solverBodyB.m_angularVelocity:b3MakeVector3(0,0,0));
  513. rel_vel = vel1Dotn+vel2Dotn;
  514. // b3Scalar positionalError = 0.f;
  515. b3SimdScalar velocityError = desiredVelocity - rel_vel;
  516. b3SimdScalar velocityImpulse = velocityError * b3SimdScalar(solverConstraint.m_jacDiagABInv);
  517. solverConstraint.m_rhs = velocityImpulse;
  518. solverConstraint.m_cfm = cfmSlip;
  519. solverConstraint.m_lowerLimit = 0;
  520. solverConstraint.m_upperLimit = 1e10f;
  521. }
  522. }
  523. b3SolverConstraint& b3PgsJacobiSolver::addRollingFrictionConstraint(b3RigidBodyData* bodies,b3InertiaData* inertias,const b3Vector3& normalAxis,int solverBodyIdA,int solverBodyIdB,int frictionIndex,b3ContactPoint& cp,const b3Vector3& rel_pos1,const b3Vector3& rel_pos2,b3RigidBodyData* colObj0,b3RigidBodyData* colObj1, b3Scalar relaxation, b3Scalar desiredVelocity, b3Scalar cfmSlip)
  524. {
  525. b3SolverConstraint& solverConstraint = m_tmpSolverContactRollingFrictionConstraintPool.expandNonInitializing();
  526. solverConstraint.m_frictionIndex = frictionIndex;
  527. setupRollingFrictionConstraint(bodies,inertias,solverConstraint, normalAxis, solverBodyIdA, solverBodyIdB, cp, rel_pos1, rel_pos2,
  528. colObj0, colObj1, relaxation, desiredVelocity, cfmSlip);
  529. return solverConstraint;
  530. }
  531. int b3PgsJacobiSolver::getOrInitSolverBody(int bodyIndex, b3RigidBodyData* bodies,b3InertiaData* inertias)
  532. {
  533. //b3Assert(bodyIndex< m_tmpSolverBodyPool.size());
  534. b3RigidBodyData& body = bodies[bodyIndex];
  535. int curIndex = -1;
  536. if (m_usePgs || body.m_invMass==0.f)
  537. {
  538. if (m_bodyCount[bodyIndex]<0)
  539. {
  540. curIndex = m_tmpSolverBodyPool.size();
  541. b3SolverBody& solverBody = m_tmpSolverBodyPool.expand();
  542. initSolverBody(bodyIndex,&solverBody,&body);
  543. solverBody.m_originalBodyIndex = bodyIndex;
  544. m_bodyCount[bodyIndex] = curIndex;
  545. } else
  546. {
  547. curIndex = m_bodyCount[bodyIndex];
  548. }
  549. } else
  550. {
  551. b3Assert(m_bodyCount[bodyIndex]>0);
  552. m_bodyCountCheck[bodyIndex]++;
  553. curIndex = m_tmpSolverBodyPool.size();
  554. b3SolverBody& solverBody = m_tmpSolverBodyPool.expand();
  555. initSolverBody(bodyIndex,&solverBody,&body);
  556. solverBody.m_originalBodyIndex = bodyIndex;
  557. }
  558. b3Assert(curIndex>=0);
  559. return curIndex;
  560. }
  561. #include <stdio.h>
  562. void b3PgsJacobiSolver::setupContactConstraint(b3RigidBodyData* bodies, b3InertiaData* inertias,b3SolverConstraint& solverConstraint,
  563. int solverBodyIdA, int solverBodyIdB,
  564. b3ContactPoint& cp, const b3ContactSolverInfo& infoGlobal,
  565. b3Vector3& vel, b3Scalar& rel_vel, b3Scalar& relaxation,
  566. b3Vector3& rel_pos1, b3Vector3& rel_pos2)
  567. {
  568. const b3Vector3& pos1 = cp.getPositionWorldOnA();
  569. const b3Vector3& pos2 = cp.getPositionWorldOnB();
  570. b3SolverBody* bodyA = &m_tmpSolverBodyPool[solverBodyIdA];
  571. b3SolverBody* bodyB = &m_tmpSolverBodyPool[solverBodyIdB];
  572. b3RigidBodyData* rb0 = &bodies[bodyA->m_originalBodyIndex];
  573. b3RigidBodyData* rb1 = &bodies[bodyB->m_originalBodyIndex];
  574. // b3Vector3 rel_pos1 = pos1 - colObj0->getWorldTransform().getOrigin();
  575. // b3Vector3 rel_pos2 = pos2 - colObj1->getWorldTransform().getOrigin();
  576. rel_pos1 = pos1 - bodyA->getWorldTransform().getOrigin();
  577. rel_pos2 = pos2 - bodyB->getWorldTransform().getOrigin();
  578. relaxation = 1.f;
  579. b3Vector3 torqueAxis0 = rel_pos1.cross(cp.m_normalWorldOnB);
  580. solverConstraint.m_angularComponentA = rb0 ? getInvInertiaTensorWorld(&inertias[bodyA->m_originalBodyIndex])*torqueAxis0 : b3MakeVector3(0,0,0);
  581. b3Vector3 torqueAxis1 = rel_pos2.cross(cp.m_normalWorldOnB);
  582. solverConstraint.m_angularComponentB = rb1 ? getInvInertiaTensorWorld(&inertias[bodyB->m_originalBodyIndex])*-torqueAxis1 : b3MakeVector3(0,0,0);
  583. b3Scalar scaledDenom;
  584. {
  585. #ifdef COMPUTE_IMPULSE_DENOM
  586. b3Scalar denom0 = rb0->computeImpulseDenominator(pos1,cp.m_normalWorldOnB);
  587. b3Scalar denom1 = rb1->computeImpulseDenominator(pos2,cp.m_normalWorldOnB);
  588. #else
  589. b3Vector3 vec;
  590. b3Scalar denom0 = 0.f;
  591. b3Scalar denom1 = 0.f;
  592. if (rb0)
  593. {
  594. vec = ( solverConstraint.m_angularComponentA).cross(rel_pos1);
  595. denom0 = rb0->m_invMass + cp.m_normalWorldOnB.dot(vec);
  596. }
  597. if (rb1)
  598. {
  599. vec = ( -solverConstraint.m_angularComponentB).cross(rel_pos2);
  600. denom1 = rb1->m_invMass + cp.m_normalWorldOnB.dot(vec);
  601. }
  602. #endif //COMPUTE_IMPULSE_DENOM
  603. b3Scalar denom;
  604. if (m_usePgs)
  605. {
  606. scaledDenom = denom = relaxation/(denom0+denom1);
  607. } else
  608. {
  609. denom = relaxation/(denom0+denom1);
  610. b3Scalar countA = rb0->m_invMass? b3Scalar(m_bodyCount[bodyA->m_originalBodyIndex]) : 1.f;
  611. b3Scalar countB = rb1->m_invMass? b3Scalar(m_bodyCount[bodyB->m_originalBodyIndex]) : 1.f;
  612. scaledDenom = relaxation/(denom0*countA+denom1*countB);
  613. }
  614. solverConstraint.m_jacDiagABInv = denom;
  615. }
  616. solverConstraint.m_contactNormal = cp.m_normalWorldOnB;
  617. solverConstraint.m_relpos1CrossNormal = torqueAxis0;
  618. solverConstraint.m_relpos2CrossNormal = -torqueAxis1;
  619. b3Scalar restitution = 0.f;
  620. b3Scalar penetration = cp.getDistance()+infoGlobal.m_linearSlop;
  621. {
  622. b3Vector3 vel1,vel2;
  623. vel1 = rb0? getVelocityInLocalPoint(rb0,rel_pos1) : b3MakeVector3(0,0,0);
  624. vel2 = rb1? getVelocityInLocalPoint(rb1, rel_pos2) : b3MakeVector3(0,0,0);
  625. // b3Vector3 vel2 = rb1 ? rb1->getVelocityInLocalPoint(rel_pos2) : b3Vector3(0,0,0);
  626. vel = vel1 - vel2;
  627. rel_vel = cp.m_normalWorldOnB.dot(vel);
  628. solverConstraint.m_friction = cp.m_combinedFriction;
  629. restitution = restitutionCurve(rel_vel, cp.m_combinedRestitution);
  630. if (restitution <= b3Scalar(0.))
  631. {
  632. restitution = 0.f;
  633. };
  634. }
  635. ///warm starting (or zero if disabled)
  636. if (infoGlobal.m_solverMode & B3_SOLVER_USE_WARMSTARTING)
  637. {
  638. solverConstraint.m_appliedImpulse = cp.m_appliedImpulse * infoGlobal.m_warmstartingFactor;
  639. if (rb0)
  640. bodyA->internalApplyImpulse(solverConstraint.m_contactNormal*bodyA->internalGetInvMass(),solverConstraint.m_angularComponentA,solverConstraint.m_appliedImpulse);
  641. if (rb1)
  642. bodyB->internalApplyImpulse(solverConstraint.m_contactNormal*bodyB->internalGetInvMass(),-solverConstraint.m_angularComponentB,-(b3Scalar)solverConstraint.m_appliedImpulse);
  643. } else
  644. {
  645. solverConstraint.m_appliedImpulse = 0.f;
  646. }
  647. solverConstraint.m_appliedPushImpulse = 0.f;
  648. {
  649. b3Scalar vel1Dotn = solverConstraint.m_contactNormal.dot(rb0?bodyA->m_linearVelocity:b3MakeVector3(0,0,0))
  650. + solverConstraint.m_relpos1CrossNormal.dot(rb0?bodyA->m_angularVelocity:b3MakeVector3(0,0,0));
  651. b3Scalar vel2Dotn = -solverConstraint.m_contactNormal.dot(rb1?bodyB->m_linearVelocity:b3MakeVector3(0,0,0))
  652. + solverConstraint.m_relpos2CrossNormal.dot(rb1?bodyB->m_angularVelocity:b3MakeVector3(0,0,0));
  653. b3Scalar rel_vel = vel1Dotn+vel2Dotn;
  654. b3Scalar positionalError = 0.f;
  655. b3Scalar velocityError = restitution - rel_vel;// * damping;
  656. b3Scalar erp = infoGlobal.m_erp2;
  657. if (!infoGlobal.m_splitImpulse || (penetration > infoGlobal.m_splitImpulsePenetrationThreshold))
  658. {
  659. erp = infoGlobal.m_erp;
  660. }
  661. if (penetration>0)
  662. {
  663. positionalError = 0;
  664. velocityError -= penetration / infoGlobal.m_timeStep;
  665. } else
  666. {
  667. positionalError = -penetration * erp/infoGlobal.m_timeStep;
  668. }
  669. b3Scalar penetrationImpulse = positionalError*scaledDenom;//solverConstraint.m_jacDiagABInv;
  670. b3Scalar velocityImpulse = velocityError *scaledDenom;//solverConstraint.m_jacDiagABInv;
  671. if (!infoGlobal.m_splitImpulse || (penetration > infoGlobal.m_splitImpulsePenetrationThreshold))
  672. {
  673. //combine position and velocity into rhs
  674. solverConstraint.m_rhs = penetrationImpulse+velocityImpulse;
  675. solverConstraint.m_rhsPenetration = 0.f;
  676. } else
  677. {
  678. //split position and velocity into rhs and m_rhsPenetration
  679. solverConstraint.m_rhs = velocityImpulse;
  680. solverConstraint.m_rhsPenetration = penetrationImpulse;
  681. }
  682. solverConstraint.m_cfm = 0.f;
  683. solverConstraint.m_lowerLimit = 0;
  684. solverConstraint.m_upperLimit = 1e10f;
  685. }
  686. }
  687. void b3PgsJacobiSolver::setFrictionConstraintImpulse( b3RigidBodyData* bodies, b3InertiaData* inertias,b3SolverConstraint& solverConstraint,
  688. int solverBodyIdA, int solverBodyIdB,
  689. b3ContactPoint& cp, const b3ContactSolverInfo& infoGlobal)
  690. {
  691. b3SolverBody* bodyA = &m_tmpSolverBodyPool[solverBodyIdA];
  692. b3SolverBody* bodyB = &m_tmpSolverBodyPool[solverBodyIdB];
  693. {
  694. b3SolverConstraint& frictionConstraint1 = m_tmpSolverContactFrictionConstraintPool[solverConstraint.m_frictionIndex];
  695. if (infoGlobal.m_solverMode & B3_SOLVER_USE_WARMSTARTING)
  696. {
  697. frictionConstraint1.m_appliedImpulse = cp.m_appliedImpulseLateral1 * infoGlobal.m_warmstartingFactor;
  698. if (bodies[bodyA->m_originalBodyIndex].m_invMass)
  699. bodyA->internalApplyImpulse(frictionConstraint1.m_contactNormal*bodies[bodyA->m_originalBodyIndex].m_invMass,frictionConstraint1.m_angularComponentA,frictionConstraint1.m_appliedImpulse);
  700. if (bodies[bodyB->m_originalBodyIndex].m_invMass)
  701. bodyB->internalApplyImpulse(frictionConstraint1.m_contactNormal*bodies[bodyB->m_originalBodyIndex].m_invMass,-frictionConstraint1.m_angularComponentB,-(b3Scalar)frictionConstraint1.m_appliedImpulse);
  702. } else
  703. {
  704. frictionConstraint1.m_appliedImpulse = 0.f;
  705. }
  706. }
  707. if ((infoGlobal.m_solverMode & B3_SOLVER_USE_2_FRICTION_DIRECTIONS))
  708. {
  709. b3SolverConstraint& frictionConstraint2 = m_tmpSolverContactFrictionConstraintPool[solverConstraint.m_frictionIndex+1];
  710. if (infoGlobal.m_solverMode & B3_SOLVER_USE_WARMSTARTING)
  711. {
  712. frictionConstraint2.m_appliedImpulse = cp.m_appliedImpulseLateral2 * infoGlobal.m_warmstartingFactor;
  713. if (bodies[bodyA->m_originalBodyIndex].m_invMass)
  714. bodyA->internalApplyImpulse(frictionConstraint2.m_contactNormal*bodies[bodyA->m_originalBodyIndex].m_invMass,frictionConstraint2.m_angularComponentA,frictionConstraint2.m_appliedImpulse);
  715. if (bodies[bodyB->m_originalBodyIndex].m_invMass)
  716. bodyB->internalApplyImpulse(frictionConstraint2.m_contactNormal*bodies[bodyB->m_originalBodyIndex].m_invMass,-frictionConstraint2.m_angularComponentB,-(b3Scalar)frictionConstraint2.m_appliedImpulse);
  717. } else
  718. {
  719. frictionConstraint2.m_appliedImpulse = 0.f;
  720. }
  721. }
  722. }
  723. void b3PgsJacobiSolver::convertContact(b3RigidBodyData* bodies, b3InertiaData* inertias,b3Contact4* manifold,const b3ContactSolverInfo& infoGlobal)
  724. {
  725. b3RigidBodyData* colObj0=0,*colObj1=0;
  726. int solverBodyIdA = getOrInitSolverBody(manifold->getBodyA(),bodies,inertias);
  727. int solverBodyIdB = getOrInitSolverBody(manifold->getBodyB(),bodies,inertias);
  728. // b3RigidBody* bodyA = b3RigidBody::upcast(colObj0);
  729. // b3RigidBody* bodyB = b3RigidBody::upcast(colObj1);
  730. b3SolverBody* solverBodyA = &m_tmpSolverBodyPool[solverBodyIdA];
  731. b3SolverBody* solverBodyB = &m_tmpSolverBodyPool[solverBodyIdB];
  732. ///avoid collision response between two static objects
  733. if (solverBodyA->m_invMass.isZero() && solverBodyB->m_invMass.isZero())
  734. return;
  735. int rollingFriction=1;
  736. int numContacts = getNumContacts(manifold);
  737. for (int j=0;j<numContacts;j++)
  738. {
  739. b3ContactPoint cp;
  740. getContactPoint(manifold,j,cp);
  741. if (cp.getDistance() <= getContactProcessingThreshold(manifold))
  742. {
  743. b3Vector3 rel_pos1;
  744. b3Vector3 rel_pos2;
  745. b3Scalar relaxation;
  746. b3Scalar rel_vel;
  747. b3Vector3 vel;
  748. int frictionIndex = m_tmpSolverContactConstraintPool.size();
  749. b3SolverConstraint& solverConstraint = m_tmpSolverContactConstraintPool.expandNonInitializing();
  750. // b3RigidBody* rb0 = b3RigidBody::upcast(colObj0);
  751. // b3RigidBody* rb1 = b3RigidBody::upcast(colObj1);
  752. solverConstraint.m_solverBodyIdA = solverBodyIdA;
  753. solverConstraint.m_solverBodyIdB = solverBodyIdB;
  754. solverConstraint.m_originalContactPoint = &cp;
  755. setupContactConstraint(bodies,inertias,solverConstraint, solverBodyIdA, solverBodyIdB, cp, infoGlobal, vel, rel_vel, relaxation, rel_pos1, rel_pos2);
  756. // const b3Vector3& pos1 = cp.getPositionWorldOnA();
  757. // const b3Vector3& pos2 = cp.getPositionWorldOnB();
  758. /////setup the friction constraints
  759. solverConstraint.m_frictionIndex = m_tmpSolverContactFrictionConstraintPool.size();
  760. b3Vector3 angVelA,angVelB;
  761. solverBodyA->getAngularVelocity(angVelA);
  762. solverBodyB->getAngularVelocity(angVelB);
  763. b3Vector3 relAngVel = angVelB-angVelA;
  764. if ((cp.m_combinedRollingFriction>0.f) && (rollingFriction>0))
  765. {
  766. //only a single rollingFriction per manifold
  767. rollingFriction--;
  768. if (relAngVel.length()>infoGlobal.m_singleAxisRollingFrictionThreshold)
  769. {
  770. relAngVel.normalize();
  771. if (relAngVel.length()>0.001)
  772. addRollingFrictionConstraint(bodies,inertias,relAngVel,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation);
  773. } else
  774. {
  775. addRollingFrictionConstraint(bodies,inertias,cp.m_normalWorldOnB,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation);
  776. b3Vector3 axis0,axis1;
  777. b3PlaneSpace1(cp.m_normalWorldOnB,axis0,axis1);
  778. if (axis0.length()>0.001)
  779. addRollingFrictionConstraint(bodies,inertias,axis0,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation);
  780. if (axis1.length()>0.001)
  781. addRollingFrictionConstraint(bodies,inertias,axis1,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation);
  782. }
  783. }
  784. ///Bullet has several options to set the friction directions
  785. ///By default, each contact has only a single friction direction that is recomputed automatically very frame
  786. ///based on the relative linear velocity.
  787. ///If the relative velocity it zero, it will automatically compute a friction direction.
  788. ///You can also enable two friction directions, using the B3_SOLVER_USE_2_FRICTION_DIRECTIONS.
  789. ///In that case, the second friction direction will be orthogonal to both contact normal and first friction direction.
  790. ///
  791. ///If you choose B3_SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION, then the friction will be independent from the relative projected velocity.
  792. ///
  793. ///The user can manually override the friction directions for certain contacts using a contact callback,
  794. ///and set the cp.m_lateralFrictionInitialized to true
  795. ///In that case, you can set the target relative motion in each friction direction (cp.m_contactMotion1 and cp.m_contactMotion2)
  796. ///this will give a conveyor belt effect
  797. ///
  798. if (!(infoGlobal.m_solverMode & B3_SOLVER_ENABLE_FRICTION_DIRECTION_CACHING) || !cp.m_lateralFrictionInitialized)
  799. {
  800. cp.m_lateralFrictionDir1 = vel - cp.m_normalWorldOnB * rel_vel;
  801. b3Scalar lat_rel_vel = cp.m_lateralFrictionDir1.length2();
  802. if (!(infoGlobal.m_solverMode & B3_SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION) && lat_rel_vel > B3_EPSILON)
  803. {
  804. cp.m_lateralFrictionDir1 *= 1.f/b3Sqrt(lat_rel_vel);
  805. if((infoGlobal.m_solverMode & B3_SOLVER_USE_2_FRICTION_DIRECTIONS))
  806. {
  807. cp.m_lateralFrictionDir2 = cp.m_lateralFrictionDir1.cross(cp.m_normalWorldOnB);
  808. cp.m_lateralFrictionDir2.normalize();//??
  809. addFrictionConstraint(bodies,inertias,cp.m_lateralFrictionDir2,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation);
  810. }
  811. addFrictionConstraint(bodies,inertias,cp.m_lateralFrictionDir1,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation);
  812. } else
  813. {
  814. b3PlaneSpace1(cp.m_normalWorldOnB,cp.m_lateralFrictionDir1,cp.m_lateralFrictionDir2);
  815. if ((infoGlobal.m_solverMode & B3_SOLVER_USE_2_FRICTION_DIRECTIONS))
  816. {
  817. addFrictionConstraint(bodies,inertias,cp.m_lateralFrictionDir2,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation);
  818. }
  819. addFrictionConstraint(bodies,inertias,cp.m_lateralFrictionDir1,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation);
  820. if ((infoGlobal.m_solverMode & B3_SOLVER_USE_2_FRICTION_DIRECTIONS) && (infoGlobal.m_solverMode & B3_SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION))
  821. {
  822. cp.m_lateralFrictionInitialized = true;
  823. }
  824. }
  825. } else
  826. {
  827. addFrictionConstraint(bodies,inertias,cp.m_lateralFrictionDir1,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation,cp.m_contactMotion1, cp.m_contactCFM1);
  828. if ((infoGlobal.m_solverMode & B3_SOLVER_USE_2_FRICTION_DIRECTIONS))
  829. addFrictionConstraint(bodies,inertias,cp.m_lateralFrictionDir2,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation, cp.m_contactMotion2, cp.m_contactCFM2);
  830. setFrictionConstraintImpulse( bodies,inertias,solverConstraint, solverBodyIdA, solverBodyIdB, cp, infoGlobal);
  831. }
  832. }
  833. }
  834. }
  835. b3Scalar b3PgsJacobiSolver::solveGroupCacheFriendlySetup(b3RigidBodyData* bodies, b3InertiaData* inertias, int numBodies, b3Contact4* manifoldPtr, int numManifolds,b3TypedConstraint** constraints,int numConstraints,const b3ContactSolverInfo& infoGlobal)
  836. {
  837. B3_PROFILE("solveGroupCacheFriendlySetup");
  838. m_maxOverrideNumSolverIterations = 0;
  839. m_tmpSolverBodyPool.resize(0);
  840. m_bodyCount.resize(0);
  841. m_bodyCount.resize(numBodies,0);
  842. m_bodyCountCheck.resize(0);
  843. m_bodyCountCheck.resize(numBodies,0);
  844. m_deltaLinearVelocities.resize(0);
  845. m_deltaLinearVelocities.resize(numBodies,b3MakeVector3(0,0,0));
  846. m_deltaAngularVelocities.resize(0);
  847. m_deltaAngularVelocities.resize(numBodies,b3MakeVector3(0,0,0));
  848. //int totalBodies = 0;
  849. for (int i=0;i<numConstraints;i++)
  850. {
  851. int bodyIndexA = constraints[i]->getRigidBodyA();
  852. int bodyIndexB = constraints[i]->getRigidBodyB();
  853. if (m_usePgs)
  854. {
  855. m_bodyCount[bodyIndexA]=-1;
  856. m_bodyCount[bodyIndexB]=-1;
  857. } else
  858. {
  859. //didn't implement joints with Jacobi version yet
  860. b3Assert(0);
  861. }
  862. }
  863. for (int i=0;i<numManifolds;i++)
  864. {
  865. int bodyIndexA = manifoldPtr[i].getBodyA();
  866. int bodyIndexB = manifoldPtr[i].getBodyB();
  867. if (m_usePgs)
  868. {
  869. m_bodyCount[bodyIndexA]=-1;
  870. m_bodyCount[bodyIndexB]=-1;
  871. } else
  872. {
  873. if (bodies[bodyIndexA].m_invMass)
  874. {
  875. //m_bodyCount[bodyIndexA]+=manifoldPtr[i].getNPoints();
  876. m_bodyCount[bodyIndexA]++;
  877. }
  878. else
  879. m_bodyCount[bodyIndexA]=-1;
  880. if (bodies[bodyIndexB].m_invMass)
  881. // m_bodyCount[bodyIndexB]+=manifoldPtr[i].getNPoints();
  882. m_bodyCount[bodyIndexB]++;
  883. else
  884. m_bodyCount[bodyIndexB]=-1;
  885. }
  886. }
  887. if (1)
  888. {
  889. int j;
  890. for (j=0;j<numConstraints;j++)
  891. {
  892. b3TypedConstraint* constraint = constraints[j];
  893. constraint->internalSetAppliedImpulse(0.0f);
  894. }
  895. }
  896. //b3RigidBody* rb0=0,*rb1=0;
  897. //if (1)
  898. {
  899. {
  900. int totalNumRows = 0;
  901. int i;
  902. m_tmpConstraintSizesPool.resizeNoInitialize(numConstraints);
  903. //calculate the total number of contraint rows
  904. for (i=0;i<numConstraints;i++)
  905. {
  906. b3TypedConstraint::b3ConstraintInfo1& info1 = m_tmpConstraintSizesPool[i];
  907. b3JointFeedback* fb = constraints[i]->getJointFeedback();
  908. if (fb)
  909. {
  910. fb->m_appliedForceBodyA.setZero();
  911. fb->m_appliedTorqueBodyA.setZero();
  912. fb->m_appliedForceBodyB.setZero();
  913. fb->m_appliedTorqueBodyB.setZero();
  914. }
  915. if (constraints[i]->isEnabled())
  916. {
  917. }
  918. if (constraints[i]->isEnabled())
  919. {
  920. constraints[i]->getInfo1(&info1,bodies);
  921. } else
  922. {
  923. info1.m_numConstraintRows = 0;
  924. info1.nub = 0;
  925. }
  926. totalNumRows += info1.m_numConstraintRows;
  927. }
  928. m_tmpSolverNonContactConstraintPool.resizeNoInitialize(totalNumRows);
  929. #ifndef DISABLE_JOINTS
  930. ///setup the b3SolverConstraints
  931. int currentRow = 0;
  932. for (i=0;i<numConstraints;i++)
  933. {
  934. const b3TypedConstraint::b3ConstraintInfo1& info1 = m_tmpConstraintSizesPool[i];
  935. if (info1.m_numConstraintRows)
  936. {
  937. b3Assert(currentRow<totalNumRows);
  938. b3SolverConstraint* currentConstraintRow = &m_tmpSolverNonContactConstraintPool[currentRow];
  939. b3TypedConstraint* constraint = constraints[i];
  940. b3RigidBodyData& rbA = bodies[ constraint->getRigidBodyA()];
  941. //b3RigidBody& rbA = constraint->getRigidBodyA();
  942. // b3RigidBody& rbB = constraint->getRigidBodyB();
  943. b3RigidBodyData& rbB = bodies[ constraint->getRigidBodyB()];
  944. int solverBodyIdA = getOrInitSolverBody(constraint->getRigidBodyA(),bodies,inertias);
  945. int solverBodyIdB = getOrInitSolverBody(constraint->getRigidBodyB(),bodies,inertias);
  946. b3SolverBody* bodyAPtr = &m_tmpSolverBodyPool[solverBodyIdA];
  947. b3SolverBody* bodyBPtr = &m_tmpSolverBodyPool[solverBodyIdB];
  948. int overrideNumSolverIterations = constraint->getOverrideNumSolverIterations() > 0 ? constraint->getOverrideNumSolverIterations() : infoGlobal.m_numIterations;
  949. if (overrideNumSolverIterations>m_maxOverrideNumSolverIterations)
  950. m_maxOverrideNumSolverIterations = overrideNumSolverIterations;
  951. int j;
  952. for ( j=0;j<info1.m_numConstraintRows;j++)
  953. {
  954. memset(&currentConstraintRow[j],0,sizeof(b3SolverConstraint));
  955. currentConstraintRow[j].m_lowerLimit = -B3_INFINITY;
  956. currentConstraintRow[j].m_upperLimit = B3_INFINITY;
  957. currentConstraintRow[j].m_appliedImpulse = 0.f;
  958. currentConstraintRow[j].m_appliedPushImpulse = 0.f;
  959. currentConstraintRow[j].m_solverBodyIdA = solverBodyIdA;
  960. currentConstraintRow[j].m_solverBodyIdB = solverBodyIdB;
  961. currentConstraintRow[j].m_overrideNumSolverIterations = overrideNumSolverIterations;
  962. }
  963. bodyAPtr->internalGetDeltaLinearVelocity().setValue(0.f,0.f,0.f);
  964. bodyAPtr->internalGetDeltaAngularVelocity().setValue(0.f,0.f,0.f);
  965. bodyAPtr->internalGetPushVelocity().setValue(0.f,0.f,0.f);
  966. bodyAPtr->internalGetTurnVelocity().setValue(0.f,0.f,0.f);
  967. bodyBPtr->internalGetDeltaLinearVelocity().setValue(0.f,0.f,0.f);
  968. bodyBPtr->internalGetDeltaAngularVelocity().setValue(0.f,0.f,0.f);
  969. bodyBPtr->internalGetPushVelocity().setValue(0.f,0.f,0.f);
  970. bodyBPtr->internalGetTurnVelocity().setValue(0.f,0.f,0.f);
  971. b3TypedConstraint::b3ConstraintInfo2 info2;
  972. info2.fps = 1.f/infoGlobal.m_timeStep;
  973. info2.erp = infoGlobal.m_erp;
  974. info2.m_J1linearAxis = currentConstraintRow->m_contactNormal;
  975. info2.m_J1angularAxis = currentConstraintRow->m_relpos1CrossNormal;
  976. info2.m_J2linearAxis = 0;
  977. info2.m_J2angularAxis = currentConstraintRow->m_relpos2CrossNormal;
  978. info2.rowskip = sizeof(b3SolverConstraint)/sizeof(b3Scalar);//check this
  979. ///the size of b3SolverConstraint needs be a multiple of b3Scalar
  980. b3Assert(info2.rowskip*sizeof(b3Scalar)== sizeof(b3SolverConstraint));
  981. info2.m_constraintError = &currentConstraintRow->m_rhs;
  982. currentConstraintRow->m_cfm = infoGlobal.m_globalCfm;
  983. info2.m_damping = infoGlobal.m_damping;
  984. info2.cfm = &currentConstraintRow->m_cfm;
  985. info2.m_lowerLimit = &currentConstraintRow->m_lowerLimit;
  986. info2.m_upperLimit = &currentConstraintRow->m_upperLimit;
  987. info2.m_numIterations = infoGlobal.m_numIterations;
  988. constraints[i]->getInfo2(&info2,bodies);
  989. ///finalize the constraint setup
  990. for ( j=0;j<info1.m_numConstraintRows;j++)
  991. {
  992. b3SolverConstraint& solverConstraint = currentConstraintRow[j];
  993. if (solverConstraint.m_upperLimit>=constraints[i]->getBreakingImpulseThreshold())
  994. {
  995. solverConstraint.m_upperLimit = constraints[i]->getBreakingImpulseThreshold();
  996. }
  997. if (solverConstraint.m_lowerLimit<=-constraints[i]->getBreakingImpulseThreshold())
  998. {
  999. solverConstraint.m_lowerLimit = -constraints[i]->getBreakingImpulseThreshold();
  1000. }
  1001. solverConstraint.m_originalContactPoint = constraint;
  1002. b3Matrix3x3& invInertiaWorldA= inertias[constraint->getRigidBodyA()].m_invInertiaWorld;
  1003. {
  1004. //b3Vector3 angularFactorA(1,1,1);
  1005. const b3Vector3& ftorqueAxis1 = solverConstraint.m_relpos1CrossNormal;
  1006. solverConstraint.m_angularComponentA = invInertiaWorldA*ftorqueAxis1;//*angularFactorA;
  1007. }
  1008. b3Matrix3x3& invInertiaWorldB= inertias[constraint->getRigidBodyB()].m_invInertiaWorld;
  1009. {
  1010. const b3Vector3& ftorqueAxis2 = solverConstraint.m_relpos2CrossNormal;
  1011. solverConstraint.m_angularComponentB = invInertiaWorldB*ftorqueAxis2;//*constraint->getRigidBodyB().getAngularFactor();
  1012. }
  1013. {
  1014. //it is ok to use solverConstraint.m_contactNormal instead of -solverConstraint.m_contactNormal
  1015. //because it gets multiplied iMJlB
  1016. b3Vector3 iMJlA = solverConstraint.m_contactNormal*rbA.m_invMass;
  1017. b3Vector3 iMJaA = invInertiaWorldA*solverConstraint.m_relpos1CrossNormal;
  1018. b3Vector3 iMJlB = solverConstraint.m_contactNormal*rbB.m_invMass;//sign of normal?
  1019. b3Vector3 iMJaB = invInertiaWorldB*solverConstraint.m_relpos2CrossNormal;
  1020. b3Scalar sum = iMJlA.dot(solverConstraint.m_contactNormal);
  1021. sum += iMJaA.dot(solverConstraint.m_relpos1CrossNormal);
  1022. sum += iMJlB.dot(solverConstraint.m_contactNormal);
  1023. sum += iMJaB.dot(solverConstraint.m_relpos2CrossNormal);
  1024. b3Scalar fsum = b3Fabs(sum);
  1025. b3Assert(fsum > B3_EPSILON);
  1026. solverConstraint.m_jacDiagABInv = fsum>B3_EPSILON?b3Scalar(1.)/sum : 0.f;
  1027. }
  1028. ///fix rhs
  1029. ///todo: add force/torque accelerators
  1030. {
  1031. b3Scalar rel_vel;
  1032. b3Scalar vel1Dotn = solverConstraint.m_contactNormal.dot(rbA.m_linVel) + solverConstraint.m_relpos1CrossNormal.dot(rbA.m_angVel);
  1033. b3Scalar vel2Dotn = -solverConstraint.m_contactNormal.dot(rbB.m_linVel) + solverConstraint.m_relpos2CrossNormal.dot(rbB.m_angVel);
  1034. rel_vel = vel1Dotn+vel2Dotn;
  1035. b3Scalar restitution = 0.f;
  1036. b3Scalar positionalError = solverConstraint.m_rhs;//already filled in by getConstraintInfo2
  1037. b3Scalar velocityError = restitution - rel_vel * info2.m_damping;
  1038. b3Scalar penetrationImpulse = positionalError*solverConstraint.m_jacDiagABInv;
  1039. b3Scalar velocityImpulse = velocityError *solverConstraint.m_jacDiagABInv;
  1040. solverConstraint.m_rhs = penetrationImpulse+velocityImpulse;
  1041. solverConstraint.m_appliedImpulse = 0.f;
  1042. }
  1043. }
  1044. }
  1045. currentRow+=m_tmpConstraintSizesPool[i].m_numConstraintRows;
  1046. }
  1047. #endif //DISABLE_JOINTS
  1048. }
  1049. {
  1050. int i;
  1051. for (i=0;i<numManifolds;i++)
  1052. {
  1053. b3Contact4& manifold = manifoldPtr[i];
  1054. convertContact(bodies,inertias,&manifold,infoGlobal);
  1055. }
  1056. }
  1057. }
  1058. // b3ContactSolverInfo info = infoGlobal;
  1059. int numNonContactPool = m_tmpSolverNonContactConstraintPool.size();
  1060. int numConstraintPool = m_tmpSolverContactConstraintPool.size();
  1061. int numFrictionPool = m_tmpSolverContactFrictionConstraintPool.size();
  1062. ///@todo: use stack allocator for such temporarily memory, same for solver bodies/constraints
  1063. m_orderNonContactConstraintPool.resizeNoInitialize(numNonContactPool);
  1064. if ((infoGlobal.m_solverMode & B3_SOLVER_USE_2_FRICTION_DIRECTIONS))
  1065. m_orderTmpConstraintPool.resizeNoInitialize(numConstraintPool*2);
  1066. else
  1067. m_orderTmpConstraintPool.resizeNoInitialize(numConstraintPool);
  1068. m_orderFrictionConstraintPool.resizeNoInitialize(numFrictionPool);
  1069. {
  1070. int i;
  1071. for (i=0;i<numNonContactPool;i++)
  1072. {
  1073. m_orderNonContactConstraintPool[i] = i;
  1074. }
  1075. for (i=0;i<numConstraintPool;i++)
  1076. {
  1077. m_orderTmpConstraintPool[i] = i;
  1078. }
  1079. for (i=0;i<numFrictionPool;i++)
  1080. {
  1081. m_orderFrictionConstraintPool[i] = i;
  1082. }
  1083. }
  1084. return 0.f;
  1085. }
  1086. b3Scalar b3PgsJacobiSolver::solveSingleIteration(int iteration,b3TypedConstraint** constraints,int numConstraints,const b3ContactSolverInfo& infoGlobal)
  1087. {
  1088. int numNonContactPool = m_tmpSolverNonContactConstraintPool.size();
  1089. int numConstraintPool = m_tmpSolverContactConstraintPool.size();
  1090. int numFrictionPool = m_tmpSolverContactFrictionConstraintPool.size();
  1091. if (infoGlobal.m_solverMode & B3_SOLVER_RANDMIZE_ORDER)
  1092. {
  1093. if (1) // uncomment this for a bit less random ((iteration & 7) == 0)
  1094. {
  1095. for (int j=0; j<numNonContactPool; ++j) {
  1096. int tmp = m_orderNonContactConstraintPool[j];
  1097. int swapi = b3RandInt2(j+1);
  1098. m_orderNonContactConstraintPool[j] = m_orderNonContactConstraintPool[swapi];
  1099. m_orderNonContactConstraintPool[swapi] = tmp;
  1100. }
  1101. //contact/friction constraints are not solved more than
  1102. if (iteration< infoGlobal.m_numIterations)
  1103. {
  1104. for (int j=0; j<numConstraintPool; ++j) {
  1105. int tmp = m_orderTmpConstraintPool[j];
  1106. int swapi = b3RandInt2(j+1);
  1107. m_orderTmpConstraintPool[j] = m_orderTmpConstraintPool[swapi];
  1108. m_orderTmpConstraintPool[swapi] = tmp;
  1109. }
  1110. for (int j=0; j<numFrictionPool; ++j) {
  1111. int tmp = m_orderFrictionConstraintPool[j];
  1112. int swapi = b3RandInt2(j+1);
  1113. m_orderFrictionConstraintPool[j] = m_orderFrictionConstraintPool[swapi];
  1114. m_orderFrictionConstraintPool[swapi] = tmp;
  1115. }
  1116. }
  1117. }
  1118. }
  1119. if (infoGlobal.m_solverMode & B3_SOLVER_SIMD)
  1120. {
  1121. ///solve all joint constraints, using SIMD, if available
  1122. for (int j=0;j<m_tmpSolverNonContactConstraintPool.size();j++)
  1123. {
  1124. b3SolverConstraint& constraint = m_tmpSolverNonContactConstraintPool[m_orderNonContactConstraintPool[j]];
  1125. if (iteration < constraint.m_overrideNumSolverIterations)
  1126. resolveSingleConstraintRowGenericSIMD(m_tmpSolverBodyPool[constraint.m_solverBodyIdA],m_tmpSolverBodyPool[constraint.m_solverBodyIdB],constraint);
  1127. }
  1128. if (iteration< infoGlobal.m_numIterations)
  1129. {
  1130. ///solve all contact constraints using SIMD, if available
  1131. if (infoGlobal.m_solverMode & B3_SOLVER_INTERLEAVE_CONTACT_AND_FRICTION_CONSTRAINTS)
  1132. {
  1133. int numPoolConstraints = m_tmpSolverContactConstraintPool.size();
  1134. int multiplier = (infoGlobal.m_solverMode & B3_SOLVER_USE_2_FRICTION_DIRECTIONS)? 2 : 1;
  1135. for (int c=0;c<numPoolConstraints;c++)
  1136. {
  1137. b3Scalar totalImpulse =0;
  1138. {
  1139. const b3SolverConstraint& solveManifold = m_tmpSolverContactConstraintPool[m_orderTmpConstraintPool[c]];
  1140. resolveSingleConstraintRowLowerLimitSIMD(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA],m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold);
  1141. totalImpulse = solveManifold.m_appliedImpulse;
  1142. }
  1143. bool applyFriction = true;
  1144. if (applyFriction)
  1145. {
  1146. {
  1147. b3SolverConstraint& solveManifold = m_tmpSolverContactFrictionConstraintPool[m_orderFrictionConstraintPool[c*multiplier]];
  1148. if (totalImpulse>b3Scalar(0))
  1149. {
  1150. solveManifold.m_lowerLimit = -(solveManifold.m_friction*totalImpulse);
  1151. solveManifold.m_upperLimit = solveManifold.m_friction*totalImpulse;
  1152. resolveSingleConstraintRowGenericSIMD(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA],m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold);
  1153. }
  1154. }
  1155. if (infoGlobal.m_solverMode & B3_SOLVER_USE_2_FRICTION_DIRECTIONS)
  1156. {
  1157. b3SolverConstraint& solveManifold = m_tmpSolverContactFrictionConstraintPool[m_orderFrictionConstraintPool[c*multiplier+1]];
  1158. if (totalImpulse>b3Scalar(0))
  1159. {
  1160. solveManifold.m_lowerLimit = -(solveManifold.m_friction*totalImpulse);
  1161. solveManifold.m_upperLimit = solveManifold.m_friction*totalImpulse;
  1162. resolveSingleConstraintRowGenericSIMD(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA],m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold);
  1163. }
  1164. }
  1165. }
  1166. }
  1167. }
  1168. else//B3_SOLVER_INTERLEAVE_CONTACT_AND_FRICTION_CONSTRAINTS
  1169. {
  1170. //solve the friction constraints after all contact constraints, don't interleave them
  1171. int numPoolConstraints = m_tmpSolverContactConstraintPool.size();
  1172. int j;
  1173. for (j=0;j<numPoolConstraints;j++)
  1174. {
  1175. const b3SolverConstraint& solveManifold = m_tmpSolverContactConstraintPool[m_orderTmpConstraintPool[j]];
  1176. resolveSingleConstraintRowLowerLimitSIMD(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA],m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold);
  1177. }
  1178. if (!m_usePgs)
  1179. averageVelocities();
  1180. ///solve all friction constraints, using SIMD, if available
  1181. int numFrictionPoolConstraints = m_tmpSolverContactFrictionConstraintPool.size();
  1182. for (j=0;j<numFrictionPoolConstraints;j++)
  1183. {
  1184. b3SolverConstraint& solveManifold = m_tmpSolverContactFrictionConstraintPool[m_orderFrictionConstraintPool[j]];
  1185. b3Scalar totalImpulse = m_tmpSolverContactConstraintPool[solveManifold.m_frictionIndex].m_appliedImpulse;
  1186. if (totalImpulse>b3Scalar(0))
  1187. {
  1188. solveManifold.m_lowerLimit = -(solveManifold.m_friction*totalImpulse);
  1189. solveManifold.m_upperLimit = solveManifold.m_friction*totalImpulse;
  1190. resolveSingleConstraintRowGenericSIMD(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA],m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold);
  1191. }
  1192. }
  1193. int numRollingFrictionPoolConstraints = m_tmpSolverContactRollingFrictionConstraintPool.size();
  1194. for (j=0;j<numRollingFrictionPoolConstraints;j++)
  1195. {
  1196. b3SolverConstraint& rollingFrictionConstraint = m_tmpSolverContactRollingFrictionConstraintPool[j];
  1197. b3Scalar totalImpulse = m_tmpSolverContactConstraintPool[rollingFrictionConstraint.m_frictionIndex].m_appliedImpulse;
  1198. if (totalImpulse>b3Scalar(0))
  1199. {
  1200. b3Scalar rollingFrictionMagnitude = rollingFrictionConstraint.m_friction*totalImpulse;
  1201. if (rollingFrictionMagnitude>rollingFrictionConstraint.m_friction)
  1202. rollingFrictionMagnitude = rollingFrictionConstraint.m_friction;
  1203. rollingFrictionConstraint.m_lowerLimit = -rollingFrictionMagnitude;
  1204. rollingFrictionConstraint.m_upperLimit = rollingFrictionMagnitude;
  1205. resolveSingleConstraintRowGenericSIMD(m_tmpSolverBodyPool[rollingFrictionConstraint.m_solverBodyIdA],m_tmpSolverBodyPool[rollingFrictionConstraint.m_solverBodyIdB],rollingFrictionConstraint);
  1206. }
  1207. }
  1208. }
  1209. }
  1210. } else
  1211. {
  1212. //non-SIMD version
  1213. ///solve all joint constraints
  1214. for (int j=0;j<m_tmpSolverNonContactConstraintPool.size();j++)
  1215. {
  1216. b3SolverConstraint& constraint = m_tmpSolverNonContactConstraintPool[m_orderNonContactConstraintPool[j]];
  1217. if (iteration < constraint.m_overrideNumSolverIterations)
  1218. resolveSingleConstraintRowGeneric(m_tmpSolverBodyPool[constraint.m_solverBodyIdA],m_tmpSolverBodyPool[constraint.m_solverBodyIdB],constraint);
  1219. }
  1220. if (iteration< infoGlobal.m_numIterations)
  1221. {
  1222. ///solve all contact constraints
  1223. int numPoolConstraints = m_tmpSolverContactConstraintPool.size();
  1224. for (int j=0;j<numPoolConstraints;j++)
  1225. {
  1226. const b3SolverConstraint& solveManifold = m_tmpSolverContactConstraintPool[m_orderTmpConstraintPool[j]];
  1227. resolveSingleConstraintRowLowerLimit(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA],m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold);
  1228. }
  1229. ///solve all friction constraints
  1230. int numFrictionPoolConstraints = m_tmpSolverContactFrictionConstraintPool.size();
  1231. for (int j=0;j<numFrictionPoolConstraints;j++)
  1232. {
  1233. b3SolverConstraint& solveManifold = m_tmpSolverContactFrictionConstraintPool[m_orderFrictionConstraintPool[j]];
  1234. b3Scalar totalImpulse = m_tmpSolverContactConstraintPool[solveManifold.m_frictionIndex].m_appliedImpulse;
  1235. if (totalImpulse>b3Scalar(0))
  1236. {
  1237. solveManifold.m_lowerLimit = -(solveManifold.m_friction*totalImpulse);
  1238. solveManifold.m_upperLimit = solveManifold.m_friction*totalImpulse;
  1239. resolveSingleConstraintRowGeneric(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA],m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold);
  1240. }
  1241. }
  1242. int numRollingFrictionPoolConstraints = m_tmpSolverContactRollingFrictionConstraintPool.size();
  1243. for (int j=0;j<numRollingFrictionPoolConstraints;j++)
  1244. {
  1245. b3SolverConstraint& rollingFrictionConstraint = m_tmpSolverContactRollingFrictionConstraintPool[j];
  1246. b3Scalar totalImpulse = m_tmpSolverContactConstraintPool[rollingFrictionConstraint.m_frictionIndex].m_appliedImpulse;
  1247. if (totalImpulse>b3Scalar(0))
  1248. {
  1249. b3Scalar rollingFrictionMagnitude = rollingFrictionConstraint.m_friction*totalImpulse;
  1250. if (rollingFrictionMagnitude>rollingFrictionConstraint.m_friction)
  1251. rollingFrictionMagnitude = rollingFrictionConstraint.m_friction;
  1252. rollingFrictionConstraint.m_lowerLimit = -rollingFrictionMagnitude;
  1253. rollingFrictionConstraint.m_upperLimit = rollingFrictionMagnitude;
  1254. resolveSingleConstraintRowGeneric(m_tmpSolverBodyPool[rollingFrictionConstraint.m_solverBodyIdA],m_tmpSolverBodyPool[rollingFrictionConstraint.m_solverBodyIdB],rollingFrictionConstraint);
  1255. }
  1256. }
  1257. }
  1258. }
  1259. return 0.f;
  1260. }
  1261. void b3PgsJacobiSolver::solveGroupCacheFriendlySplitImpulseIterations(b3TypedConstraint** constraints,int numConstraints,const b3ContactSolverInfo& infoGlobal)
  1262. {
  1263. int iteration;
  1264. if (infoGlobal.m_splitImpulse)
  1265. {
  1266. if (infoGlobal.m_solverMode & B3_SOLVER_SIMD)
  1267. {
  1268. for ( iteration = 0;iteration<infoGlobal.m_numIterations;iteration++)
  1269. {
  1270. {
  1271. int numPoolConstraints = m_tmpSolverContactConstraintPool.size();
  1272. int j;
  1273. for (j=0;j<numPoolConstraints;j++)
  1274. {
  1275. const b3SolverConstraint& solveManifold = m_tmpSolverContactConstraintPool[m_orderTmpConstraintPool[j]];
  1276. resolveSplitPenetrationSIMD(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA],m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold);
  1277. }
  1278. }
  1279. }
  1280. }
  1281. else
  1282. {
  1283. for ( iteration = 0;iteration<infoGlobal.m_numIterations;iteration++)
  1284. {
  1285. {
  1286. int numPoolConstraints = m_tmpSolverContactConstraintPool.size();
  1287. int j;
  1288. for (j=0;j<numPoolConstraints;j++)
  1289. {
  1290. const b3SolverConstraint& solveManifold = m_tmpSolverContactConstraintPool[m_orderTmpConstraintPool[j]];
  1291. resolveSplitPenetrationImpulseCacheFriendly(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA],m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold);
  1292. }
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. b3Scalar b3PgsJacobiSolver::solveGroupCacheFriendlyIterations(b3TypedConstraint** constraints,int numConstraints,const b3ContactSolverInfo& infoGlobal)
  1299. {
  1300. B3_PROFILE("solveGroupCacheFriendlyIterations");
  1301. {
  1302. ///this is a special step to resolve penetrations (just for contacts)
  1303. solveGroupCacheFriendlySplitImpulseIterations(constraints,numConstraints,infoGlobal);
  1304. int maxIterations = m_maxOverrideNumSolverIterations > infoGlobal.m_numIterations? m_maxOverrideNumSolverIterations : infoGlobal.m_numIterations;
  1305. for ( int iteration = 0 ; iteration< maxIterations ; iteration++)
  1306. //for ( int iteration = maxIterations-1 ; iteration >= 0;iteration--)
  1307. {
  1308. solveSingleIteration(iteration, constraints,numConstraints,infoGlobal);
  1309. if (!m_usePgs)
  1310. {
  1311. averageVelocities();
  1312. }
  1313. }
  1314. }
  1315. return 0.f;
  1316. }
  1317. void b3PgsJacobiSolver::averageVelocities()
  1318. {
  1319. B3_PROFILE("averaging");
  1320. //average the velocities
  1321. int numBodies = m_bodyCount.size();
  1322. m_deltaLinearVelocities.resize(0);
  1323. m_deltaLinearVelocities.resize(numBodies,b3MakeVector3(0,0,0));
  1324. m_deltaAngularVelocities.resize(0);
  1325. m_deltaAngularVelocities.resize(numBodies,b3MakeVector3(0,0,0));
  1326. for (int i=0;i<m_tmpSolverBodyPool.size();i++)
  1327. {
  1328. if (!m_tmpSolverBodyPool[i].m_invMass.isZero())
  1329. {
  1330. int orgBodyIndex = m_tmpSolverBodyPool[i].m_originalBodyIndex;
  1331. m_deltaLinearVelocities[orgBodyIndex]+=m_tmpSolverBodyPool[i].getDeltaLinearVelocity();
  1332. m_deltaAngularVelocities[orgBodyIndex]+=m_tmpSolverBodyPool[i].getDeltaAngularVelocity();
  1333. }
  1334. }
  1335. for (int i=0;i<m_tmpSolverBodyPool.size();i++)
  1336. {
  1337. int orgBodyIndex = m_tmpSolverBodyPool[i].m_originalBodyIndex;
  1338. if (!m_tmpSolverBodyPool[i].m_invMass.isZero())
  1339. {
  1340. b3Assert(m_bodyCount[orgBodyIndex] == m_bodyCountCheck[orgBodyIndex]);
  1341. b3Scalar factor = 1.f/b3Scalar(m_bodyCount[orgBodyIndex]);
  1342. m_tmpSolverBodyPool[i].m_deltaLinearVelocity = m_deltaLinearVelocities[orgBodyIndex]*factor;
  1343. m_tmpSolverBodyPool[i].m_deltaAngularVelocity = m_deltaAngularVelocities[orgBodyIndex]*factor;
  1344. }
  1345. }
  1346. }
  1347. b3Scalar b3PgsJacobiSolver::solveGroupCacheFriendlyFinish(b3RigidBodyData* bodies,b3InertiaData* inertias,int numBodies,const b3ContactSolverInfo& infoGlobal)
  1348. {
  1349. B3_PROFILE("solveGroupCacheFriendlyFinish");
  1350. int numPoolConstraints = m_tmpSolverContactConstraintPool.size();
  1351. int i,j;
  1352. if (infoGlobal.m_solverMode & B3_SOLVER_USE_WARMSTARTING)
  1353. {
  1354. for (j=0;j<numPoolConstraints;j++)
  1355. {
  1356. const b3SolverConstraint& solveManifold = m_tmpSolverContactConstraintPool[j];
  1357. b3ContactPoint* pt = (b3ContactPoint*) solveManifold.m_originalContactPoint;
  1358. b3Assert(pt);
  1359. pt->m_appliedImpulse = solveManifold.m_appliedImpulse;
  1360. // float f = m_tmpSolverContactFrictionConstraintPool[solveManifold.m_frictionIndex].m_appliedImpulse;
  1361. // printf("pt->m_appliedImpulseLateral1 = %f\n", f);
  1362. pt->m_appliedImpulseLateral1 = m_tmpSolverContactFrictionConstraintPool[solveManifold.m_frictionIndex].m_appliedImpulse;
  1363. //printf("pt->m_appliedImpulseLateral1 = %f\n", pt->m_appliedImpulseLateral1);
  1364. if ((infoGlobal.m_solverMode & B3_SOLVER_USE_2_FRICTION_DIRECTIONS))
  1365. {
  1366. pt->m_appliedImpulseLateral2 = m_tmpSolverContactFrictionConstraintPool[solveManifold.m_frictionIndex+1].m_appliedImpulse;
  1367. }
  1368. //do a callback here?
  1369. }
  1370. }
  1371. numPoolConstraints = m_tmpSolverNonContactConstraintPool.size();
  1372. for (j=0;j<numPoolConstraints;j++)
  1373. {
  1374. const b3SolverConstraint& solverConstr = m_tmpSolverNonContactConstraintPool[j];
  1375. b3TypedConstraint* constr = (b3TypedConstraint*)solverConstr.m_originalContactPoint;
  1376. b3JointFeedback* fb = constr->getJointFeedback();
  1377. if (fb)
  1378. {
  1379. b3SolverBody* bodyA = &m_tmpSolverBodyPool[solverConstr.m_solverBodyIdA];
  1380. b3SolverBody* bodyB = &m_tmpSolverBodyPool[solverConstr.m_solverBodyIdB];
  1381. fb->m_appliedForceBodyA += solverConstr.m_contactNormal*solverConstr.m_appliedImpulse*bodyA->m_linearFactor/infoGlobal.m_timeStep;
  1382. fb->m_appliedForceBodyB += -solverConstr.m_contactNormal*solverConstr.m_appliedImpulse*bodyB->m_linearFactor/infoGlobal.m_timeStep;
  1383. fb->m_appliedTorqueBodyA += solverConstr.m_relpos1CrossNormal* bodyA->m_angularFactor*solverConstr.m_appliedImpulse/infoGlobal.m_timeStep;
  1384. fb->m_appliedTorqueBodyB += -solverConstr.m_relpos1CrossNormal* bodyB->m_angularFactor*solverConstr.m_appliedImpulse/infoGlobal.m_timeStep;
  1385. }
  1386. constr->internalSetAppliedImpulse(solverConstr.m_appliedImpulse);
  1387. if (b3Fabs(solverConstr.m_appliedImpulse)>=constr->getBreakingImpulseThreshold())
  1388. {
  1389. constr->setEnabled(false);
  1390. }
  1391. }
  1392. {
  1393. B3_PROFILE("write back velocities and transforms");
  1394. for ( i=0;i<m_tmpSolverBodyPool.size();i++)
  1395. {
  1396. int bodyIndex = m_tmpSolverBodyPool[i].m_originalBodyIndex;
  1397. //b3Assert(i==bodyIndex);
  1398. b3RigidBodyData* body = &bodies[bodyIndex];
  1399. if (body->m_invMass)
  1400. {
  1401. if (infoGlobal.m_splitImpulse)
  1402. m_tmpSolverBodyPool[i].writebackVelocityAndTransform(infoGlobal.m_timeStep, infoGlobal.m_splitImpulseTurnErp);
  1403. else
  1404. m_tmpSolverBodyPool[i].writebackVelocity();
  1405. if (m_usePgs)
  1406. {
  1407. body->m_linVel = m_tmpSolverBodyPool[i].m_linearVelocity;
  1408. body->m_angVel = m_tmpSolverBodyPool[i].m_angularVelocity;
  1409. } else
  1410. {
  1411. b3Scalar factor = 1.f/b3Scalar(m_bodyCount[bodyIndex]);
  1412. b3Vector3 deltaLinVel = m_deltaLinearVelocities[bodyIndex]*factor;
  1413. b3Vector3 deltaAngVel = m_deltaAngularVelocities[bodyIndex]*factor;
  1414. //printf("body %d\n",bodyIndex);
  1415. //printf("deltaLinVel = %f,%f,%f\n",deltaLinVel.getX(),deltaLinVel.getY(),deltaLinVel.getZ());
  1416. //printf("deltaAngVel = %f,%f,%f\n",deltaAngVel.getX(),deltaAngVel.getY(),deltaAngVel.getZ());
  1417. body->m_linVel += deltaLinVel;
  1418. body->m_angVel += deltaAngVel;
  1419. }
  1420. if (infoGlobal.m_splitImpulse)
  1421. {
  1422. body->m_pos = m_tmpSolverBodyPool[i].m_worldTransform.getOrigin();
  1423. b3Quaternion orn;
  1424. orn = m_tmpSolverBodyPool[i].m_worldTransform.getRotation();
  1425. body->m_quat = orn;
  1426. }
  1427. }
  1428. }
  1429. }
  1430. m_tmpSolverContactConstraintPool.resizeNoInitialize(0);
  1431. m_tmpSolverNonContactConstraintPool.resizeNoInitialize(0);
  1432. m_tmpSolverContactFrictionConstraintPool.resizeNoInitialize(0);
  1433. m_tmpSolverContactRollingFrictionConstraintPool.resizeNoInitialize(0);
  1434. m_tmpSolverBodyPool.resizeNoInitialize(0);
  1435. return 0.f;
  1436. }
  1437. void b3PgsJacobiSolver::reset()
  1438. {
  1439. m_btSeed2 = 0;
  1440. }