2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
17 #ifndef BULLET2_PHYSICSCONTROLLER_H
18 #define BULLET2_PHYSICSCONTROLLER_H
22 #include "PHY_IPhysicsController.h"
24 /// PHY_IPhysicsController is the abstract simplified Interface to a physical object.
25 /// It contains the IMotionState and IDeformableMesh Interfaces.
26 #include "btBulletDynamicsCommon.h"
27 #include "LinearMath/btTransform.h"
29 #include "PHY_IMotionState.h"
31 extern float gDeactivationTime;
32 extern float gLinearSleepingTreshold;
33 extern float gAngularSleepingTreshold;
34 extern bool gDisableDeactivation;
35 class CcdPhysicsEnvironment;
38 class btCollisionShape;
41 // It contains all the information needed to create a simple bullet shape at runtime
42 class CcdShapeConstructionInfo
45 CcdShapeConstructionInfo() :
46 m_shapeType(PHY_SHAPE_NONE),
49 m_halfExtend(0.f,0.f,0.f),
50 m_childScale(1.0f,1.0f,1.0f),
52 m_unscaledShape(NULL),
55 m_childTrans.setIdentity();
58 ~CcdShapeConstructionInfo();
60 CcdShapeConstructionInfo* AddRef()
74 void AddShape(CcdShapeConstructionInfo* shapeInfo);
76 btTriangleMeshShape* GetMeshShape(void)
78 return m_unscaledShape;
80 CcdShapeConstructionInfo* GetNextShape()
84 CcdShapeConstructionInfo* GetChildShape(int i)
86 CcdShapeConstructionInfo* shape = m_nextShape;
87 while (i > 0 && shape != NULL)
89 shape = shape->m_nextShape;
95 bool SetMesh(RAS_MeshObject* mesh, bool polytope);
97 btCollisionShape* CreateBulletShape();
100 PHY_ShapeType m_shapeType;
103 btVector3 m_halfExtend;
104 btTransform m_childTrans;
105 btVector3 m_childScale;
106 std::vector<btPoint3> m_vertexArray; // Contains both vertex array for polytope shape and
107 // triangle array for concave mesh shape.
108 // In this case a triangle is made of 3 consecutive points
109 std::vector<int> m_polygonIndexArray; // Contains the array of polygon index in the
110 // original mesh that correspond to shape triangles.
111 // only set for concave mesh shape.
112 const RAS_MeshObject* m_meshObject; // Keep a pointer to the original mesh
115 CcdShapeConstructionInfo* m_nextShape; // for compound shape
116 btBvhTriangleMeshShape* m_unscaledShape;// holds the shared unscale BVH mesh shape,
117 // the actual shape is of type btScaledBvhTriangleMeshShape
118 int m_refCount; // this class is shared between replicas
119 // keep track of users so that we can release it
122 struct CcdConstructionInfo
125 ///CollisionFilterGroups provides some optional usage of basic collision filtering
126 ///this is done during broadphase, so very early in the pipeline
127 ///more advanced collision filtering should be done in btCollisionDispatcher::NeedsCollision
128 enum CollisionFilterGroups
135 AllFilter = DefaultFilter | StaticFilter | KinematicFilter | DebrisFilter | SensorFilter,
139 CcdConstructionInfo()
140 : m_localInertiaTensor(1.f, 1.f, 1.f),
142 m_scaling(1.f,1.f,1.f),
146 m_linearDamping(0.1f),
147 m_angularDamping(0.1f),
151 m_collisionFilterGroup(DefaultFilter),
152 m_collisionFilterMask(AllFilter),
161 btVector3 m_localInertiaTensor;
165 btScalar m_restitution;
167 btScalar m_linearDamping;
168 btScalar m_angularDamping;
170 int m_collisionFlags;
173 ///optional use of collision group/mask:
174 ///only collision with object goups that match the collision mask.
175 ///this is very basic early out. advanced collision filtering should be
176 ///done in the btCollisionDispatcher::NeedsCollision and NeedsResponse
177 ///both values default to 1
178 short int m_collisionFilterGroup;
179 short int m_collisionFilterMask;
181 ///these pointers are used as argument passing for the CcdPhysicsController constructor
182 ///and not anymore after that
183 class btCollisionShape* m_collisionShape;
184 class PHY_IMotionState* m_MotionState;
185 class CcdShapeConstructionInfo* m_shapeInfo;
187 CcdPhysicsEnvironment* m_physicsEnv; //needed for self-replication
188 float m_inertiaFactor;//tweak the inertia (hooked up to Blender 'formfactor'
195 ///CcdPhysicsController is a physics object that supports continuous collision detection and time of impact based physics resolution.
196 class CcdPhysicsController : public PHY_IPhysicsController
199 class PHY_IMotionState* m_MotionState;
200 btMotionState* m_bulletMotionState;
201 class btCollisionShape* m_collisionShape;
202 class CcdShapeConstructionInfo* m_shapeInfo;
204 friend class CcdPhysicsEnvironment; // needed when updating the controller
207 void* m_newClientInfo;
208 int m_registerCount; // needed when multiple sensors use the same controller
209 CcdConstructionInfo m_cci;//needed for replication
210 void GetWorldOrientation(btMatrix3x3& mat);
212 void CreateRigidbody();
215 return (m_registerCount++ == 0) ? true : false;
218 return (--m_registerCount == 0) ? true : false;
222 void setWorldOrientation(const btMatrix3x3& mat);
226 int m_collisionDelay;
229 CcdPhysicsController (const CcdConstructionInfo& ci);
231 virtual ~CcdPhysicsController();
234 btRigidBody* GetRigidBody() { return m_body;}
235 CcdShapeConstructionInfo* GetShapeInfo() { return m_shapeInfo; }
237 btCollisionShape* GetCollisionShape() {
238 return m_body->getCollisionShape();
240 ////////////////////////////////////
241 // PHY_IPhysicsController interface
242 ////////////////////////////////////
246 SynchronizeMotionStates ynchronizes dynas, kinematic and deformable entities (and do 'late binding')
248 virtual bool SynchronizeMotionStates(float time);
250 WriteMotionStateToDynamics ynchronizes dynas, kinematic and deformable entities (and do 'late binding')
253 virtual void WriteMotionStateToDynamics(bool nondynaonly);
254 virtual void WriteDynamicsToMotionState();
255 // controller replication
256 virtual void PostProcessReplica(class PHY_IMotionState* motionstate,class PHY_IPhysicsController* parentctrl);
259 virtual void RelativeTranslate(float dlocX,float dlocY,float dlocZ,bool local);
260 virtual void RelativeRotate(const float drot[9],bool local);
261 virtual void getOrientation(float &quatImag0,float &quatImag1,float &quatImag2,float &quatReal);
262 virtual void setOrientation(float quatImag0,float quatImag1,float quatImag2,float quatReal);
263 virtual void setPosition(float posX,float posY,float posZ);
264 virtual void getPosition(PHY__Vector3& pos) const;
266 virtual void setScaling(float scaleX,float scaleY,float scaleZ);
269 virtual void ApplyTorque(float torqueX,float torqueY,float torqueZ,bool local);
270 virtual void ApplyForce(float forceX,float forceY,float forceZ,bool local);
271 virtual void SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local);
272 virtual void SetLinearVelocity(float lin_velX,float lin_velY,float lin_velZ,bool local);
273 virtual void applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ);
274 virtual void SetActive(bool active);
276 // reading out information from physics
277 virtual void GetLinearVelocity(float& linvX,float& linvY,float& linvZ);
278 virtual void GetAngularVelocity(float& angVelX,float& angVelY,float& angVelZ);
279 virtual void GetVelocity(const float posX,const float posY,const float posZ,float& linvX,float& linvY,float& linvZ);
280 virtual void getReactionForce(float& forceX,float& forceY,float& forceZ);
282 // dyna's that are rigidbody are free in orientation, dyna's with non-rigidbody are restricted
283 virtual void setRigidBody(bool rigid);
286 virtual void resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ);
288 // clientinfo for raycasts for example
289 virtual void* getNewClientInfo();
290 virtual void setNewClientInfo(void* clientinfo);
291 virtual PHY_IPhysicsController* GetReplica();
293 ///There should be no 'SetCollisionFilterGroup' method, as changing this during run-time is will result in errors
294 short int GetCollisionFilterGroup() const
296 return m_cci.m_collisionFilterGroup;
298 ///There should be no 'SetCollisionFilterGroup' method, as changing this during run-time is will result in errors
299 short int GetCollisionFilterMask() const
301 return m_cci.m_collisionFilterMask;
304 virtual void calcXform() {} ;
305 virtual void SetMargin(float margin) {};
306 virtual float GetMargin() const {return 0.f;};
309 bool wantsSleeping();
311 void UpdateDeactivation(float timeStep);
313 static btTransform GetTransformFromMotionState(PHY_IMotionState* motionState);
315 void setAabb(const btVector3& aabbMin,const btVector3& aabbMax);
318 class PHY_IMotionState* GetMotionState()
320 return m_MotionState;
323 const class PHY_IMotionState* GetMotionState() const
325 return m_MotionState;
328 class CcdPhysicsEnvironment* GetPhysicsEnvironment()
330 return m_cci.m_physicsEnv;
338 ///DefaultMotionState implements standard motionstate, using btTransform
339 class DefaultMotionState : public PHY_IMotionState
343 DefaultMotionState();
345 virtual ~DefaultMotionState();
347 virtual void getWorldPosition(float& posX,float& posY,float& posZ);
348 virtual void getWorldScaling(float& scaleX,float& scaleY,float& scaleZ);
349 virtual void getWorldOrientation(float& quatIma0,float& quatIma1,float& quatIma2,float& quatReal);
351 virtual void setWorldPosition(float posX,float posY,float posZ);
352 virtual void setWorldOrientation(float quatIma0,float quatIma1,float quatIma2,float quatReal);
354 virtual void calculateWorldTransformations();
356 btTransform m_worldTransform;
357 btVector3 m_localScaling;
362 #endif //BULLET2_PHYSICSCONTROLLER_H