Tizen 2.1 base
[platform/upstream/libbullet.git] / Extras / PhysicsEffects / include / BulletPhysicsEffects / btLowLevelConstraintSolver2.cpp
1 /*\r
2    Copyright (C) 2010 Sony Computer Entertainment Inc.\r
3    All rights reserved.\r
4 \r
5 This software is provided 'as-is', without any express or implied warranty.\r
6 In no event will the authors be held liable for any damages arising from the use of this software.\r
7 Permission is granted to anyone to use this software for any purpose, \r
8 including commercial applications, and to alter it and redistribute it freely, \r
9 subject to the following restrictions:\r
10 \r
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.\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\r
13 3. This notice may not be removed or altered from any source distribution.\r
14 \r
15 */\r
16 \r
17 \r
18 #include "btLowLevelConstraintSolver2.h"\r
19 #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"\r
20 \r
21 #include "LinearMath/btQuickprof.h"\r
22 #include "BulletMultiThreaded/btThreadSupportInterface.h"\r
23 #include "BulletMultiThreaded/HeapManager.h"\r
24 #include "BulletMultiThreaded/PlatformDefinitions.h"\r
25 #include "BulletPhysicsEffects/btLowLevelData.h"\r
26 #include "BulletMultiThreaded/vectormath2bullet.h"\r
27 //#include "PfxSimdUtils.h"\r
28 #include "LinearMath/btScalar.h"\r
29 #include "BulletMultiThreaded/HeapManager.h"\r
30 \r
31 \r
32 \r
33 /////////////////\r
34 \r
35 \r
36 #define TMP_BUFF_BYTES (15*1024*1024)\r
37 static unsigned char ATTRIBUTE_ALIGNED128(tmp_buff[TMP_BUFF_BYTES]);\r
38 \r
39 \r
40 \r
41 \r
42 btLowLevelConstraintSolver2::btLowLevelConstraintSolver2(btLowLevelData* lowLevelData)\r
43 :m_lowLevelData(lowLevelData)\r
44 {\r
45         \r
46 }\r
47         \r
48 btLowLevelConstraintSolver2::~btLowLevelConstraintSolver2()\r
49 {\r
50         \r
51 }\r
52 \r
53 \r
54 static void solveConstraints(btLowLevelData* lowLevelData, btScalar timeStep, btScalar separateBias, int iteration)\r
55 {\r
56         PfxPerfCounter pc;\r
57         HeapManager pool((unsigned char*)tmp_buff,TMP_BUFF_BYTES);\r
58 \r
59         unsigned int numCurrentPairs = lowLevelData->m_numPairs[lowLevelData->m_pairSwap];\r
60         PfxBroadphasePair *currentPairs = lowLevelData->m_pairsBuff[lowLevelData->m_pairSwap];\r
61 \r
62         pc.countBegin("setup solver bodies");\r
63         {\r
64                 PfxSetupSolverBodiesParam param;\r
65                 param.states = lowLevelData->m_states;\r
66                 param.bodies = lowLevelData->m_bodies;\r
67                 param.solverBodies = lowLevelData->m_solverBodies;\r
68                 param.numRigidBodies = lowLevelData->m_numRigidBodies;\r
69                 \r
70                 int ret = pfxSetupSolverBodies(param);\r
71                 if(ret != SCE_PFX_OK) \r
72                         SCE_PFX_PRINTF("pfxSetupSolverBodies failed %d\n",ret);\r
73         }\r
74         pc.countEnd();\r
75 \r
76         pc.countBegin("setup contact constraints");\r
77         {\r
78                 PfxSetupContactConstraintsParam param;\r
79                 param.contactPairs = currentPairs;\r
80                 param.numContactPairs = numCurrentPairs;\r
81                 param.offsetContactManifolds = lowLevelData->m_contacts;\r
82                 param.offsetRigidStates = lowLevelData->m_states;\r
83                 param.offsetRigidBodies = lowLevelData->m_bodies;\r
84                 param.offsetSolverBodies = lowLevelData->m_solverBodies;\r
85                 param.numRigidBodies = lowLevelData->m_numRigidBodies;\r
86                 param.timeStep = timeStep;\r
87                 param.separateBias = separateBias;\r
88                 \r
89                 int ret = pfxSetupContactConstraints(param);\r
90                 if(ret != SCE_PFX_OK) SCE_PFX_PRINTF("pfxSetupJointConstraints failed %d\n",ret);\r
91         }\r
92         pc.countEnd();\r
93 #if 0\r
94         pc.countBegin("setup joint constraints");\r
95         {\r
96                 PfxSetupJointConstraintsParam param;\r
97                 param.jointPairs = 0;//jointPairs;\r
98                 param.numJointPairs = 0;//numJoints;\r
99                 param.offsetJoints = 0;//joints;\r
100                 param.offsetRigidStates = lowLevelData->m_states;\r
101                 param.offsetRigidBodies = lowLevelData->m_bodies;\r
102                 param.offsetSolverBodies = lowLevelData->m_solverBodies;\r
103                 param.numRigidBodies = lowLevelData->m_numRigidBodies;\r
104                 param.timeStep = timeStep;\r
105 \r
106                 for(int i=0;i<numJoints;i++) {\r
107                         pfxUpdateJointPairs(jointPairs[i],i,joints[i],states[joints[i].m_rigidBodyIdA],states[joints[i].m_rigidBodyIdB]);\r
108                 }\r
109 \r
110                 int ret = pfxSetupJointConstraints(param);\r
111                 if(ret != SCE_PFX_OK) SCE_PFX_PRINTF("pfxSetupJointConstraints failed %d\n",ret);\r
112         }\r
113         pc.countEnd();\r
114 #endif\r
115 \r
116         pc.countBegin("solve constraints");\r
117         {\r
118                 PfxSolveConstraintsParam param;\r
119                 param.workBytes = pfxGetWorkBytesOfSolveConstraints(lowLevelData->m_numRigidBodies,numCurrentPairs,0);//numJoints);\r
120                 param.workBuff = pool.allocate(param.workBytes);\r
121                 param.contactPairs = currentPairs;\r
122                 param.numContactPairs = numCurrentPairs;\r
123                 param.offsetContactManifolds = lowLevelData->m_contacts;\r
124                 param.jointPairs = 0;//jointPairs;\r
125                 param.numJointPairs = 0;//numJoints;\r
126                 param.offsetJoints = 0;//joints;\r
127                 param.offsetRigidStates = lowLevelData->m_states;\r
128                 param.offsetSolverBodies = lowLevelData->m_solverBodies;\r
129                 param.numRigidBodies = lowLevelData->m_numRigidBodies;\r
130                 param.iteration = iteration;\r
131 \r
132                 int ret = pfxSolveConstraints(param);\r
133                 if(ret != SCE_PFX_OK) SCE_PFX_PRINTF("pfxSolveConstraints failed %d\n",ret);\r
134                 \r
135                 pool.deallocate(param.workBuff);\r
136         }\r
137         pc.countEnd();\r
138 \r
139 \r
140         //pc.printCount();\r
141 }\r
142 \r
143 \r
144 \r
145 btScalar btLowLevelConstraintSolver2::solveGroup(btCollisionObject** bodies1,int numRigidBodies,btPersistentManifold** manifoldPtr,int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer, btStackAlloc* stackAlloc,btDispatcher* dispatcher)\r
146 {\r
147 \r
148         //copy velocity from BT to PE\r
149         {\r
150                 BT_PROFILE("copy back");\r
151                 for (int i=0;i<numRigidBodies;i++)\r
152                 {\r
153                         btCollisionObject* obj = bodies1[i];\r
154                         btRigidBody* rb = btRigidBody::upcast(obj);\r
155                         if (rb && (rb->getInvMass()>0.f))\r
156                         {\r
157                                 int objectIndex = rb->getBroadphaseProxy()->m_uniqueId;\r
158                                 sce::PhysicsEffects::PfxRigidState& state = m_lowLevelData->m_states[objectIndex];\r
159 \r
160                                 state.setLinearVelocity(getVmVector3(rb->getLinearVelocity()));\r
161                                 state.setAngularVelocity(getVmVector3(rb->getAngularVelocity()));\r
162 \r
163                         }\r
164                 }\r
165         }\r
166 \r
167         btScalar separateBias = 0.1f;\r
168         solveConstraints(m_lowLevelData, infoGlobal.m_timeStep, separateBias, infoGlobal.m_numIterations);\r
169 \r
170 \r
171         //copy resulting velocity back from PE to BT\r
172         {\r
173                 BT_PROFILE("copy back");\r
174                 for (int i=0;i<numRigidBodies;i++)\r
175                 {\r
176                         btCollisionObject* obj = bodies1[i];\r
177                         btRigidBody* rb = btRigidBody::upcast(obj);\r
178                         if (rb && (rb->getInvMass()>0.f))\r
179                         {\r
180                                 int objectIndex = rb->getBroadphaseProxy()->m_uniqueId;\r
181                                 sce::PhysicsEffects::PfxRigidState& state = m_lowLevelData->m_states[objectIndex];\r
182 \r
183                                 rb->setLinearVelocity(btVector3(state.getLinearVelocity().getX(),state.getLinearVelocity().getY(),state.getLinearVelocity().getZ()));\r
184                                 rb->setAngularVelocity(btVector3(state.getAngularVelocity().getX(),state.getAngularVelocity().getY(),state.getAngularVelocity().getZ()));\r
185                         }\r
186                 }\r
187         }\r
188 \r
189         return 0.f;\r
190 }\r