btSequentialImpulseConstraintSolver.cpp 71 KB

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