these files missed the cvs commt yesterday, this should fix compilation problems
[blender-staging.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         SimdTransform xform = m_body->getCenterOfMassTransform();
118         xform.setOrigin(xform.getOrigin() + SimdVector3(dlocX,dlocY,dlocZ));
119         this->m_body->setCenterOfMassTransform(xform);
120
121 }
122
123 void            CcdPhysicsController::RelativeRotate(const float drot[9],bool local)
124 {
125 }
126 void            CcdPhysicsController::getOrientation(float &quatImag0,float &quatImag1,float &quatImag2,float &quatReal)
127 {
128
129 }
130 void            CcdPhysicsController::setOrientation(float quatImag0,float quatImag1,float quatImag2,float quatReal)
131 {
132         m_body->activate();
133
134         SimdTransform xform  = m_body->getCenterOfMassTransform();
135         xform.setRotation(SimdQuaternion(quatImag0,quatImag1,quatImag2,quatReal));
136         m_body->setCenterOfMassTransform(xform);
137
138 }
139
140 void            CcdPhysicsController::setPosition(float posX,float posY,float posZ)
141 {
142         m_body->activate();
143
144         SimdTransform xform  = m_body->getCenterOfMassTransform();
145         xform.setOrigin(SimdVector3(posX,posY,posZ));
146         m_body->setCenterOfMassTransform(xform);
147
148 }
149 void            CcdPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
150 {
151 }
152
153 void            CcdPhysicsController::getPosition(PHY__Vector3& pos) const
154 {
155         assert(0);
156 }
157
158 void            CcdPhysicsController::setScaling(float scaleX,float scaleY,float scaleZ)
159 {
160 }
161                 
162                 // physics methods
163 void            CcdPhysicsController::ApplyTorque(float torqueX,float torqueY,float torqueZ,bool local)
164 {
165 }
166 void            CcdPhysicsController::ApplyForce(float forceX,float forceY,float forceZ,bool local)
167 {
168 }
169 void            CcdPhysicsController::SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local)
170 {
171         SimdVector3 angvel(ang_velX,ang_velY,ang_velZ);
172
173         m_body->setAngularVelocity(angvel);
174
175 }
176 void            CcdPhysicsController::SetLinearVelocity(float lin_velX,float lin_velY,float lin_velZ,bool local)
177 {
178
179         SimdVector3 linVel(lin_velX,lin_velY,lin_velZ);
180         m_body->setLinearVelocity(linVel);
181 }
182 void            CcdPhysicsController::applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ)
183 {
184         
185         SimdVector3 impulse(impulseX,impulseY,impulseZ);
186         SimdVector3 pos(attachX,attachY,attachZ);
187
188         m_body->activate();
189
190         m_body->applyImpulse(impulse,pos);
191
192 }
193 void            CcdPhysicsController::SetActive(bool active)
194 {
195 }
196                 // reading out information from physics
197 void            CcdPhysicsController::GetLinearVelocity(float& linvX,float& linvY,float& linvZ)
198 {
199 }
200 void            CcdPhysicsController::GetVelocity(const float posX,const float posY,const float posZ,float& linvX,float& linvY,float& linvZ)
201 {
202 }
203 void            CcdPhysicsController::getReactionForce(float& forceX,float& forceY,float& forceZ)
204 {
205 }
206
207                 // dyna's that are rigidbody are free in orientation, dyna's with non-rigidbody are restricted 
208 void            CcdPhysicsController::setRigidBody(bool rigid)
209 {
210 }
211
212                 // clientinfo for raycasts for example
213 void*           CcdPhysicsController::getNewClientInfo()
214 {
215         return m_newClientInfo;
216 }
217 void            CcdPhysicsController::setNewClientInfo(void* clientinfo)
218 {
219         m_newClientInfo = clientinfo;
220 }
221
222
223 void    CcdPhysicsController::UpdateDeactivation(float timeStep)
224 {
225         if ( (m_body->GetActivationState() == 2))
226                 return;
227         
228
229         if ((m_body->getLinearVelocity().length2() < gLinearSleepingTreshold*gLinearSleepingTreshold) &&
230                 (m_body->getAngularVelocity().length2() < gAngularSleepingTreshold*gAngularSleepingTreshold))
231         {
232                 m_body->m_deactivationTime += timeStep;
233         } else
234         {
235                 m_body->m_deactivationTime=0.f;
236                 m_body->SetActivationState(0);
237         }
238
239 }
240
241 bool CcdPhysicsController::wantsSleeping()
242 {
243
244         //disable deactivation
245         if (gDisableDeactivation || (gDeactivationTime == 0.f))
246                 return false;
247         //2 == ISLAND_SLEEPING, 3 == WANTS_DEACTIVATION
248         if ( (m_body->GetActivationState() == 2) || (m_body->GetActivationState() == 3))
249                 return true;
250
251         if (m_body->m_deactivationTime> gDeactivationTime)
252         {
253                 return true;
254         }
255         return false;
256 }
257