d9c5575f657cf65e53e1c1812804a487e7b24826
[blender.git] / source / gameengine / Physics / Bullet / CcdPhysicsController.cpp
1 #include "CcdPhysicsController.h"
2
3 #include "Dynamics/RigidBody.h"
4 #include "PHY_IMotionState.h"
5 #include "BroadphaseCollision/BroadphaseProxy.h"
6 #include "CollisionShapes/ConvexShape.h"
7
8 class BP_Proxy;
9
10 ///todo: fill all the empty CcdPhysicsController methods, hook them up to the RigidBody class
11
12 //'temporarily' global variables
13 float   gDeactivationTime = 2.f;
14 bool    gDisableDeactivation = false;
15
16 float gLinearSleepingTreshold = 0.8f;
17 float gAngularSleepingTreshold = 1.0f;
18
19 #include "Dynamics/MassProps.h"
20
21 SimdVector3 startVel(0,0,0);//-10000);
22 CcdPhysicsController::CcdPhysicsController (const CcdConstructionInfo& ci)
23 {
24         m_collisionDelay = 0;
25         m_newClientInfo = 0;
26         
27         m_MotionState = ci.m_MotionState;
28
29
30         SimdTransform trans;
31         float tmp[3];
32         m_MotionState->getWorldPosition(tmp[0],tmp[1],tmp[2]);
33         trans.setOrigin(SimdVector3(tmp[0],tmp[1],tmp[2]));
34
35         SimdQuaternion orn;
36         m_MotionState->getWorldOrientation(orn[0],orn[1],orn[2],orn[3]);
37         trans.setRotation(orn);
38
39         MassProps mp(ci.m_mass, ci.m_localInertiaTensor);
40
41         m_body = new RigidBody(mp,0,0,ci.m_friction,ci.m_restitution);
42         
43         m_broadphaseHandle = ci.m_broadphaseHandle;
44
45         m_collisionShape = ci.m_collisionShape;
46
47         //
48         // init the rigidbody properly
49         //
50         
51         m_body->setMassProps(ci.m_mass, ci.m_localInertiaTensor);
52         m_body->setGravity( ci.m_gravity);
53
54         
55         m_body->setDamping(ci.m_linearDamping, ci.m_angularDamping);
56
57
58         m_body->setCenterOfMassTransform( trans );
59
60         #ifdef WIN32
61         if (m_body->getInvMass())
62                 m_body->setLinearVelocity(startVel);
63         #endif
64
65 }
66
67 CcdPhysicsController::~CcdPhysicsController()
68 {
69         //will be reference counted, due to sharing
70         //delete m_collisionShape;
71         delete m_MotionState;
72         delete m_body;
73 }
74
75                 /**
76                         SynchronizeMotionStates ynchronizes dynas, kinematic and deformable entities (and do 'late binding')
77                 */
78 bool            CcdPhysicsController::SynchronizeMotionStates(float time)
79 {
80         const SimdVector3& worldPos = m_body->getCenterOfMassPosition();
81         m_MotionState->setWorldPosition(worldPos[0],worldPos[1],worldPos[2]);
82         
83         const SimdQuaternion& worldquat = m_body->getOrientation();
84         m_MotionState->setWorldOrientation(worldquat[0],worldquat[1],worldquat[2],worldquat[3]);
85
86         m_MotionState->calculateWorldTransformations();
87
88         float scale[3];
89         m_MotionState->getWorldScaling(scale[0],scale[1],scale[2]);
90         
91         SimdVector3 scaling(scale[0],scale[1],scale[2]);
92         m_collisionShape->setLocalScaling(scaling);
93
94
95         return true;
96 }
97
98                 /**
99                         WriteMotionStateToDynamics synchronizes dynas, kinematic and deformable entities (and do 'late binding')
100                 */
101                 
102 void            CcdPhysicsController::WriteMotionStateToDynamics(bool nondynaonly)
103 {
104
105 }
106 void            CcdPhysicsController::WriteDynamicsToMotionState()
107 {
108 }
109                 // controller replication
110 void            CcdPhysicsController::PostProcessReplica(class PHY_IMotionState* motionstate,class PHY_IPhysicsController* parentctrl)
111 {
112 }
113
114                 // kinematic methods
115 void            CcdPhysicsController::RelativeTranslate(float dlocX,float dlocY,float dlocZ,bool local)
116 {
117         SimdVector3 dloc(dlocX,dlocY,dlocZ);
118         SimdTransform xform = m_body->getCenterOfMassTransform();
119
120         if (local)
121         {
122                 dloc = xform.getBasis()*dloc;
123         }
124
125         xform.setOrigin(xform.getOrigin() + dloc);
126         this->m_body->setCenterOfMassTransform(xform);
127
128 }
129
130 void            CcdPhysicsController::RelativeRotate(const float rotval[9],bool local)
131 {
132         if (m_body )
133         {
134                 SimdMatrix3x3 drotmat(  rotval[0],rotval[1],rotval[2],
135                                                                 rotval[4],rotval[5],rotval[6],
136                                                                 rotval[8],rotval[9],rotval[10]);
137
138
139                 SimdMatrix3x3 currentOrn;
140                 GetWorldOrientation(currentOrn);
141
142                 SimdTransform xform = m_body->getCenterOfMassTransform();
143
144                 xform.setBasis(xform.getBasis()*(local ? 
145                 drotmat : (currentOrn.inverse() * drotmat * currentOrn)));
146
147                 m_body->setCenterOfMassTransform(xform);
148         }
149
150 }
151
152 void CcdPhysicsController::GetWorldOrientation(SimdMatrix3x3& mat)
153 {
154         float orn[4];
155         m_MotionState->getWorldOrientation(orn[0],orn[1],orn[2],orn[3]);
156         SimdQuaternion quat(orn[0],orn[1],orn[2],orn[3]);
157         mat.setRotation(quat);
158 }
159
160 void            CcdPhysicsController::getOrientation(float &quatImag0,float &quatImag1,float &quatImag2,float &quatReal)
161 {
162         SimdQuaternion q = m_body->getCenterOfMassTransform().getRotation();
163         quatImag0 = q[0];
164         quatImag1 = q[1];
165         quatImag2 = q[2];
166         quatReal = q[3];
167 }
168 void            CcdPhysicsController::setOrientation(float quatImag0,float quatImag1,float quatImag2,float quatReal)
169 {
170         m_body->activate();
171
172         SimdTransform xform  = m_body->getCenterOfMassTransform();
173         xform.setRotation(SimdQuaternion(quatImag0,quatImag1,quatImag2,quatReal));
174         m_body->setCenterOfMassTransform(xform);
175
176 }
177
178 void            CcdPhysicsController::setPosition(float posX,float posY,float posZ)
179 {
180         m_body->activate();
181
182         SimdTransform xform  = m_body->getCenterOfMassTransform();
183         xform.setOrigin(SimdVector3(posX,posY,posZ));
184         m_body->setCenterOfMassTransform(xform);
185
186 }
187 void            CcdPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
188 {
189 }
190
191 void            CcdPhysicsController::getPosition(PHY__Vector3& pos) const
192 {
193         assert(0);
194 }
195
196 void            CcdPhysicsController::setScaling(float scaleX,float scaleY,float scaleZ)
197 {
198         if (m_body && m_body->GetCollisionShape())
199         {
200                 SimdVector3 scaling(scaleX,scaleY,scaleZ);
201                 m_body->GetCollisionShape()->setLocalScaling(scaling);
202         }
203 }
204                 
205                 // physics methods
206 void            CcdPhysicsController::ApplyTorque(float torqueX,float torqueY,float torqueZ,bool local)
207 {
208         SimdVector3 torque(torqueX,torqueY,torqueZ);
209         SimdTransform xform = m_body->getCenterOfMassTransform();
210         if (local)
211         {
212                 torque  = xform.getBasis()*torque;
213         }
214         m_body->applyTorque(torque);
215 }
216
217 void            CcdPhysicsController::ApplyForce(float forceX,float forceY,float forceZ,bool local)
218 {
219         SimdVector3 force(forceX,forceX,forceX);
220         SimdTransform xform = m_body->getCenterOfMassTransform();
221         if (local)
222         {
223                 force   = xform.getBasis()*force;
224         }
225         m_body->applyCentralForce(force);
226 }
227 void            CcdPhysicsController::SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local)
228 {
229         SimdVector3 angvel(ang_velX,ang_velY,ang_velZ);
230         SimdTransform xform = m_body->getCenterOfMassTransform();
231         if (local)
232         {
233                 angvel  = xform.getBasis()*angvel;
234         }
235
236         m_body->setAngularVelocity(angvel);
237
238 }
239 void            CcdPhysicsController::SetLinearVelocity(float lin_velX,float lin_velY,float lin_velZ,bool local)
240 {
241         SimdVector3 linVel(lin_velX,lin_velY,lin_velZ);
242         SimdTransform xform = m_body->getCenterOfMassTransform();
243         if (local)
244         {
245                 linVel  = xform.getBasis()*linVel;
246         }
247         m_body->setLinearVelocity(linVel);
248 }
249 void            CcdPhysicsController::applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ)
250 {
251         
252         SimdVector3 impulse(impulseX,impulseY,impulseZ);
253         SimdVector3 pos(attachX,attachY,attachZ);
254
255         m_body->activate();
256
257         m_body->applyImpulse(impulse,pos);
258
259 }
260 void            CcdPhysicsController::SetActive(bool active)
261 {
262 }
263                 // reading out information from physics
264 void            CcdPhysicsController::GetLinearVelocity(float& linvX,float& linvY,float& linvZ)
265 {
266         const SimdVector3& linvel = this->m_body->getLinearVelocity();
267         linvX = linvel.x();
268         linvY = linvel.y();
269         linvZ = linvel.z();
270
271 }
272 void            CcdPhysicsController::GetVelocity(const float posX,const float posY,const float posZ,float& linvX,float& linvY,float& linvZ)
273 {
274         SimdVector3 pos(posX,posY,posZ);
275         SimdVector3 rel_pos = pos-m_body->getCenterOfMassPosition();
276         SimdVector3 linvel = m_body->getVelocityInLocalPoint(rel_pos);
277         linvX = linvel.x();
278         linvY = linvel.y();
279         linvZ = linvel.z();
280 }
281 void            CcdPhysicsController::getReactionForce(float& forceX,float& forceY,float& forceZ)
282 {
283 }
284
285                 // dyna's that are rigidbody are free in orientation, dyna's with non-rigidbody are restricted 
286 void            CcdPhysicsController::setRigidBody(bool rigid)
287 {
288 }
289
290                 // clientinfo for raycasts for example
291 void*           CcdPhysicsController::getNewClientInfo()
292 {
293         return m_newClientInfo;
294 }
295 void            CcdPhysicsController::setNewClientInfo(void* clientinfo)
296 {
297         m_newClientInfo = clientinfo;
298 }
299
300
301 void    CcdPhysicsController::UpdateDeactivation(float timeStep)
302 {
303         if ( (m_body->GetActivationState() == 2))
304                 return;
305         
306
307         if ((m_body->getLinearVelocity().length2() < gLinearSleepingTreshold*gLinearSleepingTreshold) &&
308                 (m_body->getAngularVelocity().length2() < gAngularSleepingTreshold*gAngularSleepingTreshold))
309         {
310                 m_body->m_deactivationTime += timeStep;
311         } else
312         {
313                 m_body->m_deactivationTime=0.f;
314                 m_body->SetActivationState(0);
315         }
316
317 }
318
319 bool CcdPhysicsController::wantsSleeping()
320 {
321
322         //disable deactivation
323         if (gDisableDeactivation || (gDeactivationTime == 0.f))
324                 return false;
325         //2 == ISLAND_SLEEPING, 3 == WANTS_DEACTIVATION
326         if ( (m_body->GetActivationState() == 2) || (m_body->GetActivationState() == 3))
327                 return true;
328
329         if (m_body->m_deactivationTime> gDeactivationTime)
330         {
331                 return true;
332         }
333         return false;
334 }
335