c974b9b6828b33c33a57a28221d8f83b276c2650
[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 "Dynamics/RigidBody.h"
9
10 #include "SG_Spatial.h"
11
12 #include "KX_GameObject.h"
13 #include "KX_MotionState.h"
14 #include "KX_ClientObjectInfo.h"
15
16 #include "PHY_IPhysicsEnvironment.h"
17
18
19 KX_BulletPhysicsController::KX_BulletPhysicsController (const CcdConstructionInfo& ci, bool dyna)
20 : KX_IPhysicsController(dyna,(PHY_IPhysicsController*)this),
21 CcdPhysicsController(ci)
22 {
23
24 }
25         
26 KX_BulletPhysicsController::~KX_BulletPhysicsController ()
27 {
28
29 }
30
31 void    KX_BulletPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
32 {
33         CcdPhysicsController::resolveCombinedVelocities(linvelX,linvelY,linvelZ,angVelX,angVelY,angVelZ);
34
35 }
36
37
38         ///////////////////////////////////
39         //      KX_IPhysicsController interface
40         ////////////////////////////////////
41
42 void    KX_BulletPhysicsController::applyImpulse(const MT_Point3& attach, const MT_Vector3& impulse)
43 {
44                 CcdPhysicsController::applyImpulse(attach[0],attach[1],attach[2],impulse[0],impulse[1],impulse[2]);
45
46 }
47
48 void    KX_BulletPhysicsController::SetObject (SG_IObject* object)
49 {
50         SG_Controller::SetObject(object);
51
52         // cheating here...
53         //should not be necessary, is it for duplicates ?
54
55         KX_GameObject* gameobj = (KX_GameObject*)       object->GetSGClientObject();
56         gameobj->SetPhysicsController(this,gameobj->IsDynamic());
57         CcdPhysicsController::setNewClientInfo(gameobj->getClientInfo());
58
59
60 }
61
62 void    KX_BulletPhysicsController::SetMargin (float collisionMargin)
63 {
64         CcdPhysicsController::SetMargin(collisionMargin);
65 }
66 void    KX_BulletPhysicsController::RelativeTranslate(const MT_Vector3& dloc,bool local)
67 {
68         CcdPhysicsController::RelativeTranslate(dloc[0],dloc[1],dloc[2],local);
69
70 }
71
72 void    KX_BulletPhysicsController::RelativeRotate(const MT_Matrix3x3& drot,bool local)
73 {
74         float   rotval[12];
75         drot.getValue(rotval);
76         CcdPhysicsController::RelativeRotate(rotval,local);
77 }
78
79 void    KX_BulletPhysicsController::ApplyTorque(const MT_Vector3& torque,bool local)
80 {
81                 CcdPhysicsController::ApplyTorque(torque.x(),torque.y(),torque.z(),local);
82 }
83 void    KX_BulletPhysicsController::ApplyForce(const MT_Vector3& force,bool local)
84 {
85         CcdPhysicsController::ApplyForce(force.x(),force.y(),force.z(),local);
86 }
87 MT_Vector3 KX_BulletPhysicsController::GetLinearVelocity()
88 {
89         float angVel[3];
90         CcdPhysicsController::GetAngularVelocity(angVel[0],angVel[1],angVel[2]);
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
148 }
149 void    KX_BulletPhysicsController::RestoreDynamics()
150 {
151 }
152
153 SG_Controller*  KX_BulletPhysicsController::GetReplica(class SG_Node* destnode)
154 {
155         PHY_IMotionState* motionstate = new KX_MotionState(destnode);
156
157         KX_BulletPhysicsController* physicsreplica = new KX_BulletPhysicsController(*this);
158
159         //parentcontroller is here be able to avoid collisions between parent/child
160
161         PHY_IPhysicsController* parentctrl = NULL;
162         
163         if (destnode != destnode->GetRootSGParent())
164         {
165                 KX_GameObject* clientgameobj = (KX_GameObject*) destnode->GetRootSGParent()->GetSGClientObject();
166                 if (clientgameobj)
167                 {
168                         parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
169                 } else
170                 {
171                         // it could be a false node, try the children
172                         NodeList::const_iterator childit;
173                         for (
174                                 childit = destnode->GetSGChildren().begin();
175                         childit!= destnode->GetSGChildren().end();
176                         ++childit
177                                 ) {
178                                 KX_GameObject *clientgameobj = static_cast<KX_GameObject*>( (*childit)->GetSGClientObject());
179                                 if (clientgameobj)
180                                 {
181                                         parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
182                                 }
183                         }
184                 }
185         }
186
187         physicsreplica->PostProcessReplica(motionstate,parentctrl);
188
189         return physicsreplica;
190         
191 }
192
193
194
195 void    KX_BulletPhysicsController::SetSumoTransform(bool nondynaonly)
196 {
197 }
198
199 // todo: remove next line !
200 void    KX_BulletPhysicsController::SetSimulatedTime(double time)
201 {
202 }
203         
204 // call from scene graph to update
205 bool KX_BulletPhysicsController::Update(double time)
206 {
207         return false;
208
209         // todo: check this code
210         //if (GetMass())
211         //{
212         //      return false;//true;
213 //      }
214 //      return false;
215 }
216
217 #endif //#ifdef USE_BULLET