merge with 2.5 at r18679
[blender.git] / source / gameengine / Ketsji / KX_OdePhysicsController.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * The contents of this file may be used under the terms of either the GNU
7  * General Public License Version 2 or later (the "GPL", see
8  * http://www.gnu.org/licenses/gpl.html ), or the Blender License 1.0 or
9  * later (the "BL", see http://www.blender.org/BL/ ) which has to be
10  * bought from the Blender Foundation to become active, in which case the
11  * above mentioned GPL option does not apply.
12  *
13  * The Original Code is Copyright (C) 2002 by NaN Holding BV.
14  * All rights reserved.
15  *
16  * The Original Code is: all of this file.
17  *
18  * Contributor(s): none yet.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22 #include "KX_ConvertPhysicsObject.h"
23
24 #ifdef USE_ODE
25
26 #include "KX_OdePhysicsController.h"
27 #include "KX_GameObject.h"
28 #include "KX_MotionState.h"
29
30 #include "MT_assert.h"
31
32 #include "PHY_IPhysicsEnvironment.h"
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 KX_OdePhysicsController::KX_OdePhysicsController(
39                                                                                                  bool dyna,
40                                                                                                  bool fullRigidBody,
41                                                                                                  bool phantom,
42                                                                                                  class PHY_IMotionState* motionstate,
43                                                                                                  struct dxSpace* space,
44                                                                                                  struct dxWorld*        world,
45                                                                                                  float  mass,
46                                                                                                  float  friction,
47                                                                                                  float  restitution,
48                                                                                                  bool   implicitsphere,
49                                                                                                  float  center[3],
50                                                                                                  float  extends[3],
51                                                                                                  float  radius
52                                                                                                  ) 
53 : KX_IPhysicsController(dyna,false,(PHY_IPhysicsController*)this),
54 ODEPhysicsController(
55 dyna,fullRigidBody,phantom,motionstate,
56 space,world,mass,friction,restitution,
57 implicitsphere,center,extends,radius)
58 {
59 };
60
61
62 bool    KX_OdePhysicsController::Update(double time)
63 {
64         return SynchronizeMotionStates(time);
65 }
66
67 void                    KX_OdePhysicsController::SetObject (SG_IObject* object)
68 {
69         SG_Controller::SetObject(object);
70
71         // cheating here...
72         KX_GameObject* gameobj = (KX_GameObject*)       object->GetSGClientObject();
73         gameobj->SetPhysicsController(this);
74         
75 }
76
77
78
79 void    KX_OdePhysicsController::applyImpulse(const MT_Point3& attach, const MT_Vector3& impulse)
80 {
81         ODEPhysicsController::applyImpulse(attach[0],attach[1],attach[2],impulse[0],impulse[1],impulse[2]);
82 }
83         
84
85
86 void    KX_OdePhysicsController::RelativeTranslate(const MT_Vector3& dloc,bool local)
87 {
88         ODEPhysicsController::RelativeTranslate(dloc[0],dloc[1],dloc[2],local);
89
90 }
91 void    KX_OdePhysicsController::RelativeRotate(const MT_Matrix3x3& drot,bool local)
92 {
93         double oldmat[12];
94         drot.getValue(oldmat);
95         float newmat[9];
96         float *m = &newmat[0];
97         double *orgm = &oldmat[0];
98
99          *m++ = *orgm++;*m++ = *orgm++;*m++ = *orgm++;orgm++;
100          *m++ = *orgm++;*m++ = *orgm++;*m++ = *orgm++;orgm++;
101          *m++ = *orgm++;*m++ = *orgm++;*m++ = *orgm++;orgm++;
102
103          ODEPhysicsController::RelativeRotate(newmat,local);
104
105 }
106
107 void    KX_OdePhysicsController::ApplyTorque(const MT_Vector3& torque,bool local)
108 {
109                 ODEPhysicsController::ApplyTorque(torque[0],torque[1],torque[2],local);
110
111 }
112 void    KX_OdePhysicsController::ApplyForce(const MT_Vector3& force,bool local)
113 {
114                 ODEPhysicsController::ApplyForce(force[0],force[1],force[2],local);
115
116 }
117 MT_Vector3 KX_OdePhysicsController::GetLinearVelocity()
118 {
119         return MT_Vector3(0,0,0);
120 }
121
122 MT_Vector3 KX_OdePhysicsController::GetVelocity(const MT_Point3& pos)
123 {
124         return MT_Vector3(0,0,0);
125 }
126
127 void    KX_OdePhysicsController::SetAngularVelocity(const MT_Vector3& ang_vel,bool local)
128 {
129
130 }
131 void    KX_OdePhysicsController::SetLinearVelocity(const MT_Vector3& lin_vel,bool local)
132 {
133         ODEPhysicsController::SetLinearVelocity(lin_vel[0],lin_vel[1],lin_vel[2],local);
134 }
135
136 void KX_OdePhysicsController::setOrientation(const MT_Matrix3x3& rot)
137 {
138         MT_Quaternion orn = rot.getRotation();
139         ODEPhysicsController::setOrientation(orn[0],orn[1],orn[2],orn[3]);
140 }
141
142 void KX_OdePhysicsController::getOrientation(MT_Quaternion& orn)
143 {
144         float florn[4];
145         florn[0]=orn[0];
146         florn[1]=orn[1];
147         florn[2]=orn[2];
148         florn[3]=orn[3];
149         ODEPhysicsController::getOrientation(florn[0],florn[1],florn[2],florn[3]);
150         orn[0] = florn[0];
151         orn[1] = florn[1];
152         orn[2] = florn[2];
153         orn[3] = florn[3];
154
155
156 }
157
158 void KX_OdePhysicsController::setPosition(const MT_Point3& pos)
159 {
160         ODEPhysicsController::setPosition(pos[0],pos[1],pos[2]);
161 }
162
163 void KX_OdePhysicsController::setScaling(const MT_Vector3& scaling)
164 {
165 }
166
167 MT_Scalar       KX_OdePhysicsController::GetMass()
168 {
169         return ODEPhysicsController::getMass();
170 }
171
172 MT_Scalar       KX_OdePhysicsController::GetRadius()
173 {
174         return MT_Scalar(0.f);
175 }
176
177 MT_Vector3      KX_OdePhysicsController::getReactionForce()
178 {
179         return MT_Vector3(0,0,0);
180 }
181 void    KX_OdePhysicsController::setRigidBody(bool rigid)
182 {
183
184 }
185
186 void    KX_OdePhysicsController::SuspendDynamics(bool)
187 {
188         ODEPhysicsController::SuspendDynamics();
189 }
190 void    KX_OdePhysicsController::RestoreDynamics()
191 {
192         ODEPhysicsController::RestoreDynamics();
193 }
194         
195
196 SG_Controller*  KX_OdePhysicsController::GetReplica(class SG_Node* destnode)
197 {
198         PHY_IMotionState* motionstate = new KX_MotionState(destnode);
199         KX_OdePhysicsController* copyctrl = new KX_OdePhysicsController(*this);
200
201         // nlin: copied from KX_SumoPhysicsController.cpp. Not 100% sure what this does....
202         // furthermore, the parentctrl is not used in ODEPhysicsController::PostProcessReplica, but
203         // maybe it can/should be used in the future...
204
205         // begin copy block ------------------------------------------------------------------
206
207         //parentcontroller is here be able to avoid collisions between parent/child
208
209         PHY_IPhysicsController* parentctrl = NULL;
210
211         if (destnode != destnode->GetRootSGParent())
212         {
213                 KX_GameObject* clientgameobj = (KX_GameObject*) destnode->GetRootSGParent()->GetSGClientObject();
214                 if (clientgameobj)
215                 {
216                         parentctrl = (KX_OdePhysicsController*)clientgameobj->GetPhysicsController();
217                 } else
218                 {
219                         // it could be a false node, try the children
220                         NodeList::const_iterator childit;
221                         for (
222                                 childit = destnode->GetSGChildren().begin();
223                         childit!= destnode->GetSGChildren().end();
224                         ++childit
225                                 ) {
226                                 KX_GameObject* clientgameobj = static_cast<KX_GameObject*>( (*childit)->GetSGClientObject());
227                                 if (clientgameobj)
228                                 {
229                                         parentctrl = (KX_OdePhysicsController*)clientgameobj->GetPhysicsController();
230                                 }
231                         }
232                 }
233         }
234         // end copy block ------------------------------------------------------------------
235
236         copyctrl->PostProcessReplica(motionstate, this);
237
238         return copyctrl;
239         
240 }
241
242 void            KX_OdePhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
243 {
244 }
245
246         
247 void    KX_OdePhysicsController::SetSumoTransform(bool nondynaonly)
248 {
249
250 }
251         // todo: remove next line !
252 void    KX_OdePhysicsController::SetSimulatedTime(double time)
253 {
254
255 }
256         
257 #endif //USE_ODE