3d20ce8a3a558d0a9067c4d687997c4f2579a76f
[blender.git] / source / gameengine / Ketsji / KX_BulletPhysicsController.cpp
1 //under visual studio the #define in KX_ConvertPhysicsObject.h is quicker for recompilation
2 #include "KX_ConvertPhysicsObject.h"
3
4 #ifdef USE_BULLET
5
6 #include "KX_BulletPhysicsController.h"
7
8 #include "btBulletDynamicsCommon.h"
9 #include "SG_Spatial.h"
10
11 #include "KX_GameObject.h"
12 #include "KX_MotionState.h"
13 #include "KX_ClientObjectInfo.h"
14
15 #include "PHY_IPhysicsEnvironment.h"
16 #include "CcdPhysicsEnvironment.h"
17 #include "BulletSoftBody/btSoftBody.h"
18
19
20 KX_BulletPhysicsController::KX_BulletPhysicsController (const CcdConstructionInfo& ci, bool dyna)
21 : KX_IPhysicsController(dyna,(PHY_IPhysicsController*)this),
22 CcdPhysicsController(ci),
23 m_savedCollisionFlags(0)
24 {
25
26 }
27         
28 KX_BulletPhysicsController::~KX_BulletPhysicsController ()
29 {
30         // The game object has a direct link to 
31         if (m_pObject)
32         {
33                 // If we cheat in SetObject, we must also cheat here otherwise the 
34                 // object will still things it has a physical controller
35                 // Note that it requires that m_pObject is reset in case the object is deleted
36                 // before the controller (usual case, see KX_Scene::RemoveNodeDestructObjec)
37                 // The non usual case is when the object is not deleted because its reference is hanging
38                 // in a AddObject actuator but the node is deleted. This case is covered here.
39                 KX_GameObject* gameobj = (KX_GameObject*)       m_pObject->GetSGClientObject();
40                 gameobj->SetPhysicsController(NULL,false);
41         }
42 }
43
44 void    KX_BulletPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
45 {
46         CcdPhysicsController::resolveCombinedVelocities(linvelX,linvelY,linvelZ,angVelX,angVelY,angVelZ);
47
48 }
49
50
51         ///////////////////////////////////
52         //      KX_IPhysicsController interface
53         ////////////////////////////////////
54
55 void    KX_BulletPhysicsController::applyImpulse(const MT_Point3& attach, const MT_Vector3& impulse)
56 {
57                 CcdPhysicsController::applyImpulse(attach[0],attach[1],attach[2],impulse[0],impulse[1],impulse[2]);
58
59 }
60
61 void    KX_BulletPhysicsController::SetObject (SG_IObject* object)
62 {
63         SG_Controller::SetObject(object);
64
65         // cheating here...
66         //should not be necessary, is it for duplicates ?
67
68         KX_GameObject* gameobj = (KX_GameObject*)       object->GetSGClientObject();
69         gameobj->SetPhysicsController(this,gameobj->IsDynamic());
70         CcdPhysicsController::setNewClientInfo(gameobj->getClientInfo());
71
72
73 }
74
75
76 void    KX_BulletPhysicsController::setMargin (float collisionMargin)
77 {
78         CcdPhysicsController::SetMargin(collisionMargin);
79 }
80 void    KX_BulletPhysicsController::RelativeTranslate(const MT_Vector3& dloc,bool local)
81 {
82         CcdPhysicsController::RelativeTranslate(dloc[0],dloc[1],dloc[2],local);
83
84 }
85
86 void    KX_BulletPhysicsController::RelativeRotate(const MT_Matrix3x3& drot,bool local)
87 {
88         float   rotval[12];
89         drot.getValue(rotval);
90         CcdPhysicsController::RelativeRotate(rotval,local);
91 }
92
93 void    KX_BulletPhysicsController::ApplyTorque(const MT_Vector3& torque,bool local)
94 {
95                 CcdPhysicsController::ApplyTorque(torque.x(),torque.y(),torque.z(),local);
96 }
97 void    KX_BulletPhysicsController::ApplyForce(const MT_Vector3& force,bool local)
98 {
99         CcdPhysicsController::ApplyForce(force.x(),force.y(),force.z(),local);
100 }
101 MT_Vector3 KX_BulletPhysicsController::GetLinearVelocity()
102 {
103         float angVel[3];
104         //CcdPhysicsController::GetAngularVelocity(angVel[0],angVel[1],angVel[2]);
105         CcdPhysicsController::GetLinearVelocity(angVel[0],angVel[1],angVel[2]);//rcruiz
106         return MT_Vector3(angVel[0],angVel[1],angVel[2]);
107 }
108 MT_Vector3 KX_BulletPhysicsController::GetAngularVelocity()
109 {
110         float angVel[3];
111         //CcdPhysicsController::GetAngularVelocity(angVel[0],angVel[1],angVel[2]);
112         CcdPhysicsController::GetAngularVelocity(angVel[0],angVel[1],angVel[2]);//rcruiz
113         return MT_Vector3(angVel[0],angVel[1],angVel[2]);
114 }
115 MT_Vector3 KX_BulletPhysicsController::GetVelocity(const MT_Point3& pos)
116 {
117         float linVel[3];
118         CcdPhysicsController::GetLinearVelocity(linVel[0],linVel[1],linVel[2]);
119         return MT_Vector3(linVel[0],linVel[1],linVel[2]);
120 }
121
122 void    KX_BulletPhysicsController::SetAngularVelocity(const MT_Vector3& ang_vel,bool local)
123 {
124         CcdPhysicsController::SetAngularVelocity(ang_vel.x(),ang_vel.y(),ang_vel.z(),local);
125
126 }
127 void    KX_BulletPhysicsController::SetLinearVelocity(const MT_Vector3& lin_vel,bool local)
128 {
129         CcdPhysicsController::SetLinearVelocity(lin_vel.x(),lin_vel.y(),lin_vel.z(),local);
130 }
131 void    KX_BulletPhysicsController::getOrientation(MT_Quaternion& orn)
132 {
133         float myorn[4];
134         CcdPhysicsController::getOrientation(myorn[0],myorn[1],myorn[2],myorn[3]);
135         orn = MT_Quaternion(myorn[0],myorn[1],myorn[2],myorn[3]);
136 }
137 void KX_BulletPhysicsController::setOrientation(const MT_Matrix3x3& orn)
138 {
139         btMatrix3x3 btmat(orn[0][0], orn[0][1], orn[0][2], orn[1][0], orn[1][1], orn[1][2], orn[2][0], orn[2][1], orn[2][2]);
140         CcdPhysicsController::setWorldOrientation(btmat);
141 }
142 void KX_BulletPhysicsController::setPosition(const MT_Point3& pos)
143 {
144         CcdPhysicsController::setPosition(pos.x(),pos.y(),pos.z());
145 }
146 void KX_BulletPhysicsController::setScaling(const MT_Vector3& scaling)
147 {
148         CcdPhysicsController::setScaling(scaling.x(),scaling.y(),scaling.z());
149 }
150 MT_Scalar       KX_BulletPhysicsController::GetMass()
151 {
152         if (GetSoftBody())
153                 return GetSoftBody()->getTotalMass();
154         
155         MT_Scalar invmass = 0.f;
156         if (GetRigidBody())
157                 invmass = GetRigidBody()->getInvMass();
158         if (invmass)
159                 return 1.f/invmass;
160         return 0.f;
161
162 }
163 MT_Vector3      KX_BulletPhysicsController::getReactionForce()
164 {
165         assert(0);
166         return MT_Vector3(0.f,0.f,0.f);
167 }
168 void    KX_BulletPhysicsController::setRigidBody(bool rigid)
169 {
170 }
171
172 void    KX_BulletPhysicsController::SuspendDynamics(bool ghost)
173 {
174         btRigidBody *body = GetRigidBody();
175         if (body && body->getActivationState() != DISABLE_SIMULATION)
176         {
177                 btBroadphaseProxy* handle = body->getBroadphaseHandle();
178                 m_savedCollisionFlags = body->getCollisionFlags();
179                 m_savedMass = GetMass();
180                 m_savedCollisionFilterGroup = handle->m_collisionFilterGroup;
181                 m_savedCollisionFilterMask = handle->m_collisionFilterMask;
182                 body->setActivationState(DISABLE_SIMULATION);
183                 GetPhysicsEnvironment()->updateCcdPhysicsController(this, 
184                         0.0,
185                         btCollisionObject::CF_STATIC_OBJECT|((ghost)?btCollisionObject::CF_NO_CONTACT_RESPONSE:(m_savedCollisionFlags&btCollisionObject::CF_NO_CONTACT_RESPONSE)),
186                         btBroadphaseProxy::StaticFilter, 
187                         btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::StaticFilter);
188         }
189 }
190
191 void    KX_BulletPhysicsController::RestoreDynamics()
192 {
193         btRigidBody *body = GetRigidBody();
194         if (body && body->getActivationState() == DISABLE_SIMULATION)
195         {
196                 GetPhysicsEnvironment()->updateCcdPhysicsController(this, 
197                         m_savedMass,
198                         m_savedCollisionFlags,
199                         m_savedCollisionFilterGroup,
200                         m_savedCollisionFilterMask);
201                 GetRigidBody()->forceActivationState(ACTIVE_TAG);
202         }
203 }
204
205 SG_Controller*  KX_BulletPhysicsController::GetReplica(class SG_Node* destnode)
206 {
207         PHY_IMotionState* motionstate = new KX_MotionState(destnode);
208
209         KX_BulletPhysicsController* physicsreplica = new KX_BulletPhysicsController(*this);
210
211         //parentcontroller is here be able to avoid collisions between parent/child
212
213         PHY_IPhysicsController* parentctrl = NULL;
214         KX_BulletPhysicsController* parentKxCtrl = NULL;
215         CcdPhysicsController* ccdParent = NULL;
216
217         
218         if (destnode != destnode->GetRootSGParent())
219         {
220                 KX_GameObject* clientgameobj = (KX_GameObject*) destnode->GetRootSGParent()->GetSGClientObject();
221                 if (clientgameobj)
222                 {
223                         parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
224                 } else
225                 {
226                         // it could be a false node, try the children
227                         NodeList::const_iterator childit;
228                         for (
229                                 childit = destnode->GetSGChildren().begin();
230                         childit!= destnode->GetSGChildren().end();
231                         ++childit
232                                 ) {
233                                 KX_GameObject *clientgameobj = static_cast<KX_GameObject*>( (*childit)->GetSGClientObject());
234                                 if (clientgameobj)
235                                 {
236                                         parentKxCtrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
237                                         parentctrl = parentKxCtrl;
238                                         ccdParent = parentKxCtrl;
239                                 }
240                         }
241                 }
242         }
243
244         physicsreplica->setParentCtrl(ccdParent);
245         physicsreplica->PostProcessReplica(motionstate,parentctrl);
246         physicsreplica->m_userdata = (PHY_IPhysicsController*)physicsreplica;
247         return physicsreplica;
248         
249 }
250
251
252
253 void    KX_BulletPhysicsController::SetSumoTransform(bool nondynaonly)
254 {
255         if (GetRigidBody())
256                 GetRigidBody()->activate(true);
257
258         if (!m_bDyna)
259         {
260                 GetCollisionObject()->setCollisionFlags(GetRigidBody()->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
261         } else
262         {
263                 if (!nondynaonly)
264                 {
265                         btTransform worldTrans;
266                         if (GetRigidBody())
267                         {
268                                 GetRigidBody()->getMotionState()->getWorldTransform(worldTrans);
269                                 GetRigidBody()->setCenterOfMassTransform(worldTrans);
270                         }
271                         
272                         /*
273                         scaling?
274                         if (m_bDyna)
275                         {
276                                 m_sumoObj->setScaling(MT_Vector3(1,1,1));
277                         } else
278                         {
279                                 MT_Vector3 scale;
280                                 GetWorldScaling(scale);
281                                 m_sumoObj->setScaling(scale);
282                         }
283                         */
284
285                 }
286         }
287 }
288
289 // todo: remove next line !
290 void    KX_BulletPhysicsController::SetSimulatedTime(double time)
291 {
292 }
293         
294 // call from scene graph to update
295 bool KX_BulletPhysicsController::Update(double time)
296 {
297         return false;
298
299         // todo: check this code
300         //if (GetMass())
301         //{
302         //      return false;//true;
303 //      }
304 //      return false;
305 }
306
307 #endif //#ifdef USE_BULLET