5ddacd05d050dfb18b906cb81ffd6013c07f9cb6
[blender.git] / extern / bullet / Extras / PhysicsInterface / CcdPhysics / 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 rotval[9],bool local)
124 {
125         if (m_body )
126         {
127                 SimdMatrix3x3 drotmat(  rotval[0],rotval[1],rotval[2],
128                                                                 rotval[4],rotval[5],rotval[6],
129                                                                 rotval[8],rotval[9],rotval[10]);
130
131
132                 SimdMatrix3x3 currentOrn;
133                 GetWorldOrientation(currentOrn);
134
135                 SimdTransform xform = m_body->getCenterOfMassTransform();
136
137                 xform.setBasis(xform.getBasis()*(local ? 
138                 drotmat : (currentOrn.inverse() * drotmat * currentOrn)));
139
140                 m_body->setCenterOfMassTransform(xform);
141         }
142
143 }
144
145 void CcdPhysicsController::GetWorldOrientation(SimdMatrix3x3& mat)
146 {
147         float orn[4];
148         m_MotionState->getWorldOrientation(orn[0],orn[1],orn[2],orn[3]);
149         SimdQuaternion quat(orn[0],orn[1],orn[2],orn[3]);
150         mat.setRotation(quat);
151 }
152
153 void            CcdPhysicsController::getOrientation(float &quatImag0,float &quatImag1,float &quatImag2,float &quatReal)
154 {
155
156 }
157 void            CcdPhysicsController::setOrientation(float quatImag0,float quatImag1,float quatImag2,float quatReal)
158 {
159         m_body->activate();
160
161         SimdTransform xform  = m_body->getCenterOfMassTransform();
162         xform.setRotation(SimdQuaternion(quatImag0,quatImag1,quatImag2,quatReal));
163         m_body->setCenterOfMassTransform(xform);
164
165 }
166
167 void            CcdPhysicsController::setPosition(float posX,float posY,float posZ)
168 {
169         m_body->activate();
170
171         SimdTransform xform  = m_body->getCenterOfMassTransform();
172         xform.setOrigin(SimdVector3(posX,posY,posZ));
173         m_body->setCenterOfMassTransform(xform);
174
175 }
176 void            CcdPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
177 {
178 }
179
180 void            CcdPhysicsController::getPosition(PHY__Vector3& pos) const
181 {
182         assert(0);
183 }
184
185 void            CcdPhysicsController::setScaling(float scaleX,float scaleY,float scaleZ)
186 {
187 }
188                 
189                 // physics methods
190 void            CcdPhysicsController::ApplyTorque(float torqueX,float torqueY,float torqueZ,bool local)
191 {
192 }
193 void            CcdPhysicsController::ApplyForce(float forceX,float forceY,float forceZ,bool local)
194 {
195 }
196 void            CcdPhysicsController::SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local)
197 {
198         SimdVector3 angvel(ang_velX,ang_velY,ang_velZ);
199
200         m_body->setAngularVelocity(angvel);
201
202 }
203 void            CcdPhysicsController::SetLinearVelocity(float lin_velX,float lin_velY,float lin_velZ,bool local)
204 {
205
206         SimdVector3 linVel(lin_velX,lin_velY,lin_velZ);
207         m_body->setLinearVelocity(linVel);
208 }
209 void            CcdPhysicsController::applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ)
210 {
211         
212         SimdVector3 impulse(impulseX,impulseY,impulseZ);
213         SimdVector3 pos(attachX,attachY,attachZ);
214
215         m_body->activate();
216
217         m_body->applyImpulse(impulse,pos);
218
219 }
220 void            CcdPhysicsController::SetActive(bool active)
221 {
222 }
223                 // reading out information from physics
224 void            CcdPhysicsController::GetLinearVelocity(float& linvX,float& linvY,float& linvZ)
225 {
226 }
227 void            CcdPhysicsController::GetVelocity(const float posX,const float posY,const float posZ,float& linvX,float& linvY,float& linvZ)
228 {
229 }
230 void            CcdPhysicsController::getReactionForce(float& forceX,float& forceY,float& forceZ)
231 {
232 }
233
234                 // dyna's that are rigidbody are free in orientation, dyna's with non-rigidbody are restricted 
235 void            CcdPhysicsController::setRigidBody(bool rigid)
236 {
237 }
238
239                 // clientinfo for raycasts for example
240 void*           CcdPhysicsController::getNewClientInfo()
241 {
242         return m_newClientInfo;
243 }
244 void            CcdPhysicsController::setNewClientInfo(void* clientinfo)
245 {
246         m_newClientInfo = clientinfo;
247 }
248
249
250 void    CcdPhysicsController::UpdateDeactivation(float timeStep)
251 {
252         if ( (m_body->GetActivationState() == 2))
253                 return;
254         
255
256         if ((m_body->getLinearVelocity().length2() < gLinearSleepingTreshold*gLinearSleepingTreshold) &&
257                 (m_body->getAngularVelocity().length2() < gAngularSleepingTreshold*gAngularSleepingTreshold))
258         {
259                 m_body->m_deactivationTime += timeStep;
260         } else
261         {
262                 m_body->m_deactivationTime=0.f;
263                 m_body->SetActivationState(0);
264         }
265
266 }
267
268 bool CcdPhysicsController::wantsSleeping()
269 {
270
271         //disable deactivation
272         if (gDisableDeactivation || (gDeactivationTime == 0.f))
273                 return false;
274         //2 == ISLAND_SLEEPING, 3 == WANTS_DEACTIVATION
275         if ( (m_body->GetActivationState() == 2) || (m_body->GetActivationState() == 3))
276                 return true;
277
278         if (m_body->m_deactivationTime> gDeactivationTime)
279         {
280                 return true;
281         }
282         return false;
283 }
284