bullet: Update to current svn, r2636
[blender.git] / extern / bullet2 / src / BulletDynamics / ConstraintSolver / btSequentialImpulseConstraintSolver.h
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 btStackAlloc;
22 class btDispatcher;
23 class btCollisionObject;
24 #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
25 #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
26 #include "BulletDynamics/ConstraintSolver/btSolverBody.h"
27 #include "BulletDynamics/ConstraintSolver/btSolverConstraint.h"
28 #include "BulletCollision/NarrowPhaseCollision/btManifoldPoint.h"
29 #include "BulletDynamics/ConstraintSolver/btConstraintSolver.h"
30
31 ///The btSequentialImpulseConstraintSolver is a fast SIMD implementation of the Projected Gauss Seidel (iterative LCP) method.
32 ATTRIBUTE_ALIGNED16(class) btSequentialImpulseConstraintSolver : public btConstraintSolver
33 {
34 protected:
35         btAlignedObjectArray<btSolverBody>      m_tmpSolverBodyPool;
36         btConstraintArray                       m_tmpSolverContactConstraintPool;
37         btConstraintArray                       m_tmpSolverNonContactConstraintPool;
38         btConstraintArray                       m_tmpSolverContactFrictionConstraintPool;
39         btConstraintArray                       m_tmpSolverContactRollingFrictionConstraintPool;
40
41         btAlignedObjectArray<int>       m_orderTmpConstraintPool;
42         btAlignedObjectArray<int>       m_orderNonContactConstraintPool;
43         btAlignedObjectArray<int>       m_orderFrictionConstraintPool;
44         btAlignedObjectArray<btTypedConstraint::btConstraintInfo1> m_tmpConstraintSizesPool;
45         int                                                     m_maxOverrideNumSolverIterations;
46
47         void setupFrictionConstraint(   btSolverConstraint& solverConstraint, const btVector3& normalAxis,int solverBodyIdA,int  solverBodyIdB,
48                                                                         btManifoldPoint& cp,const btVector3& rel_pos1,const btVector3& rel_pos2,
49                                                                         btCollisionObject* colObj0,btCollisionObject* colObj1, btScalar relaxation, 
50                                                                         btScalar desiredVelocity=0., btScalar cfmSlip=0.);
51
52         void setupRollingFrictionConstraint(    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         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.);
58         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);
59
60
61         void setupContactConstraint(btSolverConstraint& solverConstraint, int solverBodyIdA, int solverBodyIdB, btManifoldPoint& cp, 
62                                                                 const btContactSolverInfo& infoGlobal, btVector3& vel, btScalar& rel_vel, btScalar& relaxation, 
63                                                                 btVector3& rel_pos1, btVector3& rel_pos2);
64
65         void setFrictionConstraintImpulse( btSolverConstraint& solverConstraint, int solverBodyIdA,int solverBodyIdB, 
66                                                                                  btManifoldPoint& cp, const btContactSolverInfo& infoGlobal);
67
68         ///m_btSeed2 is used for re-arranging the constraint rows. improves convergence/quality of friction
69         unsigned long   m_btSeed2;
70
71         
72         btScalar restitutionCurve(btScalar rel_vel, btScalar restitution);
73
74         void    convertContact(btPersistentManifold* manifold,const btContactSolverInfo& infoGlobal);
75
76
77         void    resolveSplitPenetrationSIMD(
78      btSolverBody& bodyA,btSolverBody& bodyB,
79         const btSolverConstraint& contactConstraint);
80
81         void    resolveSplitPenetrationImpulseCacheFriendly(
82        btSolverBody& bodyA,btSolverBody& bodyB,
83         const btSolverConstraint& contactConstraint);
84
85         //internal method
86         int             getOrInitSolverBody(btCollisionObject& body);
87         void    initSolverBody(btSolverBody* solverBody, btCollisionObject* collisionObject);
88
89         void    resolveSingleConstraintRowGeneric(btSolverBody& bodyA,btSolverBody& bodyB,const btSolverConstraint& contactConstraint);
90
91         void    resolveSingleConstraintRowGenericSIMD(btSolverBody& bodyA,btSolverBody& bodyB,const btSolverConstraint& contactConstraint);
92         
93         void    resolveSingleConstraintRowLowerLimit(btSolverBody& bodyA,btSolverBody& bodyB,const btSolverConstraint& contactConstraint);
94         
95         void    resolveSingleConstraintRowLowerLimitSIMD(btSolverBody& bodyA,btSolverBody& bodyB,const btSolverConstraint& contactConstraint);
96                 
97 protected:
98         
99         
100         virtual void solveGroupCacheFriendlySplitImpulseIterations(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifoldPtr, int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& infoGlobal,btIDebugDraw* debugDrawer,btStackAlloc* stackAlloc);
101         virtual btScalar solveGroupCacheFriendlyFinish(btCollisionObject** bodies,int numBodies,const btContactSolverInfo& infoGlobal);
102         btScalar solveSingleIteration(int iteration, btCollisionObject** bodies ,int numBodies,btPersistentManifold** manifoldPtr, int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& infoGlobal,btIDebugDraw* debugDrawer,btStackAlloc* stackAlloc);
103
104         virtual btScalar solveGroupCacheFriendlySetup(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifoldPtr, int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& infoGlobal,btIDebugDraw* debugDrawer,btStackAlloc* stackAlloc);
105         virtual btScalar solveGroupCacheFriendlyIterations(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifoldPtr, int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& infoGlobal,btIDebugDraw* debugDrawer,btStackAlloc* stackAlloc);
106
107
108 public:
109
110         BT_DECLARE_ALIGNED_ALLOCATOR();
111         
112         btSequentialImpulseConstraintSolver();
113         virtual ~btSequentialImpulseConstraintSolver();
114
115         virtual btScalar solveGroup(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifold,int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& info, btIDebugDraw* debugDrawer, btStackAlloc* stackAlloc,btDispatcher* dispatcher);
116         
117
118         
119         ///clear internal cached data and reset random seed
120         virtual void    reset();
121         
122         unsigned long btRand2();
123
124         int btRandInt2 (int n);
125
126         void    setRandSeed(unsigned long seed)
127         {
128                 m_btSeed2 = seed;
129         }
130         unsigned long   getRandSeed() const
131         {
132                 return m_btSeed2;
133         }
134
135 };
136
137
138
139
140 #endif //BT_SEQUENTIAL_IMPULSE_CONSTRAINT_SOLVER_H
141