Home | History | Annotate | Download | only in ConstraintSolver
      1 /*
      2 Bullet Continuous Collision Detection and Physics Library
      3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
      4 
      5 This software is provided 'as-is', without any express or implied warranty.
      6 In no event will the authors be held liable for any damages arising from the use of this software.
      7 Permission is granted to anyone to use this software for any purpose,
      8 including commercial applications, and to alter it and redistribute it freely,
      9 subject to the following restrictions:
     10 
     11 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.
     12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
     13 3. This notice may not be removed or altered from any source distribution.
     14 */
     15 
     16 #ifndef BT_SEQUENTIAL_IMPULSE_CONSTRAINT_SOLVER_H
     17 #define BT_SEQUENTIAL_IMPULSE_CONSTRAINT_SOLVER_H
     18 
     19 class btIDebugDraw;
     20 class btPersistentManifold;
     21 class btDispatcher;
     22 class btCollisionObject;
     23 #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
     24 #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
     25 #include "BulletDynamics/ConstraintSolver/btSolverBody.h"
     26 #include "BulletDynamics/ConstraintSolver/btSolverConstraint.h"
     27 #include "BulletCollision/NarrowPhaseCollision/btManifoldPoint.h"
     28 #include "BulletDynamics/ConstraintSolver/btConstraintSolver.h"
     29 
     30 typedef btSimdScalar(*btSingleConstraintRowSolver)(btSolverBody&, btSolverBody&, const btSolverConstraint&);
     31 
     32 ///The btSequentialImpulseConstraintSolver is a fast SIMD implementation of the Projected Gauss Seidel (iterative LCP) method.
     33 ATTRIBUTE_ALIGNED16(class) btSequentialImpulseConstraintSolver : public btConstraintSolver
     34 {
     35 protected:
     36 	btAlignedObjectArray<btSolverBody>      m_tmpSolverBodyPool;
     37 	btConstraintArray			m_tmpSolverContactConstraintPool;
     38 	btConstraintArray			m_tmpSolverNonContactConstraintPool;
     39 	btConstraintArray			m_tmpSolverContactFrictionConstraintPool;
     40 	btConstraintArray			m_tmpSolverContactRollingFrictionConstraintPool;
     41 
     42 	btAlignedObjectArray<int>	m_orderTmpConstraintPool;
     43 	btAlignedObjectArray<int>	m_orderNonContactConstraintPool;
     44 	btAlignedObjectArray<int>	m_orderFrictionConstraintPool;
     45 	btAlignedObjectArray<btTypedConstraint::btConstraintInfo1> m_tmpConstraintSizesPool;
     46 	int							m_maxOverrideNumSolverIterations;
     47 	int m_fixedBodyId;
     48 
     49 	btSingleConstraintRowSolver m_resolveSingleConstraintRowGeneric;
     50 	btSingleConstraintRowSolver m_resolveSingleConstraintRowLowerLimit;
     51 
     52 	void setupFrictionConstraint(	btSolverConstraint& solverConstraint, const btVector3& normalAxis,int solverBodyIdA,int  solverBodyIdB,
     53 									btManifoldPoint& cp,const btVector3& rel_pos1,const btVector3& rel_pos2,
     54 									btCollisionObject* colObj0,btCollisionObject* colObj1, btScalar relaxation,
     55 									btScalar desiredVelocity=0., btScalar cfmSlip=0.);
     56 
     57 	void setupRollingFrictionConstraint(	btSolverConstraint& solverConstraint, const btVector3& normalAxis,int solverBodyIdA,int  solverBodyIdB,
     58 									btManifoldPoint& cp,const btVector3& rel_pos1,const btVector3& rel_pos2,
     59 									btCollisionObject* colObj0,btCollisionObject* colObj1, btScalar relaxation,
     60 									btScalar desiredVelocity=0., btScalar cfmSlip=0.);
     61 
     62 	btSolverConstraint&	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=0., btScalar cfmSlip=0.);
     63 	btSolverConstraint&	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=0, btScalar cfmSlip=0.f);
     64 
     65 
     66 	void setupContactConstraint(btSolverConstraint& solverConstraint, int solverBodyIdA, int solverBodyIdB, btManifoldPoint& cp,
     67 								const btContactSolverInfo& infoGlobal,btScalar& relaxation, const btVector3& rel_pos1, const btVector3& rel_pos2);
     68 
     69 	static void	applyAnisotropicFriction(btCollisionObject* colObj,btVector3& frictionDirection, int frictionMode);
     70 
     71 	void setFrictionConstraintImpulse( btSolverConstraint& solverConstraint, int solverBodyIdA,int solverBodyIdB,
     72 										 btManifoldPoint& cp, const btContactSolverInfo& infoGlobal);
     73 
     74 	///m_btSeed2 is used for re-arranging the constraint rows. improves convergence/quality of friction
     75 	unsigned long	m_btSeed2;
     76 
     77 
     78 	btScalar restitutionCurve(btScalar rel_vel, btScalar restitution);
     79 
     80 	virtual void convertContacts(btPersistentManifold** manifoldPtr, int numManifolds, const btContactSolverInfo& infoGlobal);
     81 
     82 	void	convertContact(btPersistentManifold* manifold,const btContactSolverInfo& infoGlobal);
     83 
     84 
     85 	void	resolveSplitPenetrationSIMD(
     86      btSolverBody& bodyA,btSolverBody& bodyB,
     87         const btSolverConstraint& contactConstraint);
     88 
     89 	void	resolveSplitPenetrationImpulseCacheFriendly(
     90        btSolverBody& bodyA,btSolverBody& bodyB,
     91         const btSolverConstraint& contactConstraint);
     92 
     93 	//internal method
     94 	int		getOrInitSolverBody(btCollisionObject& body,btScalar timeStep);
     95 	void	initSolverBody(btSolverBody* solverBody, btCollisionObject* collisionObject, btScalar timeStep);
     96 
     97 	btSimdScalar	resolveSingleConstraintRowGeneric(btSolverBody& bodyA,btSolverBody& bodyB,const btSolverConstraint& contactConstraint);
     98 	btSimdScalar	resolveSingleConstraintRowGenericSIMD(btSolverBody& bodyA,btSolverBody& bodyB,const btSolverConstraint& contactConstraint);
     99 	btSimdScalar	resolveSingleConstraintRowLowerLimit(btSolverBody& bodyA,btSolverBody& bodyB,const btSolverConstraint& contactConstraint);
    100 	btSimdScalar	resolveSingleConstraintRowLowerLimitSIMD(btSolverBody& bodyA,btSolverBody& bodyB,const btSolverConstraint& contactConstraint);
    101 
    102 protected:
    103 
    104 
    105 	virtual void solveGroupCacheFriendlySplitImpulseIterations(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifoldPtr, int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& infoGlobal,btIDebugDraw* debugDrawer);
    106 	virtual btScalar solveGroupCacheFriendlyFinish(btCollisionObject** bodies,int numBodies,const btContactSolverInfo& infoGlobal);
    107 	virtual btScalar solveSingleIteration(int iteration, btCollisionObject** bodies ,int numBodies,btPersistentManifold** manifoldPtr, int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& infoGlobal,btIDebugDraw* debugDrawer);
    108 
    109 	virtual btScalar solveGroupCacheFriendlySetup(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifoldPtr, int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& infoGlobal,btIDebugDraw* debugDrawer);
    110 	virtual btScalar solveGroupCacheFriendlyIterations(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifoldPtr, int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& infoGlobal,btIDebugDraw* debugDrawer);
    111 
    112 
    113 public:
    114 
    115 	BT_DECLARE_ALIGNED_ALLOCATOR();
    116 
    117 	btSequentialImpulseConstraintSolver();
    118 	virtual ~btSequentialImpulseConstraintSolver();
    119 
    120 	virtual btScalar solveGroup(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifold,int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& info, btIDebugDraw* debugDrawer,btDispatcher* dispatcher);
    121 
    122 	///clear internal cached data and reset random seed
    123 	virtual	void	reset();
    124 
    125 	unsigned long btRand2();
    126 
    127 	int btRandInt2 (int n);
    128 
    129 	void	setRandSeed(unsigned long seed)
    130 	{
    131 		m_btSeed2 = seed;
    132 	}
    133 	unsigned long	getRandSeed() const
    134 	{
    135 		return m_btSeed2;
    136 	}
    137 
    138 
    139 	virtual btConstraintSolverType	getSolverType() const
    140 	{
    141 		return BT_SEQUENTIAL_IMPULSE_SOLVER;
    142 	}
    143 
    144 	btSingleConstraintRowSolver	getActiveConstraintRowSolverGeneric()
    145 	{
    146 		return m_resolveSingleConstraintRowGeneric;
    147 	}
    148 	void setConstraintRowSolverGeneric(btSingleConstraintRowSolver rowSolver)
    149 	{
    150 		m_resolveSingleConstraintRowGeneric = rowSolver;
    151 	}
    152 	btSingleConstraintRowSolver	getActiveConstraintRowSolverLowerLimit()
    153 	{
    154 		return m_resolveSingleConstraintRowLowerLimit;
    155 	}
    156 	void setConstraintRowSolverLowerLimit(btSingleConstraintRowSolver rowSolver)
    157 	{
    158 		m_resolveSingleConstraintRowLowerLimit = rowSolver;
    159 	}
    160 
    161 	///Various implementations of solving a single constraint row using a generic equality constraint, using scalar reference, SSE2 or SSE4
    162 	btSingleConstraintRowSolver	getScalarConstraintRowSolverGeneric();
    163 	btSingleConstraintRowSolver	getSSE2ConstraintRowSolverGeneric();
    164 	btSingleConstraintRowSolver	getSSE4_1ConstraintRowSolverGeneric();
    165 
    166 	///Various implementations of solving a single constraint row using an inequality (lower limit) constraint, using scalar reference, SSE2 or SSE4
    167 	btSingleConstraintRowSolver	getScalarConstraintRowSolverLowerLimit();
    168 	btSingleConstraintRowSolver	getSSE2ConstraintRowSolverLowerLimit();
    169 	btSingleConstraintRowSolver	getSSE4_1ConstraintRowSolverLowerLimit();
    170 };
    171 
    172 
    173 
    174 
    175 #endif //BT_SEQUENTIAL_IMPULSE_CONSTRAINT_SOLVER_H
    176 
    177