btSequentialImpulseConstraintSolver.cpp 71 KB

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