7631ee05b0b83d053282b493662321ebc9c814c1
[blender.git] / source / gameengine / Ketsji / KX_SumoPhysicsController.cpp
1 #include "KX_ConvertPhysicsObject.h"
2
3 #ifdef USE_SUMO_SOLID
4
5 #ifdef WIN32
6 #pragma warning (disable : 4786)
7 #endif
8
9 #include "KX_SumoPhysicsController.h"
10 #include "SG_Spatial.h"
11 #include "SM_Scene.h"
12 #include "KX_GameObject.h"
13 #include "KX_MotionState.h"
14 #include "KX_ClientObjectInfo.h"
15
16 #include "PHY_IPhysicsEnvironment.h"
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 void    KX_SumoPhysicsController::applyImpulse(const MT_Point3& attach, const MT_Vector3& impulse)
23 {
24         SumoPhysicsController::applyImpulse(attach[0],attach[1],attach[2],impulse[0],impulse[1],impulse[2]);
25 }
26 void    KX_SumoPhysicsController::RelativeTranslate(const MT_Vector3& dloc,bool local)
27 {
28         SumoPhysicsController::RelativeTranslate(dloc[0],dloc[1],dloc[2],local);
29
30 }
31 void    KX_SumoPhysicsController::RelativeRotate(const MT_Matrix3x3& drot,bool local)
32 {
33         float oldmat[12];
34         drot.getValue(oldmat);
35 /*      float newmat[9];
36         float *m = &newmat[0];
37         double *orgm = &oldmat[0];
38
39          *m++ = *orgm++;*m++ = *orgm++;*m++ = *orgm++;orgm++;
40          *m++ = *orgm++;*m++ = *orgm++;*m++ = *orgm++;orgm++;
41          *m++ = *orgm++;*m++ = *orgm++;*m++ = *orgm++;orgm++; */
42
43         SumoPhysicsController::RelativeRotate(oldmat,local);
44 }
45
46 void    KX_SumoPhysicsController::SetLinearVelocity(const MT_Vector3& lin_vel,bool local)
47 {
48         SumoPhysicsController::SetLinearVelocity(lin_vel[0],lin_vel[1],lin_vel[2],local);
49
50 }
51
52 void    KX_SumoPhysicsController::SetAngularVelocity(const MT_Vector3& ang_vel,bool local)
53 {
54         SumoPhysicsController::SetAngularVelocity(ang_vel[0],ang_vel[1],ang_vel[2],local);
55 }
56
57 MT_Vector3 KX_SumoPhysicsController::GetVelocity(const MT_Point3& pos)
58 {
59
60         float linvel[3];
61         SumoPhysicsController::GetVelocity(pos[0],pos[1],pos[2],linvel[0],linvel[1],linvel[2]);
62
63         return MT_Vector3 (linvel);
64 }
65
66 MT_Vector3 KX_SumoPhysicsController::GetLinearVelocity()
67 {
68         return GetVelocity(MT_Point3(0,0,0));
69         
70 }
71
72 void            KX_SumoPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
73 {
74         SumoPhysicsController::resolveCombinedVelocities(linvelX,linvelY,linvelZ,angVelX,angVelY,angVelZ);
75 }
76
77 void    KX_SumoPhysicsController::ApplyTorque(const MT_Vector3& torque,bool local)
78 {
79         SumoPhysicsController::ApplyTorque(torque[0],torque[1],torque[2],local);
80
81 }
82
83 void    KX_SumoPhysicsController::ApplyForce(const MT_Vector3& force,bool local)
84 {
85         SumoPhysicsController::ApplyForce(force[0],force[1],force[2],local);
86 }
87
88 bool KX_SumoPhysicsController::Update(double time)
89 {
90         return SynchronizeMotionStates(time); 
91 }
92
93 void    KX_SumoPhysicsController::SetSimulatedTime(double time)
94 {
95         
96 }
97
98 void    KX_SumoPhysicsController::SetSumoTransform(bool nondynaonly)
99 {
100         SumoPhysicsController::setSumoTransform(nondynaonly);
101
102 }
103
104 void    KX_SumoPhysicsController::SuspendDynamics(bool)
105 {
106         SumoPhysicsController::SuspendDynamics();
107 }
108
109 void    KX_SumoPhysicsController::RestoreDynamics()
110 {
111         SumoPhysicsController::RestoreDynamics();
112 }
113
114 SG_Controller*  KX_SumoPhysicsController::GetReplica(SG_Node* destnode)
115 {
116
117         PHY_IMotionState* motionstate = new KX_MotionState(destnode);
118
119         KX_SumoPhysicsController* physicsreplica = new KX_SumoPhysicsController(*this);
120
121         //parentcontroller is here be able to avoid collisions between parent/child
122
123         PHY_IPhysicsController* parentctrl = NULL;
124         
125         if (destnode != destnode->GetRootSGParent())
126         {
127                 KX_GameObject* clientgameobj = (KX_GameObject*) destnode->GetRootSGParent()->GetSGClientObject();
128                 if (clientgameobj)
129                 {
130                         parentctrl = (KX_SumoPhysicsController*)clientgameobj->GetPhysicsController();
131                 } else
132                 {
133                         // it could be a false node, try the children
134                         NodeList::const_iterator childit;
135                         for (
136                                 childit = destnode->GetSGChildren().begin();
137                         childit!= destnode->GetSGChildren().end();
138                         ++childit
139                                 ) {
140                                 KX_GameObject *clientgameobj = static_cast<KX_GameObject*>( (*childit)->GetSGClientObject());
141                                 if (clientgameobj)
142                                 {
143                                         parentctrl = (KX_SumoPhysicsController*)clientgameobj->GetPhysicsController();
144                                 }
145                         }
146                 }
147         }
148
149         physicsreplica->PostProcessReplica(motionstate,parentctrl);
150
151         return physicsreplica;
152 }
153
154
155 void    KX_SumoPhysicsController::SetObject (SG_IObject* object)
156 {
157         SG_Controller::SetObject(object);
158
159         // cheating here...
160 //should not be necessary, is it for duplicates ?
161
162 KX_GameObject* gameobj = (KX_GameObject*)       object->GetSGClientObject();
163 gameobj->SetPhysicsController(this,gameobj->IsDynamic());
164 GetSumoObject()->setClientObject(gameobj->getClientInfo());
165 }
166
167 void    KX_SumoPhysicsController::setMargin(float collisionMargin)
168 {
169         SumoPhysicsController::SetMargin(collisionMargin);
170 }
171
172
173 void KX_SumoPhysicsController::setOrientation(const MT_Matrix3x3& rot)
174 {
175         MT_Quaternion orn = rot.getRotation();
176         SumoPhysicsController::setOrientation(
177                 orn[0],orn[1],orn[2],orn[3]);
178
179 }
180 void KX_SumoPhysicsController::getOrientation(MT_Quaternion& orn)
181 {
182
183         float quat[4];
184
185         SumoPhysicsController::getOrientation(quat[0],quat[1],quat[2],quat[3]);
186
187         orn = MT_Quaternion(quat);
188
189 }
190
191 void KX_SumoPhysicsController::setPosition(const MT_Point3& pos)
192 {
193         SumoPhysicsController::setPosition(pos[0],pos[1],pos[2]);
194         
195 }
196         
197 void KX_SumoPhysicsController::setScaling(const MT_Vector3& scaling)
198 {
199         SumoPhysicsController::setScaling(scaling[0],scaling[1],scaling[2]);
200         
201 }
202
203 MT_Scalar       KX_SumoPhysicsController::GetMass()
204 {
205         return SumoPhysicsController::getMass();
206 }
207
208 void    KX_SumoPhysicsController::SetMass(MT_Scalar newmass)
209 {
210 }
211
212 MT_Scalar       KX_SumoPhysicsController::GetRadius()
213 {
214         return SumoPhysicsController::GetRadius();
215 }
216
217 MT_Vector3      KX_SumoPhysicsController::getReactionForce()
218 {
219         float force[3];
220         SumoPhysicsController::getReactionForce(force[0],force[1],force[2]);
221         return MT_Vector3(force);
222
223 }
224
225 void    KX_SumoPhysicsController::setRigidBody(bool rigid)
226 {
227         SumoPhysicsController::setRigidBody(rigid);
228         
229 }
230
231
232 KX_SumoPhysicsController::~KX_SumoPhysicsController()
233 {
234
235         
236 }
237
238
239 #endif//USE_SUMO_SOLID