0eff54dc0c653f99680e3481aaa6ef8e0cfc03ba
[blender.git] / source / gameengine / Ketsji / KX_OdePhysicsController.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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/BL DUAL LICENSE BLOCK *****
21  */
22 #include "KX_OdePhysicsController.h"
23 #include "KX_GameObject.h"
24 #include "KX_MotionState.h"
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 KX_OdePhysicsController::KX_OdePhysicsController(
31                                                                                                  bool dyna,
32                                                                                                  bool fullRigidBody,
33                                                                                                  bool phantom,
34                                                                                                  class PHY_IMotionState* motionstate,
35                                                                                                  struct dxSpace* space,
36                                                                                                  struct dxWorld*        world,
37                                                                                                  float  mass,
38                                                                                                  float  friction,
39                                                                                                  float  restitution,
40                                                                                                  bool   implicitsphere,
41                                                                                                  float  center[3],
42                                                                                                  float  extends[3],
43                                                                                                  float  radius
44                                                                                                  ) 
45 : KX_IPhysicsController(dyna,(PHY_IPhysicsController*)this),
46 ODEPhysicsController(
47 dyna,fullRigidBody,phantom,motionstate,
48 space,world,mass,friction,restitution,
49 implicitsphere,center,extends,radius)
50 {
51 };
52
53
54 bool    KX_OdePhysicsController::Update(double time)
55 {
56         return SynchronizeMotionStates(time);
57 }
58
59 void                    KX_OdePhysicsController::SetObject (SG_IObject* object)
60 {
61         SG_Controller::SetObject(object);
62
63         // cheating here...
64         KX_GameObject* gameobj = (KX_GameObject*)       object->GetSGClientObject();
65         gameobj->m_pPhysicsController1 = this;
66         
67 }
68
69
70
71 void    KX_OdePhysicsController::applyImpulse(const MT_Point3& attach, const MT_Vector3& impulse)
72 {
73         ODEPhysicsController::applyImpulse(attach[0],attach[1],attach[2],impulse[0],impulse[1],impulse[2]);
74 }
75         
76
77
78 void    KX_OdePhysicsController::RelativeTranslate(const MT_Vector3& dloc,bool local)
79 {
80         ODEPhysicsController::RelativeTranslate(dloc[0],dloc[1],dloc[2],local);
81
82 }
83 void    KX_OdePhysicsController::RelativeRotate(const MT_Matrix3x3& drot,bool local)
84 {
85         double oldmat[12];
86         drot.getValue(oldmat);
87         float newmat[9];
88         float *m = &newmat[0];
89         double *orgm = &oldmat[0];
90
91          *m++ = *orgm++;*m++ = *orgm++;*m++ = *orgm++;orgm++;
92          *m++ = *orgm++;*m++ = *orgm++;*m++ = *orgm++;orgm++;
93          *m++ = *orgm++;*m++ = *orgm++;*m++ = *orgm++;orgm++;
94
95          ODEPhysicsController::RelativeRotate(newmat,local);
96
97 }
98
99 void    KX_OdePhysicsController::ApplyTorque(const MT_Vector3& torque,bool local)
100 {
101                 ODEPhysicsController::ApplyTorque(torque[0],torque[1],torque[2],local);
102
103 }
104 void    KX_OdePhysicsController::ApplyForce(const MT_Vector3& force,bool local)
105 {
106                 ODEPhysicsController::ApplyForce(force[0],force[1],force[2],local);
107
108 }
109 MT_Vector3 KX_OdePhysicsController::GetLinearVelocity()
110 {
111         return MT_Vector3(0,0,0);
112 }
113
114 MT_Vector3 KX_OdePhysicsController::GetVelocity(const MT_Point3& pos)
115 {
116         return MT_Vector3(0,0,0);
117 }
118
119 void    KX_OdePhysicsController::SetAngularVelocity(const MT_Vector3& ang_vel,bool local)
120 {
121
122 }
123 void    KX_OdePhysicsController::SetLinearVelocity(const MT_Vector3& lin_vel,bool local)
124 {
125         ODEPhysicsController::SetLinearVelocity(lin_vel[0],lin_vel[1],lin_vel[2],local);
126 }
127
128 void KX_OdePhysicsController::setOrientation(const MT_Quaternion& orn)
129 {
130         ODEPhysicsController::setOrientation(orn[0],orn[1],orn[2],orn[3]);
131 }
132
133 void KX_OdePhysicsController::getOrientation(MT_Quaternion& orn)
134 {
135         float florn[4];
136         florn[0]=orn[0];
137         florn[1]=orn[1];
138         florn[2]=orn[2];
139         florn[3]=orn[3];
140         ODEPhysicsController::getOrientation(florn[0],florn[1],florn[2],florn[3]);
141         orn[0] = florn[0];
142         orn[1] = florn[1];
143         orn[2] = florn[2];
144         orn[3] = florn[3];
145
146
147 }
148
149 void KX_OdePhysicsController::setPosition(const MT_Point3& pos)
150 {
151         ODEPhysicsController::setPosition(pos[0],pos[1],pos[2]);
152 }
153
154 void KX_OdePhysicsController::setScaling(const MT_Vector3& scaling)
155 {
156 }
157
158 MT_Scalar       KX_OdePhysicsController::GetMass()
159 {
160         return ODEPhysicsController::getMass();
161 }
162
163 MT_Vector3      KX_OdePhysicsController::getReactionForce()
164 {
165         return MT_Vector3(0,0,0);
166 }
167 void    KX_OdePhysicsController::setRigidBody(bool rigid)
168 {
169
170 }
171
172 void    KX_OdePhysicsController::SuspendDynamics()
173 {
174         ODEPhysicsController::SuspendDynamics();
175 }
176 void    KX_OdePhysicsController::RestoreDynamics()
177 {
178         ODEPhysicsController::RestoreDynamics();
179 }
180         
181
182 SG_Controller*  KX_OdePhysicsController::GetReplica(class SG_Node* destnode)
183 {
184         PHY_IMotionState* motionstate = new KX_MotionState(destnode);
185         KX_OdePhysicsController* copyctrl = new KX_OdePhysicsController(*this);
186
187         // nlin: copied from KX_SumoPhysicsController.cpp. Not 100% sure what this does....
188         // furthermore, the parentctrl is not used in ODEPhysicsController::PostProcessReplica, but
189         // maybe it can/should be used in the future...
190
191         // begin copy block ------------------------------------------------------------------
192
193         //parentcontroller is here be able to avoid collisions between parent/child
194
195         PHY_IPhysicsController* parentctrl = NULL;
196
197         if (destnode != destnode->GetRootSGParent())
198         {
199                 KX_GameObject* clientgameobj = (KX_GameObject*) destnode->GetRootSGParent()->GetSGClientObject();
200                 if (clientgameobj)
201                 {
202                         parentctrl = (KX_OdePhysicsController*)clientgameobj->GetPhysicsController();
203                 } else
204                 {
205                         // it could be a false node, try the children
206                         NodeList::const_iterator childit;
207                         for (
208                                 childit = destnode->GetSGChildren().begin();
209                         childit!= destnode->GetSGChildren().end();
210                         ++childit
211                                 ) {
212                                 KX_GameObject* clientgameobj = static_cast<KX_GameObject*>( (*childit)->GetSGClientObject());
213                                 if (clientgameobj)
214                                 {
215                                         parentctrl = (KX_OdePhysicsController*)clientgameobj->GetPhysicsController();
216                                 }
217                         }
218                 }
219         }
220         // end copy block ------------------------------------------------------------------
221
222         copyctrl->PostProcessReplica(motionstate, this);
223
224         return copyctrl;
225         
226 }
227
228
229         
230 void    KX_OdePhysicsController::SetSumoTransform(bool nondynaonly)
231 {
232
233 }
234         // todo: remove next line !
235 void    KX_OdePhysicsController::SetSimulatedTime(double time)
236 {
237
238 }
239