Orange; merger with bf-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 "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 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         //GetSumoObject()->setClientObject(gameobj->getClientInfo());
57
58
59 }
60
61 void    KX_BulletPhysicsController::RelativeTranslate(const MT_Vector3& dloc,bool local)
62 {
63         CcdPhysicsController::RelativeTranslate(dloc[0],dloc[1],dloc[2],local);
64
65 }
66
67 void    KX_BulletPhysicsController::RelativeRotate(const MT_Matrix3x3& drot,bool local)
68 {
69         float   rotval[12];
70         drot.getValue(rotval);
71         CcdPhysicsController::RelativeRotate(rotval,local);
72 }
73
74 void    KX_BulletPhysicsController::ApplyTorque(const MT_Vector3& torque,bool local)
75 {
76                 CcdPhysicsController::ApplyTorque(torque.x(),torque.y(),torque.z(),local);
77 }
78 void    KX_BulletPhysicsController::ApplyForce(const MT_Vector3& force,bool local)
79 {
80         CcdPhysicsController::ApplyForce(force.x(),force.y(),force.z(),local);
81 }
82 MT_Vector3 KX_BulletPhysicsController::GetLinearVelocity()
83 {
84         float angVel[3];
85         CcdPhysicsController::GetAngularVelocity(angVel[0],angVel[1],angVel[2]);
86         return MT_Vector3(angVel[0],angVel[1],angVel[2]);
87 }
88 MT_Vector3 KX_BulletPhysicsController::GetVelocity(const MT_Point3& pos)
89 {
90         float linVel[3];
91         CcdPhysicsController::GetLinearVelocity(linVel[0],linVel[1],linVel[2]);
92         return MT_Vector3(linVel[0],linVel[1],linVel[2]);
93 }
94
95 void    KX_BulletPhysicsController::SetAngularVelocity(const MT_Vector3& ang_vel,bool local)
96 {
97         CcdPhysicsController::SetAngularVelocity(ang_vel.x(),ang_vel.y(),ang_vel.z(),local);
98
99 }
100 void    KX_BulletPhysicsController::SetLinearVelocity(const MT_Vector3& lin_vel,bool local)
101 {
102         CcdPhysicsController::SetLinearVelocity(lin_vel.x(),lin_vel.y(),lin_vel.z(),local);
103 }
104 void    KX_BulletPhysicsController::getOrientation(MT_Quaternion& orn)
105 {
106         float myorn[4];
107         CcdPhysicsController::getOrientation(myorn[0],myorn[1],myorn[2],myorn[3]);
108         orn = MT_Quaternion(myorn[0],myorn[1],myorn[2],myorn[3]);
109 }
110 void KX_BulletPhysicsController::setOrientation(const MT_Quaternion& orn)
111 {
112         CcdPhysicsController::setOrientation(orn.x(),orn.y(),orn.z(),orn.w());
113 }
114 void KX_BulletPhysicsController::setPosition(const MT_Point3& pos)
115 {
116         CcdPhysicsController::setPosition(pos.x(),pos.y(),pos.z());
117 }
118 void KX_BulletPhysicsController::setScaling(const MT_Vector3& scaling)
119 {
120         CcdPhysicsController::setScaling(scaling.x(),scaling.y(),scaling.z());
121 }
122 MT_Scalar       KX_BulletPhysicsController::GetMass()
123 {
124
125         MT_Scalar invmass = GetRigidBody()->getInvMass();
126         if (invmass)
127                 return 1.f/invmass;
128         return 0.f;
129
130 }
131 MT_Vector3      KX_BulletPhysicsController::getReactionForce()
132 {
133         assert(0);
134         return MT_Vector3(0.f,0.f,0.f);
135 }
136 void    KX_BulletPhysicsController::setRigidBody(bool rigid)
137 {
138 }
139
140 void    KX_BulletPhysicsController::SuspendDynamics()
141 {
142
143 }
144 void    KX_BulletPhysicsController::RestoreDynamics()
145 {
146 }
147
148 SG_Controller*  KX_BulletPhysicsController::GetReplica(class SG_Node* destnode)
149 {
150         PHY_IMotionState* motionstate = new KX_MotionState(destnode);
151
152         KX_BulletPhysicsController* physicsreplica = new KX_BulletPhysicsController(*this);
153
154         //parentcontroller is here be able to avoid collisions between parent/child
155
156         PHY_IPhysicsController* parentctrl = NULL;
157         
158         if (destnode != destnode->GetRootSGParent())
159         {
160                 KX_GameObject* clientgameobj = (KX_GameObject*) destnode->GetRootSGParent()->GetSGClientObject();
161                 if (clientgameobj)
162                 {
163                         parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
164                 } else
165                 {
166                         // it could be a false node, try the children
167                         NodeList::const_iterator childit;
168                         for (
169                                 childit = destnode->GetSGChildren().begin();
170                         childit!= destnode->GetSGChildren().end();
171                         ++childit
172                                 ) {
173                                 KX_GameObject *clientgameobj = static_cast<KX_GameObject*>( (*childit)->GetSGClientObject());
174                                 if (clientgameobj)
175                                 {
176                                         parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
177                                 }
178                         }
179                 }
180         }
181
182         physicsreplica->PostProcessReplica(motionstate,parentctrl);
183
184         return physicsreplica;
185         
186 }
187
188
189
190 void    KX_BulletPhysicsController::SetSumoTransform(bool nondynaonly)
191 {
192 }
193
194 // todo: remove next line !
195 void    KX_BulletPhysicsController::SetSimulatedTime(double time)
196 {
197 }
198         
199 // call from scene graph to update
200 bool KX_BulletPhysicsController::Update(double time)
201 {
202         return false;
203
204         // todo: check this code
205         //if (GetMass())
206         //{
207         //      return false;//true;
208 //      }
209 //      return false;
210 }
211
212 #endif //#ifdef USE_BULLET