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