svn merge -r 16667:16741 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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         
215         if (destnode != destnode->GetRootSGParent())
216         {
217                 KX_GameObject* clientgameobj = (KX_GameObject*) destnode->GetRootSGParent()->GetSGClientObject();
218                 if (clientgameobj)
219                 {
220                         parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
221                 } else
222                 {
223                         // it could be a false node, try the children
224                         NodeList::const_iterator childit;
225                         for (
226                                 childit = destnode->GetSGChildren().begin();
227                         childit!= destnode->GetSGChildren().end();
228                         ++childit
229                                 ) {
230                                 KX_GameObject *clientgameobj = static_cast<KX_GameObject*>( (*childit)->GetSGClientObject());
231                                 if (clientgameobj)
232                                 {
233                                         parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
234                                 }
235                         }
236                 }
237         }
238
239         physicsreplica->PostProcessReplica(motionstate,parentctrl);
240         physicsreplica->m_userdata = (PHY_IPhysicsController*)physicsreplica;
241         return physicsreplica;
242         
243 }
244
245
246
247 void    KX_BulletPhysicsController::SetSumoTransform(bool nondynaonly)
248 {
249         if (GetRigidBody())
250                 GetRigidBody()->activate(true);
251
252         if (!m_bDyna)
253         {
254                 GetCollisionObject()->setCollisionFlags(GetRigidBody()->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
255         } else
256         {
257                 if (!nondynaonly)
258                 {
259                         btTransform worldTrans;
260                         if (GetRigidBody())
261                         {
262                                 GetRigidBody()->getMotionState()->getWorldTransform(worldTrans);
263                                 GetRigidBody()->setCenterOfMassTransform(worldTrans);
264                         }
265                         
266                         /*
267                         scaling?
268                         if (m_bDyna)
269                         {
270                                 m_sumoObj->setScaling(MT_Vector3(1,1,1));
271                         } else
272                         {
273                                 MT_Vector3 scale;
274                                 GetWorldScaling(scale);
275                                 m_sumoObj->setScaling(scale);
276                         }
277                         */
278
279                 }
280         }
281 }
282
283 // todo: remove next line !
284 void    KX_BulletPhysicsController::SetSimulatedTime(double time)
285 {
286 }
287         
288 // call from scene graph to update
289 bool KX_BulletPhysicsController::Update(double time)
290 {
291         return false;
292
293         // todo: check this code
294         //if (GetMass())
295         //{
296         //      return false;//true;
297 //      }
298 //      return false;
299 }
300
301 #endif //#ifdef USE_BULLET