Filling in branch from trunk
[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
17
18 KX_BulletPhysicsController::KX_BulletPhysicsController (const CcdConstructionInfo& ci, bool dyna)
19 : KX_IPhysicsController(dyna,(PHY_IPhysicsController*)this),
20 CcdPhysicsController(ci)
21 {
22
23 }
24         
25 KX_BulletPhysicsController::~KX_BulletPhysicsController ()
26 {
27
28 }
29
30 void    KX_BulletPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
31 {
32         CcdPhysicsController::resolveCombinedVelocities(linvelX,linvelY,linvelZ,angVelX,angVelY,angVelZ);
33
34 }
35
36
37         ///////////////////////////////////
38         //      KX_IPhysicsController interface
39         ////////////////////////////////////
40
41 void    KX_BulletPhysicsController::applyImpulse(const MT_Point3& attach, const MT_Vector3& impulse)
42 {
43                 CcdPhysicsController::applyImpulse(attach[0],attach[1],attach[2],impulse[0],impulse[1],impulse[2]);
44
45 }
46
47 void    KX_BulletPhysicsController::SetObject (SG_IObject* object)
48 {
49         SG_Controller::SetObject(object);
50
51         // cheating here...
52         //should not be necessary, is it for duplicates ?
53
54         KX_GameObject* gameobj = (KX_GameObject*)       object->GetSGClientObject();
55         gameobj->SetPhysicsController(this,gameobj->IsDynamic());
56         CcdPhysicsController::setNewClientInfo(gameobj->getClientInfo());
57
58
59 }
60
61 void    KX_BulletPhysicsController::setMargin (float collisionMargin)
62 {
63         CcdPhysicsController::SetMargin(collisionMargin);
64 }
65 void    KX_BulletPhysicsController::RelativeTranslate(const MT_Vector3& dloc,bool local)
66 {
67         CcdPhysicsController::RelativeTranslate(dloc[0],dloc[1],dloc[2],local);
68
69 }
70
71 void    KX_BulletPhysicsController::RelativeRotate(const MT_Matrix3x3& drot,bool local)
72 {
73         float   rotval[12];
74         drot.getValue(rotval);
75         CcdPhysicsController::RelativeRotate(rotval,local);
76 }
77
78 void    KX_BulletPhysicsController::ApplyTorque(const MT_Vector3& torque,bool local)
79 {
80                 CcdPhysicsController::ApplyTorque(torque.x(),torque.y(),torque.z(),local);
81 }
82 void    KX_BulletPhysicsController::ApplyForce(const MT_Vector3& force,bool local)
83 {
84         CcdPhysicsController::ApplyForce(force.x(),force.y(),force.z(),local);
85 }
86 MT_Vector3 KX_BulletPhysicsController::GetLinearVelocity()
87 {
88         float angVel[3];
89         //CcdPhysicsController::GetAngularVelocity(angVel[0],angVel[1],angVel[2]);
90         CcdPhysicsController::GetLinearVelocity(angVel[0],angVel[1],angVel[2]);//rcruiz
91         return MT_Vector3(angVel[0],angVel[1],angVel[2]);
92 }
93 MT_Vector3 KX_BulletPhysicsController::GetVelocity(const MT_Point3& pos)
94 {
95         float linVel[3];
96         CcdPhysicsController::GetLinearVelocity(linVel[0],linVel[1],linVel[2]);
97         return MT_Vector3(linVel[0],linVel[1],linVel[2]);
98 }
99
100 void    KX_BulletPhysicsController::SetAngularVelocity(const MT_Vector3& ang_vel,bool local)
101 {
102         CcdPhysicsController::SetAngularVelocity(ang_vel.x(),ang_vel.y(),ang_vel.z(),local);
103
104 }
105 void    KX_BulletPhysicsController::SetLinearVelocity(const MT_Vector3& lin_vel,bool local)
106 {
107         CcdPhysicsController::SetLinearVelocity(lin_vel.x(),lin_vel.y(),lin_vel.z(),local);
108 }
109 void    KX_BulletPhysicsController::getOrientation(MT_Quaternion& orn)
110 {
111         float myorn[4];
112         CcdPhysicsController::getOrientation(myorn[0],myorn[1],myorn[2],myorn[3]);
113         orn = MT_Quaternion(myorn[0],myorn[1],myorn[2],myorn[3]);
114 }
115 void KX_BulletPhysicsController::setOrientation(const MT_Quaternion& orn)
116 {
117         CcdPhysicsController::setOrientation(orn.x(),orn.y(),orn.z(),orn.w());
118 }
119 void KX_BulletPhysicsController::setPosition(const MT_Point3& pos)
120 {
121         CcdPhysicsController::setPosition(pos.x(),pos.y(),pos.z());
122 }
123 void KX_BulletPhysicsController::setScaling(const MT_Vector3& scaling)
124 {
125         CcdPhysicsController::setScaling(scaling.x(),scaling.y(),scaling.z());
126 }
127 MT_Scalar       KX_BulletPhysicsController::GetMass()
128 {
129
130         MT_Scalar invmass = GetRigidBody()->getInvMass();
131         if (invmass)
132                 return 1.f/invmass;
133         return 0.f;
134
135 }
136 MT_Vector3      KX_BulletPhysicsController::getReactionForce()
137 {
138         assert(0);
139         return MT_Vector3(0.f,0.f,0.f);
140 }
141 void    KX_BulletPhysicsController::setRigidBody(bool rigid)
142 {
143 }
144
145 void    KX_BulletPhysicsController::SuspendDynamics()
146 {
147         GetRigidBody()->setActivationState(DISABLE_SIMULATION);
148
149 }
150 void    KX_BulletPhysicsController::RestoreDynamics()
151 {
152         GetRigidBody()->forceActivationState(ACTIVE_TAG);
153 }
154
155 SG_Controller*  KX_BulletPhysicsController::GetReplica(class SG_Node* destnode)
156 {
157         PHY_IMotionState* motionstate = new KX_MotionState(destnode);
158
159         KX_BulletPhysicsController* physicsreplica = new KX_BulletPhysicsController(*this);
160
161         //parentcontroller is here be able to avoid collisions between parent/child
162
163         PHY_IPhysicsController* parentctrl = NULL;
164         
165         if (destnode != destnode->GetRootSGParent())
166         {
167                 KX_GameObject* clientgameobj = (KX_GameObject*) destnode->GetRootSGParent()->GetSGClientObject();
168                 if (clientgameobj)
169                 {
170                         parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
171                 } else
172                 {
173                         // it could be a false node, try the children
174                         NodeList::const_iterator childit;
175                         for (
176                                 childit = destnode->GetSGChildren().begin();
177                         childit!= destnode->GetSGChildren().end();
178                         ++childit
179                                 ) {
180                                 KX_GameObject *clientgameobj = static_cast<KX_GameObject*>( (*childit)->GetSGClientObject());
181                                 if (clientgameobj)
182                                 {
183                                         parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
184                                 }
185                         }
186                 }
187         }
188
189         physicsreplica->PostProcessReplica(motionstate,parentctrl);
190         physicsreplica->m_userdata = (PHY_IPhysicsController*)physicsreplica;
191         return physicsreplica;
192         
193 }
194
195
196
197 void    KX_BulletPhysicsController::SetSumoTransform(bool nondynaonly)
198 {
199         GetRigidBody()->activate(true);
200
201         if (!m_bDyna)
202         {
203                 GetRigidBody()->setCollisionFlags(GetRigidBody()->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
204         } else
205         {
206                 if (!nondynaonly)
207                 {
208                         btTransform worldTrans;
209                         GetRigidBody()->getMotionState()->getWorldTransform(worldTrans);
210                         GetRigidBody()->setCenterOfMassTransform(worldTrans);
211                         
212                         /*
213                         scaling?
214                         if (m_bDyna)
215                         {
216                                 m_sumoObj->setScaling(MT_Vector3(1,1,1));
217                         } else
218                         {
219                                 MT_Vector3 scale;
220                                 GetWorldScaling(scale);
221                                 m_sumoObj->setScaling(scale);
222                         }
223                         */
224
225                 }
226         }
227 }
228
229 // todo: remove next line !
230 void    KX_BulletPhysicsController::SetSimulatedTime(double time)
231 {
232 }
233         
234 // call from scene graph to update
235 bool KX_BulletPhysicsController::Update(double time)
236 {
237         return false;
238
239         // todo: check this code
240         //if (GetMass())
241         //{
242         //      return false;//true;
243 //      }
244 //      return false;
245 }
246
247 #endif //#ifdef USE_BULLET