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