moved some hardcoded constants into tweaking tresholds
[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 //'temporarily' global variables
11 float   gDeactivationTime = 0.f;
12 float gLinearSleepingTreshold = 0.8f;
13 float gAngularSleepingTreshold = 1.0f;
14
15 #include "Dynamics/MassProps.h"
16
17 SimdVector3 startVel(0,0,0);//-10000);
18 CcdPhysicsController::CcdPhysicsController (const CcdConstructionInfo& ci)
19 {
20         m_collisionDelay = 0;
21
22         m_sleepingCounter = 0;
23
24         m_MotionState = ci.m_MotionState;
25
26
27         SimdTransform trans;
28         float tmp[3];
29         m_MotionState->getWorldPosition(tmp[0],tmp[1],tmp[2]);
30         trans.setOrigin(SimdVector3(tmp[0],tmp[1],tmp[2]));
31
32         SimdQuaternion orn;
33         m_MotionState->getWorldOrientation(orn[0],orn[1],orn[2],orn[3]);
34         trans.setRotation(orn);
35
36         MassProps mp(ci.m_mass, ci.m_localInertiaTensor);
37
38         m_body = new RigidBody(mp,0,0,ci.m_friction,ci.m_restitution);
39         
40         m_broadphaseHandle = ci.m_broadphaseHandle;
41
42         m_collisionShape = ci.m_collisionShape;
43
44         //
45         // init the rigidbody properly
46         //
47         
48         m_body->setMassProps(ci.m_mass, ci.m_localInertiaTensor);
49         m_body->setGravity( ci.m_gravity);
50
51         
52         m_body->setDamping(ci.m_linearDamping, ci.m_angularDamping);
53
54
55         m_body->setCenterOfMassTransform( trans );
56
57         #ifdef WIN32
58         if (m_body->getInvMass())
59                 m_body->setLinearVelocity(startVel);
60         #endif
61
62 }
63
64 CcdPhysicsController::~CcdPhysicsController()
65 {
66         //will be reference counted, due to sharing
67         //delete m_collisionShape;
68         delete m_MotionState;
69         delete m_body;
70 }
71
72                 /**
73                         SynchronizeMotionStates ynchronizes dynas, kinematic and deformable entities (and do 'late binding')
74                 */
75 bool            CcdPhysicsController::SynchronizeMotionStates(float time)
76 {
77         const SimdVector3& worldPos = m_body->getCenterOfMassPosition();
78         m_MotionState->setWorldPosition(worldPos[0],worldPos[1],worldPos[2]);
79         
80         const SimdQuaternion& worldquat = m_body->getOrientation();
81         m_MotionState->setWorldOrientation(worldquat[0],worldquat[1],worldquat[2],worldquat[3]);
82
83         m_MotionState->calculateWorldTransformations();
84
85         float scale[3];
86         m_MotionState->getWorldScaling(scale[0],scale[1],scale[2]);
87         
88         SimdVector3 scaling(scale[0],scale[1],scale[2]);
89         m_collisionShape->setLocalScaling(scaling);
90
91
92         return true;
93 }
94
95                 /**
96                         WriteMotionStateToDynamics synchronizes dynas, kinematic and deformable entities (and do 'late binding')
97                 */
98                 
99 void            CcdPhysicsController::WriteMotionStateToDynamics(bool nondynaonly)
100 {
101
102 }
103 void            CcdPhysicsController::WriteDynamicsToMotionState()
104 {
105 }
106                 // controller replication
107 void            CcdPhysicsController::PostProcessReplica(class PHY_IMotionState* motionstate,class PHY_IPhysicsController* parentctrl)
108 {
109 }
110
111                 // kinematic methods
112 void            CcdPhysicsController::RelativeTranslate(float dlocX,float dlocY,float dlocZ,bool local)
113 {
114         SimdTransform xform = m_body->getCenterOfMassTransform();
115         xform.setOrigin(xform.getOrigin() + SimdVector3(dlocX,dlocY,dlocZ));
116         this->m_body->setCenterOfMassTransform(xform);
117
118 }
119
120 void            CcdPhysicsController::RelativeRotate(const float drot[9],bool local)
121 {
122 }
123 void            CcdPhysicsController::getOrientation(float &quatImag0,float &quatImag1,float &quatImag2,float &quatReal)
124 {
125 }
126 void            CcdPhysicsController::setOrientation(float quatImag0,float quatImag1,float quatImag2,float quatReal)
127 {
128 }
129 void            CcdPhysicsController::setPosition(float posX,float posY,float posZ)
130 {
131 }
132 void            CcdPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
133 {
134 }
135
136 void            CcdPhysicsController::getPosition(PHY__Vector3& pos) const
137 {
138         assert(0);
139 }
140
141 void            CcdPhysicsController::setScaling(float scaleX,float scaleY,float scaleZ)
142 {
143 }
144                 
145                 // physics methods
146 void            CcdPhysicsController::ApplyTorque(float torqueX,float torqueY,float torqueZ,bool local)
147 {
148 }
149 void            CcdPhysicsController::ApplyForce(float forceX,float forceY,float forceZ,bool local)
150 {
151 }
152 void            CcdPhysicsController::SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local)
153 {
154         SimdVector3 angvel(ang_velX,ang_velY,ang_velZ);
155
156         m_body->setAngularVelocity(angvel);
157
158 }
159 void            CcdPhysicsController::SetLinearVelocity(float lin_velX,float lin_velY,float lin_velZ,bool local)
160 {
161
162         SimdVector3 linVel(lin_velX,lin_velY,lin_velZ);
163         m_body->setLinearVelocity(linVel);
164 }
165 void            CcdPhysicsController::applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ)
166 {
167 }
168 void            CcdPhysicsController::SetActive(bool active)
169 {
170 }
171                 // reading out information from physics
172 void            CcdPhysicsController::GetLinearVelocity(float& linvX,float& linvY,float& linvZ)
173 {
174 }
175 void            CcdPhysicsController::GetVelocity(const float posX,const float posY,const float posZ,float& linvX,float& linvY,float& linvZ)
176 {
177 }
178 void            CcdPhysicsController::getReactionForce(float& forceX,float& forceY,float& forceZ)
179 {
180 }
181
182                 // dyna's that are rigidbody are free in orientation, dyna's with non-rigidbody are restricted 
183 void            CcdPhysicsController::setRigidBody(bool rigid)
184 {
185 }
186
187                 // clientinfo for raycasts for example
188 void*           CcdPhysicsController::getNewClientInfo()
189 {
190         return 0;
191 }
192 void            CcdPhysicsController::setNewClientInfo(void* clientinfo)
193 {
194
195 }
196
197
198
199 bool CcdPhysicsController::wantsSleeping()
200 {
201
202         //disable deactivation
203         if (gDeactivationTime == 0.f)
204                 return false;
205
206         if ( (m_body->GetActivationState() == 3) || (m_body->GetActivationState() == 2))
207                 return true;
208
209         if ((m_body->getLinearVelocity().length2() < gLinearSleepingTreshold*gLinearSleepingTreshold) &&
210                 (m_body->getAngularVelocity().length2() < gAngularSleepingTreshold*gAngularSleepingTreshold))
211         {
212                 m_sleepingCounter++;
213         } else
214         {
215                 m_sleepingCounter=0;
216         }
217
218         if (m_sleepingCounter> gDeactivationTime)
219         {
220                 return true;
221         }
222         return false;
223 }
224