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