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