d28a8f40d6f4c7a7d06b3ccc9a6c27ec2da34b61
[blender-staging.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         return MT_Vector3(angVel[0],angVel[1],angVel[2]);
91 }
92 MT_Vector3 KX_BulletPhysicsController::GetVelocity(const MT_Point3& pos)
93 {
94         float linVel[3];
95         CcdPhysicsController::GetLinearVelocity(linVel[0],linVel[1],linVel[2]);
96         return MT_Vector3(linVel[0],linVel[1],linVel[2]);
97 }
98
99 void    KX_BulletPhysicsController::SetAngularVelocity(const MT_Vector3& ang_vel,bool local)
100 {
101         CcdPhysicsController::SetAngularVelocity(ang_vel.x(),ang_vel.y(),ang_vel.z(),local);
102
103 }
104 void    KX_BulletPhysicsController::SetLinearVelocity(const MT_Vector3& lin_vel,bool local)
105 {
106         CcdPhysicsController::SetLinearVelocity(lin_vel.x(),lin_vel.y(),lin_vel.z(),local);
107 }
108 void    KX_BulletPhysicsController::getOrientation(MT_Quaternion& orn)
109 {
110         float myorn[4];
111         CcdPhysicsController::getOrientation(myorn[0],myorn[1],myorn[2],myorn[3]);
112         orn = MT_Quaternion(myorn[0],myorn[1],myorn[2],myorn[3]);
113 }
114 void KX_BulletPhysicsController::setOrientation(const MT_Quaternion& orn)
115 {
116         CcdPhysicsController::setOrientation(orn.x(),orn.y(),orn.z(),orn.w());
117 }
118 void KX_BulletPhysicsController::setPosition(const MT_Point3& pos)
119 {
120         CcdPhysicsController::setPosition(pos.x(),pos.y(),pos.z());
121 }
122 void KX_BulletPhysicsController::setScaling(const MT_Vector3& scaling)
123 {
124         CcdPhysicsController::setScaling(scaling.x(),scaling.y(),scaling.z());
125 }
126 MT_Scalar       KX_BulletPhysicsController::GetMass()
127 {
128
129         MT_Scalar invmass = GetRigidBody()->getInvMass();
130         if (invmass)
131                 return 1.f/invmass;
132         return 0.f;
133
134 }
135 MT_Vector3      KX_BulletPhysicsController::getReactionForce()
136 {
137         assert(0);
138         return MT_Vector3(0.f,0.f,0.f);
139 }
140 void    KX_BulletPhysicsController::setRigidBody(bool rigid)
141 {
142 }
143
144 void    KX_BulletPhysicsController::SuspendDynamics()
145 {
146         GetRigidBody()->setActivationState(DISABLE_SIMULATION);
147
148 }
149 void    KX_BulletPhysicsController::RestoreDynamics()
150 {
151         GetRigidBody()->forceActivationState(ACTIVE_TAG);
152 }
153
154 SG_Controller*  KX_BulletPhysicsController::GetReplica(class SG_Node* destnode)
155 {
156         PHY_IMotionState* motionstate = new KX_MotionState(destnode);
157
158         KX_BulletPhysicsController* physicsreplica = new KX_BulletPhysicsController(*this);
159
160         //parentcontroller is here be able to avoid collisions between parent/child
161
162         PHY_IPhysicsController* parentctrl = NULL;
163         
164         if (destnode != destnode->GetRootSGParent())
165         {
166                 KX_GameObject* clientgameobj = (KX_GameObject*) destnode->GetRootSGParent()->GetSGClientObject();
167                 if (clientgameobj)
168                 {
169                         parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
170                 } else
171                 {
172                         // it could be a false node, try the children
173                         NodeList::const_iterator childit;
174                         for (
175                                 childit = destnode->GetSGChildren().begin();
176                         childit!= destnode->GetSGChildren().end();
177                         ++childit
178                                 ) {
179                                 KX_GameObject *clientgameobj = static_cast<KX_GameObject*>( (*childit)->GetSGClientObject());
180                                 if (clientgameobj)
181                                 {
182                                         parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
183                                 }
184                         }
185                 }
186         }
187
188         physicsreplica->PostProcessReplica(motionstate,parentctrl);
189         physicsreplica->m_userdata = (PHY_IPhysicsController*)physicsreplica;
190         return physicsreplica;
191         
192 }
193
194
195
196 void    KX_BulletPhysicsController::SetSumoTransform(bool nondynaonly)
197 {
198 }
199
200 // todo: remove next line !
201 void    KX_BulletPhysicsController::SetSimulatedTime(double time)
202 {
203 }
204         
205 // call from scene graph to update
206 bool KX_BulletPhysicsController::Update(double time)
207 {
208         return false;
209
210         // todo: check this code
211         //if (GetMass())
212         //{
213         //      return false;//true;
214 //      }
215 //      return false;
216 }
217
218 #endif //#ifdef USE_BULLET