Fixed several bugs: python refcounting related and Bullet related (basic add/remove...
[blender.git] / source / gameengine / Physics / Bullet / CcdPhysicsController.cpp
1 #include "CcdPhysicsController.h"
2
3 #include "Dynamics/RigidBody.h"
4 #include "PHY_IMotionState.h"
5 #include "BroadphaseCollision/BroadphaseProxy.h"
6 #include "CollisionShapes/ConvexShape.h"
7 #include "CcdPhysicsEnvironment.h"
8
9
10 class BP_Proxy;
11
12 ///todo: fill all the empty CcdPhysicsController methods, hook them up to the RigidBody class
13
14 //'temporarily' global variables
15 float   gDeactivationTime = 2.f;
16 bool    gDisableDeactivation = false;
17
18 float gLinearSleepingTreshold = 0.8f;
19 float gAngularSleepingTreshold = 1.0f;
20
21 #include "Dynamics/MassProps.h"
22
23 SimdVector3 startVel(0,0,0);//-10000);
24 CcdPhysicsController::CcdPhysicsController (const CcdConstructionInfo& ci)
25 :m_cci(ci)
26 {
27         m_collisionDelay = 0;
28         m_newClientInfo = 0;
29         
30         m_MotionState = ci.m_MotionState;
31
32
33         m_broadphaseHandle = ci.m_broadphaseHandle;
34
35         m_collisionShape = ci.m_collisionShape;
36
37         CreateRigidbody();
38
39         
40         #ifdef WIN32
41         if (m_body->getInvMass())
42                 m_body->setLinearVelocity(startVel);
43         #endif
44
45 }
46
47 void CcdPhysicsController::CreateRigidbody()
48 {
49         SimdTransform trans;
50         float tmp[3];
51         m_MotionState->getWorldPosition(tmp[0],tmp[1],tmp[2]);
52         trans.setOrigin(SimdVector3(tmp[0],tmp[1],tmp[2]));
53
54         SimdQuaternion orn;
55         m_MotionState->getWorldOrientation(orn[0],orn[1],orn[2],orn[3]);
56         trans.setRotation(orn);
57
58         MassProps mp(m_cci.m_mass, m_cci.m_localInertiaTensor);
59
60         m_body = new RigidBody(mp,0,0,m_cci.m_friction,m_cci.m_restitution);
61         
62         //
63         // init the rigidbody properly
64         //
65         
66         m_body->setMassProps(m_cci.m_mass, m_cci.m_localInertiaTensor);
67         m_body->setGravity( m_cci.m_gravity);
68         m_body->setDamping(m_cci.m_linearDamping, m_cci.m_angularDamping);
69         m_body->setCenterOfMassTransform( trans );
70
71
72 }
73
74 CcdPhysicsController::~CcdPhysicsController()
75 {
76         //will be reference counted, due to sharing
77         //delete m_collisionShape;
78         m_cci.m_physicsEnv->removeCcdPhysicsController(this);
79         delete m_MotionState;
80         delete m_body;
81 }
82
83                 /**
84                         SynchronizeMotionStates ynchronizes dynas, kinematic and deformable entities (and do 'late binding')
85                 */
86 bool            CcdPhysicsController::SynchronizeMotionStates(float time)
87 {
88         const SimdVector3& worldPos = m_body->getCenterOfMassPosition();
89         m_MotionState->setWorldPosition(worldPos[0],worldPos[1],worldPos[2]);
90         
91         const SimdQuaternion& worldquat = m_body->getOrientation();
92         m_MotionState->setWorldOrientation(worldquat[0],worldquat[1],worldquat[2],worldquat[3]);
93
94         m_MotionState->calculateWorldTransformations();
95
96         float scale[3];
97         m_MotionState->getWorldScaling(scale[0],scale[1],scale[2]);
98         
99         SimdVector3 scaling(scale[0],scale[1],scale[2]);
100         m_collisionShape->setLocalScaling(scaling);
101
102
103         return true;
104 }
105
106                 /**
107                         WriteMotionStateToDynamics synchronizes dynas, kinematic and deformable entities (and do 'late binding')
108                 */
109                 
110 void            CcdPhysicsController::WriteMotionStateToDynamics(bool nondynaonly)
111 {
112
113 }
114 void            CcdPhysicsController::WriteDynamicsToMotionState()
115 {
116 }
117                 // controller replication
118 void            CcdPhysicsController::PostProcessReplica(class PHY_IMotionState* motionstate,class PHY_IPhysicsController* parentctrl)
119 {
120
121         m_MotionState = motionstate;
122         m_broadphaseHandle = 0;
123         m_body = 0;
124         CreateRigidbody();
125                         
126         m_cci.m_physicsEnv->addCcdPhysicsController(this);
127
128
129 /*      SM_Object* dynaparent=0;
130         SumoPhysicsController* sumoparentctrl = (SumoPhysicsController* )parentctrl;
131         
132         if (sumoparentctrl)
133         {
134                 dynaparent = sumoparentctrl->GetSumoObject();
135         }
136         
137         SM_Object* orgsumoobject = m_sumoObj;
138         
139         
140         m_sumoObj       =       new SM_Object(
141                 orgsumoobject->getShapeHandle(), 
142                 orgsumoobject->getMaterialProps(),                      
143                 orgsumoobject->getShapeProps(),
144                 dynaparent);
145         
146         m_sumoObj->setRigidBody(orgsumoobject->isRigidBody());
147         
148         m_sumoObj->setMargin(orgsumoobject->getMargin());
149         m_sumoObj->setPosition(orgsumoobject->getPosition());
150         m_sumoObj->setOrientation(orgsumoobject->getOrientation());
151         //if it is a dyna, register for a callback
152         m_sumoObj->registerCallback(*this);
153         
154         m_sumoScene->add(* (m_sumoObj));
155         */
156
157
158
159 }
160
161                 // kinematic methods
162 void            CcdPhysicsController::RelativeTranslate(float dlocX,float dlocY,float dlocZ,bool local)
163 {
164         SimdVector3 dloc(dlocX,dlocY,dlocZ);
165         SimdTransform xform = m_body->getCenterOfMassTransform();
166
167         if (local)
168         {
169                 dloc = xform.getBasis()*dloc;
170         }
171
172         xform.setOrigin(xform.getOrigin() + dloc);
173         this->m_body->setCenterOfMassTransform(xform);
174
175 }
176
177 void            CcdPhysicsController::RelativeRotate(const float rotval[9],bool local)
178 {
179         if (m_body )
180         {
181                 SimdMatrix3x3 drotmat(  rotval[0],rotval[1],rotval[2],
182                                                                 rotval[4],rotval[5],rotval[6],
183                                                                 rotval[8],rotval[9],rotval[10]);
184
185
186                 SimdMatrix3x3 currentOrn;
187                 GetWorldOrientation(currentOrn);
188
189                 SimdTransform xform = m_body->getCenterOfMassTransform();
190
191                 xform.setBasis(xform.getBasis()*(local ? 
192                 drotmat : (currentOrn.inverse() * drotmat * currentOrn)));
193
194                 m_body->setCenterOfMassTransform(xform);
195         }
196
197 }
198
199 void CcdPhysicsController::GetWorldOrientation(SimdMatrix3x3& mat)
200 {
201         float orn[4];
202         m_MotionState->getWorldOrientation(orn[0],orn[1],orn[2],orn[3]);
203         SimdQuaternion quat(orn[0],orn[1],orn[2],orn[3]);
204         mat.setRotation(quat);
205 }
206
207 void            CcdPhysicsController::getOrientation(float &quatImag0,float &quatImag1,float &quatImag2,float &quatReal)
208 {
209         SimdQuaternion q = m_body->getCenterOfMassTransform().getRotation();
210         quatImag0 = q[0];
211         quatImag1 = q[1];
212         quatImag2 = q[2];
213         quatReal = q[3];
214 }
215 void            CcdPhysicsController::setOrientation(float quatImag0,float quatImag1,float quatImag2,float quatReal)
216 {
217         m_body->activate();
218
219         SimdTransform xform  = m_body->getCenterOfMassTransform();
220         xform.setRotation(SimdQuaternion(quatImag0,quatImag1,quatImag2,quatReal));
221         m_body->setCenterOfMassTransform(xform);
222
223 }
224
225 void            CcdPhysicsController::setPosition(float posX,float posY,float posZ)
226 {
227         m_body->activate();
228
229         SimdTransform xform  = m_body->getCenterOfMassTransform();
230         xform.setOrigin(SimdVector3(posX,posY,posZ));
231         m_body->setCenterOfMassTransform(xform);
232
233 }
234 void            CcdPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
235 {
236 }
237
238 void            CcdPhysicsController::getPosition(PHY__Vector3& pos) const
239 {
240         assert(0);
241 }
242
243 void            CcdPhysicsController::setScaling(float scaleX,float scaleY,float scaleZ)
244 {
245         if (m_body && m_body->GetCollisionShape())
246         {
247                 SimdVector3 scaling(scaleX,scaleY,scaleZ);
248                 m_body->GetCollisionShape()->setLocalScaling(scaling);
249         }
250 }
251                 
252                 // physics methods
253 void            CcdPhysicsController::ApplyTorque(float torqueX,float torqueY,float torqueZ,bool local)
254 {
255         SimdVector3 torque(torqueX,torqueY,torqueZ);
256         SimdTransform xform = m_body->getCenterOfMassTransform();
257         if (local)
258         {
259                 torque  = xform.getBasis()*torque;
260         }
261         m_body->applyTorque(torque);
262 }
263
264 void            CcdPhysicsController::ApplyForce(float forceX,float forceY,float forceZ,bool local)
265 {
266         SimdVector3 force(forceX,forceX,forceX);
267         SimdTransform xform = m_body->getCenterOfMassTransform();
268         if (local)
269         {
270                 force   = xform.getBasis()*force;
271         }
272         m_body->applyCentralForce(force);
273 }
274 void            CcdPhysicsController::SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local)
275 {
276         SimdVector3 angvel(ang_velX,ang_velY,ang_velZ);
277         SimdTransform xform = m_body->getCenterOfMassTransform();
278         if (local)
279         {
280                 angvel  = xform.getBasis()*angvel;
281         }
282
283         m_body->setAngularVelocity(angvel);
284
285 }
286 void            CcdPhysicsController::SetLinearVelocity(float lin_velX,float lin_velY,float lin_velZ,bool local)
287 {
288         SimdVector3 linVel(lin_velX,lin_velY,lin_velZ);
289         SimdTransform xform = m_body->getCenterOfMassTransform();
290         if (local)
291         {
292                 linVel  = xform.getBasis()*linVel;
293         }
294         m_body->setLinearVelocity(linVel);
295 }
296 void            CcdPhysicsController::applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ)
297 {
298         
299         SimdVector3 impulse(impulseX,impulseY,impulseZ);
300         SimdVector3 pos(attachX,attachY,attachZ);
301
302         m_body->activate();
303
304         m_body->applyImpulse(impulse,pos);
305
306 }
307 void            CcdPhysicsController::SetActive(bool active)
308 {
309 }
310                 // reading out information from physics
311 void            CcdPhysicsController::GetLinearVelocity(float& linvX,float& linvY,float& linvZ)
312 {
313         const SimdVector3& linvel = this->m_body->getLinearVelocity();
314         linvX = linvel.x();
315         linvY = linvel.y();
316         linvZ = linvel.z();
317
318 }
319
320 void            CcdPhysicsController::GetAngularVelocity(float& angVelX,float& angVelY,float& angVelZ)
321 {
322         const SimdVector3& angvel= m_body->getAngularVelocity();
323         angVelX = angvel.x();
324         angVelY = angvel.y();
325         angVelZ = angvel.z();
326 }
327
328 void            CcdPhysicsController::GetVelocity(const float posX,const float posY,const float posZ,float& linvX,float& linvY,float& linvZ)
329 {
330         SimdVector3 pos(posX,posY,posZ);
331         SimdVector3 rel_pos = pos-m_body->getCenterOfMassPosition();
332         SimdVector3 linvel = m_body->getVelocityInLocalPoint(rel_pos);
333         linvX = linvel.x();
334         linvY = linvel.y();
335         linvZ = linvel.z();
336 }
337 void            CcdPhysicsController::getReactionForce(float& forceX,float& forceY,float& forceZ)
338 {
339 }
340
341                 // dyna's that are rigidbody are free in orientation, dyna's with non-rigidbody are restricted 
342 void            CcdPhysicsController::setRigidBody(bool rigid)
343 {
344 }
345
346                 // clientinfo for raycasts for example
347 void*           CcdPhysicsController::getNewClientInfo()
348 {
349         return m_newClientInfo;
350 }
351 void            CcdPhysicsController::setNewClientInfo(void* clientinfo)
352 {
353         m_newClientInfo = clientinfo;
354 }
355
356
357 void    CcdPhysicsController::UpdateDeactivation(float timeStep)
358 {
359         if ( (m_body->GetActivationState() == 2))
360                 return;
361         
362
363         if ((m_body->getLinearVelocity().length2() < gLinearSleepingTreshold*gLinearSleepingTreshold) &&
364                 (m_body->getAngularVelocity().length2() < gAngularSleepingTreshold*gAngularSleepingTreshold))
365         {
366                 m_body->m_deactivationTime += timeStep;
367         } else
368         {
369                 m_body->m_deactivationTime=0.f;
370                 m_body->SetActivationState(0);
371         }
372
373 }
374
375 bool CcdPhysicsController::wantsSleeping()
376 {
377
378         //disable deactivation
379         if (gDisableDeactivation || (gDeactivationTime == 0.f))
380                 return false;
381         //2 == ISLAND_SLEEPING, 3 == WANTS_DEACTIVATION
382         if ( (m_body->GetActivationState() == 2) || (m_body->GetActivationState() == 3))
383                 return true;
384
385         if (m_body->m_deactivationTime> gDeactivationTime)
386         {
387                 return true;
388         }
389         return false;
390 }
391