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