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