/* Bullet Continuous Collision Detection and Physics Library Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 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. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ ///original version written by Erwin Coumans, October 2013 #include "btMLCPSolver.h" #include "LinearMath/btMatrixX.h" #include "LinearMath/btQuickprof.h" #include "btSolveProjectedGaussSeidel.h" btMLCPSolver::btMLCPSolver( btMLCPSolverInterface* solver) :m_solver(solver), m_fallback(0), m_cfm(0.000001)//0.0000001 { } btMLCPSolver::~btMLCPSolver() { } bool gUseMatrixMultiply = false; bool interleaveContactAndFriction = false; btScalar btMLCPSolver::solveGroupCacheFriendlySetup(btCollisionObject** bodies, int numBodiesUnUsed, btPersistentManifold** manifoldPtr, int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& infoGlobal,btIDebugDraw* debugDrawer) { btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup( bodies, numBodiesUnUsed, manifoldPtr, numManifolds,constraints,numConstraints,infoGlobal,debugDrawer); { BT_PROFILE("gather constraint data"); int numFrictionPerContact = m_tmpSolverContactConstraintPool.size()==m_tmpSolverContactFrictionConstraintPool.size()? 1 : 2; int numBodies = m_tmpSolverBodyPool.size(); m_allConstraintArray.resize(0); m_limitDependencies.resize(m_tmpSolverNonContactConstraintPool.size()+m_tmpSolverContactConstraintPool.size()+m_tmpSolverContactFrictionConstraintPool.size()); btAssert(m_limitDependencies.size() == m_tmpSolverNonContactConstraintPool.size()+m_tmpSolverContactConstraintPool.size()+m_tmpSolverContactFrictionConstraintPool.size()); // printf("m_limitDependencies.size() = %d\n",m_limitDependencies.size()); int dindex = 0; for (int i=0;i limitDependenciesCopy = m_limitDependencies; // printf("solve first LCP\n"); result = m_solver->solveMLCP(m_A, m_b, m_x, m_lo,m_hi, m_limitDependencies,infoGlobal.m_numIterations ); if (result) result = m_solver->solveMLCP(Acopy, m_bSplit, m_xSplit, m_lo,m_hi, limitDependenciesCopy,infoGlobal.m_numIterations ); } else { result = m_solver->solveMLCP(m_A, m_b, m_x, m_lo,m_hi, m_limitDependencies,infoGlobal.m_numIterations ); } return result; } struct btJointNode { int jointIndex; // pointer to enclosing dxJoint object int otherBodyIndex; // *other* body this joint is connected to int nextJointNodeIndex;//-1 for null int constraintRowIndex; }; void btMLCPSolver::createMLCPFast(const btContactSolverInfo& infoGlobal) { int numContactRows = interleaveContactAndFriction ? 3 : 1; int numConstraintRows = m_allConstraintArray.size(); int n = numConstraintRows; { BT_PROFILE("init b (rhs)"); m_b.resize(numConstraintRows); m_bSplit.resize(numConstraintRows); m_b.setZero(); m_bSplit.setZero(); for (int i=0;i=0) { m_lo[i] = -BT_INFINITY; m_hi[i] = BT_INFINITY; } else { m_lo[i] = m_allConstraintArray[i].m_lowerLimit; m_hi[i] = m_allConstraintArray[i].m_upperLimit; } } } // int m=m_allConstraintArray.size(); int numBodies = m_tmpSolverBodyPool.size(); btAlignedObjectArray bodyJointNodeArray; { BT_PROFILE("bodyJointNodeArray.resize"); bodyJointNodeArray.resize(numBodies,-1); } btAlignedObjectArray jointNodeArray; { BT_PROFILE("jointNodeArray.reserve"); jointNodeArray.reserve(2*m_allConstraintArray.size()); } static btMatrixXu J3; { BT_PROFILE("J3.resize"); J3.resize(2*m,8); } static btMatrixXu JinvM3; { BT_PROFILE("JinvM3.resize/setZero"); JinvM3.resize(2*m,8); JinvM3.setZero(); J3.setZero(); } int cur=0; int rowOffset = 0; static btAlignedObjectArray ofs; { BT_PROFILE("ofs resize"); ofs.resize(0); ofs.resizeNoInitialize(m_allConstraintArray.size()); } { BT_PROFILE("Compute J and JinvM"); int c=0; int numRows = 0; for (int i=0;igetInvMass(); btVector3 relPosCrossNormalInvInertia = m_allConstraintArray[i+row].m_relpos1CrossNormal * orgBodyA->getInvInertiaTensorWorld(); for (int r=0;r<3;r++) { J3.setElem(cur,r,m_allConstraintArray[i+row].m_contactNormal1[r]); J3.setElem(cur,r+4,m_allConstraintArray[i+row].m_relpos1CrossNormal[r]); JinvM3.setElem(cur,r,normalInvMass[r]); JinvM3.setElem(cur,r+4,relPosCrossNormalInvInertia[r]); } J3.setElem(cur,3,0); JinvM3.setElem(cur,3,0); J3.setElem(cur,7,0); JinvM3.setElem(cur,7,0); } } else { cur += numRows; } if (orgBodyB) { { int slotB=-1; //find free jointNode slot for sbA slotB =jointNodeArray.size(); jointNodeArray.expand();//NonInitializing(); int prevSlot = bodyJointNodeArray[sbB]; bodyJointNodeArray[sbB] = slotB; jointNodeArray[slotB].nextJointNodeIndex = prevSlot; jointNodeArray[slotB].jointIndex = c; jointNodeArray[slotB].otherBodyIndex = orgBodyA ? sbA : -1; jointNodeArray[slotB].constraintRowIndex = i; } for (int row=0;rowgetInvMass(); btVector3 relPosInvInertiaB = m_allConstraintArray[i+row].m_relpos2CrossNormal * orgBodyB->getInvInertiaTensorWorld(); for (int r=0;r<3;r++) { J3.setElem(cur,r,m_allConstraintArray[i+row].m_contactNormal2[r]); J3.setElem(cur,r+4,m_allConstraintArray[i+row].m_relpos2CrossNormal[r]); JinvM3.setElem(cur,r,normalInvMassB[r]); JinvM3.setElem(cur,r+4,relPosInvInertiaB[r]); } J3.setElem(cur,3,0); JinvM3.setElem(cur,3,0); J3.setElem(cur,7,0); JinvM3.setElem(cur,7,0); } } else { cur += numRows; } rowOffset+=numRows; } } //compute JinvM = J*invM. const btScalar* JinvM = JinvM3.getBufferPointer(); const btScalar* Jptr = J3.getBufferPointer(); { BT_PROFILE("m_A.resize"); m_A.resize(n,n); } { BT_PROFILE("m_A.setZero"); m_A.setZero(); } int c=0; { int numRows = 0; BT_PROFILE("Compute A"); for (int i=0;i=0) { int j0 = jointNodeArray[startJointNodeA].jointIndex; int cr0 = jointNodeArray[startJointNodeA].constraintRowIndex; if (j0=0) { int j1 = jointNodeArray[startJointNodeB].jointIndex; int cj1 = jointNodeArray[startJointNodeB].constraintRowIndex; if (j1m_tmpSolverBodyPool.size(); int numConstraintRows = m_allConstraintArray.size(); m_b.resize(numConstraintRows); if (infoGlobal.m_splitImpulse) m_bSplit.resize(numConstraintRows); m_bSplit.setZero(); m_b.setZero(); for (int i=0;igetInvInertiaTensorWorld()[r][c] : 0); } static btMatrixXu J; J.resize(numConstraintRows,6*numBodies); J.setZero(); m_lo.resize(numConstraintRows); m_hi.resize(numConstraintRows); for (int i=0;i