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