workaround for a irregularity/bug in physics system (happens very seldom, just 1...
[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         GetRigidBody()->SetActivationState(DISABLE_SIMULATION);
148
149 }
150 void    KX_BulletPhysicsController::RestoreDynamics()
151 {
152         GetRigidBody()->SetActivationState(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
191         return physicsreplica;
192         
193 }
194
195
196
197 void    KX_BulletPhysicsController::SetSumoTransform(bool nondynaonly)
198 {
199 }
200
201 // todo: remove next line !
202 void    KX_BulletPhysicsController::SetSimulatedTime(double time)
203 {
204 }
205         
206 // call from scene graph to update
207 bool KX_BulletPhysicsController::Update(double time)
208 {
209         return false;
210
211         // todo: check this code
212         //if (GetMass())
213         //{
214         //      return false;//true;
215 //      }
216 //      return false;
217 }
218
219 #endif //#ifdef USE_BULLET