47bc9cdfbd7bce7fb48ff7ad3486406d20ad95ed
[blender.git] / source / gameengine / Physics / Bullet / CcdPhysicsController.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
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:
10
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.
14 */
15
16 /** \file CcdPhysicsController.h
17  *  \ingroup physbullet
18  */
19
20
21 #ifndef __CCDPHYSICSCONTROLLER_H__
22 #define __CCDPHYSICSCONTROLLER_H__
23
24 #include <vector>
25 #include <map>
26
27 #include "PHY_IPhysicsController.h"
28
29 ///     PHY_IPhysicsController is the abstract simplified Interface to a physical object.
30 ///     It contains the IMotionState and IDeformableMesh Interfaces.
31 #include "btBulletDynamicsCommon.h"
32 #include "BulletDynamics/Character/btKinematicCharacterController.h"
33 #include "LinearMath/btTransform.h"
34
35 #include "PHY_IMotionState.h"
36 #include "PHY_ICharacter.h"
37
38 extern float gDeactivationTime;
39 extern float gLinearSleepingTreshold;
40 extern float gAngularSleepingTreshold;
41 extern bool gDisableDeactivation;
42 class CcdPhysicsEnvironment;
43 class btMotionState;
44 class RAS_MeshObject;
45 struct DerivedMesh;
46 class btCollisionShape;
47
48
49 #define CCD_BSB_SHAPE_MATCHING  2
50 #define CCD_BSB_BENDING_CONSTRAINTS 8
51 #define CCD_BSB_AERO_VPOINT 16 /* aero model, Vertex normals are oriented toward velocity*/
52 #define CCD_BSB_AERO_VTWOSIDE 32 /* aero model, Vertex normals are flipped to match velocity */
53
54 /* BulletSoftBody.collisionflags */
55 #define CCD_BSB_COL_SDF_RS      2 /* SDF based rigid vs soft */
56 #define CCD_BSB_COL_CL_RS       4 /* Cluster based rigid vs soft */
57 #define CCD_BSB_COL_CL_SS       8 /* Cluster based soft vs soft */
58 #define CCD_BSB_COL_VF_SS       16 /* Vertex/Face based soft vs soft */
59
60
61 // Shape contructor
62 // It contains all the information needed to create a simple bullet shape at runtime
63 class CcdShapeConstructionInfo
64 {
65 public:
66         struct UVco 
67         {
68                 float uv[2];
69         };
70         
71         static CcdShapeConstructionInfo* FindMesh(class RAS_MeshObject* mesh, struct DerivedMesh* dm, bool polytope);
72
73         CcdShapeConstructionInfo() :
74                 m_shapeType(PHY_SHAPE_NONE),
75                 m_radius(1.0),
76                 m_height(1.0),
77                 m_halfExtend(0.f,0.f,0.f),
78                 m_childScale(1.0f,1.0f,1.0f),
79                 m_userData(NULL),
80                 m_refCount(1),
81                 m_meshObject(NULL),
82                 m_unscaledShape(NULL),
83                 m_forceReInstance(false),
84                 m_weldingThreshold1(0.f),
85                 m_shapeProxy(NULL)
86         {
87                 m_childTrans.setIdentity();
88         }
89
90         ~CcdShapeConstructionInfo();
91
92         CcdShapeConstructionInfo* AddRef()
93         { 
94                 m_refCount++;
95                 return this;
96         }
97
98         int Release()
99         {
100                 if (--m_refCount > 0)
101                         return m_refCount;
102                 delete this;
103                 return 0;
104         }
105
106         bool IsUnused(void)
107         {
108                 return (m_meshObject==NULL && m_shapeArray.size() == 0 && m_shapeProxy == NULL);
109         }
110
111         void AddShape(CcdShapeConstructionInfo* shapeInfo);
112
113         btTriangleMeshShape* GetMeshShape(void)
114         {
115                 return (m_unscaledShape);
116         }
117         CcdShapeConstructionInfo* GetChildShape(int i)
118         {
119                 if (i < 0 || i >= (int)m_shapeArray.size())
120                         return NULL;
121
122                 return m_shapeArray.at(i);
123         }
124         int FindChildShape(CcdShapeConstructionInfo* shapeInfo, void* userData)
125         {
126                 if (shapeInfo == NULL)
127                         return -1;
128                 for (int i=0; i<(int)m_shapeArray.size(); i++)
129                 {
130                         CcdShapeConstructionInfo* childInfo = m_shapeArray.at(i);
131                         if ((userData == NULL || userData == childInfo->m_userData) &&
132                                 (childInfo == shapeInfo ||
133                                  (childInfo->m_shapeType == PHY_SHAPE_PROXY && 
134                                   childInfo->m_shapeProxy == shapeInfo)))
135                                 return i;
136                 }
137                 return -1;
138         }
139
140         bool RemoveChildShape(int i)
141         {
142                 if (i < 0 || i >= (int)m_shapeArray.size())
143                         return false;
144                 m_shapeArray.at(i)->Release();
145                 if (i < (int)m_shapeArray.size()-1)
146                         m_shapeArray[i] = m_shapeArray.back();
147                 m_shapeArray.pop_back();
148                 return true;
149         }
150
151         bool SetMesh(class RAS_MeshObject* mesh, struct DerivedMesh* dm, bool polytope);
152         RAS_MeshObject* GetMesh(void)
153         {
154                 return m_meshObject;
155         }
156
157         bool UpdateMesh(class KX_GameObject* gameobj, class RAS_MeshObject* mesh);
158
159
160         bool SetProxy(CcdShapeConstructionInfo* shapeInfo);
161         CcdShapeConstructionInfo* GetProxy(void)
162         {
163                 return m_shapeProxy;
164         }
165
166         btCollisionShape* CreateBulletShape(btScalar margin, bool useGimpact=false, bool useBvh=true);
167
168         // member variables
169         PHY_ShapeType                   m_shapeType;
170         btScalar                                m_radius;
171         btScalar                                m_height;
172         btVector3                               m_halfExtend;
173         btTransform                             m_childTrans;
174         btVector3                               m_childScale;
175         void*                                   m_userData;
176         btAlignedObjectArray<btScalar>  m_vertexArray;  // Contains both vertex array for polytope shape and
177                                                                                         // triangle array for concave mesh shape. Each vertex is 3 consecutive values
178                                                                                         // In this case a triangle is made of 3 consecutive points
179         std::vector<int>                m_polygonIndexArray;    // Contains the array of polygon index in the 
180                                                                                                         // original mesh that correspond to shape triangles.
181                                                                                                         // only set for concave mesh shape.
182         
183         std::vector<int>                m_triFaceArray; // Contains an array of triplets of face indices
184                                                                                         // quads turn into 2 tris
185
186         std::vector<UVco>               m_triFaceUVcoArray;     // Contains an array of pair of UV coordinate for each vertex of faces
187                                                                                                 // quads turn into 2 tris
188
189         void    setVertexWeldingThreshold1(float threshold)
190         {
191                 m_weldingThreshold1  = threshold*threshold;
192         }
193 protected:
194         static std::map<RAS_MeshObject*, CcdShapeConstructionInfo*> m_meshShapeMap;
195         int                                             m_refCount;             // this class is shared between replicas
196                                                                                         // keep track of users so that we can release it 
197         RAS_MeshObject* m_meshObject;                   // Keep a pointer to the original mesh 
198         btBvhTriangleMeshShape* m_unscaledShape;// holds the shared unscale BVH mesh shape, 
199                                                                                         // the actual shape is of type btScaledBvhTriangleMeshShape
200         std::vector<CcdShapeConstructionInfo*> m_shapeArray;    // for compound shapes
201         bool    m_forceReInstance; //use gimpact for concave dynamic/moving collision detection
202         float   m_weldingThreshold1;    //welding closeby vertices together can improve softbody stability etc.
203         CcdShapeConstructionInfo* m_shapeProxy; // only used for PHY_SHAPE_PROXY, pointer to actual shape info
204
205
206 #ifdef WITH_CXX_GUARDEDALLOC
207         MEM_CXX_CLASS_ALLOC_FUNCS("GE:CcdShapeConstructionInfo")
208 #endif
209 };
210
211 struct CcdConstructionInfo
212 {
213
214         ///CollisionFilterGroups provides some optional usage of basic collision filtering
215         ///this is done during broadphase, so very early in the pipeline
216         ///more advanced collision filtering should be done in btCollisionDispatcher::NeedsCollision
217         enum CollisionFilterGroups
218         {
219                 DefaultFilter = 1,
220                 StaticFilter = 2,
221                 KinematicFilter = 4,
222                 DebrisFilter = 8,
223                 SensorFilter = 16,
224                 CharacterFilter = 32,
225                 AllFilter = DefaultFilter | StaticFilter | KinematicFilter | DebrisFilter | SensorFilter | CharacterFilter,
226         };
227
228
229         CcdConstructionInfo()
230             :m_localInertiaTensor(1.f, 1.f, 1.f),
231                 m_gravity(0,0,0),
232                 m_scaling(1.f,1.f,1.f),
233                 m_linearFactor(0.f, 0.f, 0.f),
234                 m_angularFactor(0.f, 0.f, 0.f),
235                 m_mass(0.f),
236                 m_clamp_vel_min(-1.f),
237                 m_clamp_vel_max(-1.f),
238                 m_restitution(0.1f),
239                 m_friction(0.5f),
240                 m_linearDamping(0.1f),
241                 m_angularDamping(0.1f),
242                 m_margin(0.06f),
243                 m_gamesoftFlag(0),
244                 m_soft_linStiff(1.f),
245                 m_soft_angStiff(1.f),
246                 m_soft_volume(1.f),
247                 m_soft_viterations(0),
248                 m_soft_piterations(1),
249                 m_soft_diterations(0),
250                 m_soft_citerations(4),
251                 m_soft_kSRHR_CL(0.1f),
252                 m_soft_kSKHR_CL(1.f),
253                 m_soft_kSSHR_CL(0.5f),
254                 m_soft_kSR_SPLT_CL(0.5f),
255                 m_soft_kSK_SPLT_CL(0.5f),
256                 m_soft_kSS_SPLT_CL(0.5f),
257                 m_soft_kVCF(1.f),
258                 m_soft_kDP(0.f),
259                 m_soft_kDG(0.f),
260                 m_soft_kLF(0.f),
261                 m_soft_kPR(0.f),
262                 m_soft_kVC(0.f),
263                 m_soft_kDF(0.2f),
264                 m_soft_kMT(0),
265                 m_soft_kCHR(1.0f),
266                 m_soft_kKHR(0.1f),
267                 m_soft_kSHR(1.0f),
268                 m_soft_kAHR(0.7f),
269                 m_collisionFlags(0),
270             m_bDyna(false),
271                 m_bRigid(false),
272                 m_bSoft(false),
273                 m_bSensor(false),
274                 m_bCharacter(false),
275                 m_bGimpact(false),
276                 m_collisionFilterGroup(DefaultFilter),
277                 m_collisionFilterMask(AllFilter),
278                 m_collisionShape(0),
279                 m_MotionState(0),
280                 m_shapeInfo(0),
281                 m_physicsEnv(0),
282                 m_inertiaFactor(1.f),
283                 m_do_anisotropic(false),
284                 m_anisotropicFriction(1.f,1.f,1.f),
285                 m_do_fh(false),
286                 m_do_rot_fh(false),
287                 m_fh_spring(0.f),
288                 m_fh_damping(0.f),
289                 m_fh_distance(1.f),
290                 m_fh_normal(false)
291                 // m_contactProcessingThreshold(1e10f)
292         {
293
294         }
295
296         btVector3       m_localInertiaTensor;
297         btVector3       m_gravity;
298         btVector3       m_scaling;
299         btVector3       m_linearFactor;
300         btVector3       m_angularFactor;
301         btScalar        m_mass;
302         btScalar        m_clamp_vel_min;  
303         btScalar        m_clamp_vel_max;  
304         btScalar        m_restitution;
305         btScalar        m_friction;
306         btScalar        m_linearDamping;
307         btScalar        m_angularDamping;
308         btScalar        m_margin;
309
310         ////////////////////
311         float   m_stepHeight;
312         float   m_jumpSpeed;
313         float   m_fallSpeed;
314         
315         int             m_gamesoftFlag;
316         float   m_soft_linStiff;                        /* linear stiffness 0..1 */
317         float   m_soft_angStiff;                /* angular stiffness 0..1 */
318         float   m_soft_volume;                  /* volume preservation 0..1 */
319
320         int             m_soft_viterations;             /* Velocities solver iterations */
321         int             m_soft_piterations;             /* Positions solver iterations */
322         int             m_soft_diterations;             /* Drift solver iterations */
323         int             m_soft_citerations;             /* Cluster solver iterations */
324
325         float   m_soft_kSRHR_CL;                /* Soft vs rigid hardness [0,1] (cluster only) */
326         float   m_soft_kSKHR_CL;                /* Soft vs kinetic hardness [0,1] (cluster only) */
327         float   m_soft_kSSHR_CL;                /* Soft vs soft hardness [0,1] (cluster only) */
328         float   m_soft_kSR_SPLT_CL;     /* Soft vs rigid impulse split [0,1] (cluster only) */
329
330         float   m_soft_kSK_SPLT_CL;     /* Soft vs rigid impulse split [0,1] (cluster only) */
331         float   m_soft_kSS_SPLT_CL;     /* Soft vs rigid impulse split [0,1] (cluster only) */
332         float   m_soft_kVCF;                    /* Velocities correction factor (Baumgarte) */
333         float   m_soft_kDP;                     /* Damping coefficient [0,1] */
334
335         float   m_soft_kDG;                     /* Drag coefficient [0,+inf] */
336         float   m_soft_kLF;                     /* Lift coefficient [0,+inf] */
337         float   m_soft_kPR;                     /* Pressure coefficient [-inf,+inf] */
338         float   m_soft_kVC;                     /* Volume conversation coefficient [0,+inf] */
339
340         float   m_soft_kDF;                     /* Dynamic friction coefficient [0,1] */
341         float   m_soft_kMT;                     /* Pose matching coefficient [0,1] */
342         float   m_soft_kCHR;                    /* Rigid contacts hardness [0,1] */
343         float   m_soft_kKHR;                    /* Kinetic contacts hardness [0,1] */
344
345         float   m_soft_kSHR;                    /* Soft contacts hardness [0,1] */
346         float   m_soft_kAHR;                    /* Anchors hardness [0,1] */
347         int             m_soft_collisionflags;  /* Vertex/Face or Signed Distance Field(SDF) or Clusters, Soft versus Soft or Rigid */
348         int             m_soft_numclusteriterations;    /* number of iterations to refine collision clusters*/
349 ///////////////////
350
351
352
353         int                     m_collisionFlags;
354         bool            m_bDyna;
355         bool            m_bRigid;
356         bool            m_bSoft;
357         bool            m_bSensor;
358         bool            m_bCharacter;
359         bool            m_bGimpact;                     // use Gimpact for mesh body
360
361         ///optional use of collision group/mask:
362         ///only collision with object goups that match the collision mask.
363         ///this is very basic early out. advanced collision filtering should be
364         ///done in the btCollisionDispatcher::NeedsCollision and NeedsResponse
365         ///both values default to 1
366         short int       m_collisionFilterGroup;
367         short int       m_collisionFilterMask;
368
369         ///these pointers are used as argument passing for the CcdPhysicsController constructor
370         ///and not anymore after that
371         class btCollisionShape* m_collisionShape;
372         class PHY_IMotionState* m_MotionState;
373         class CcdShapeConstructionInfo* m_shapeInfo;
374         
375         CcdPhysicsEnvironment*  m_physicsEnv; //needed for self-replication
376         float   m_inertiaFactor;//tweak the inertia (hooked up to Blender 'formfactor'
377         bool    m_do_anisotropic;
378         btVector3 m_anisotropicFriction;
379
380         bool            m_do_fh;                 ///< Should the object have a linear Fh spring?
381         bool            m_do_rot_fh;             ///< Should the object have an angular Fh spring?
382         btScalar        m_fh_spring;             ///< Spring constant (both linear and angular)
383         btScalar        m_fh_damping;            ///< Damping factor (linear and angular) in range [0, 1]
384         btScalar        m_fh_distance;           ///< The range above the surface where Fh is active.    
385         bool            m_fh_normal;             ///< Should the object slide off slopes?
386         float           m_radius;//for fh backwards compatibility
387         
388         ///m_contactProcessingThreshold allows to process contact points with positive distance
389         ///normally only contacts with negative distance (penetration) are solved
390         ///however, rigid body stacking is more stable when positive contacts are still passed into the constraint solver
391         ///this might sometimes lead to collisions with 'internal edges' such as a sliding character controller
392         ///so disable/set m_contactProcessingThreshold to zero for sliding characters etc.
393         // float                m_contactProcessingThreshold;///< Process contacts with positive distance in range [0..INF]
394 };
395
396 class btRigidBody;
397 class btCollisionObject;
398 class btSoftBody;
399 class btPairCachingGhostObject;
400
401 class BlenderBulletCharacterController : public btKinematicCharacterController, public PHY_ICharacter
402 {
403 private:
404         btMotionState* m_motionState;
405         int m_jumps;
406         int m_maxJumps;
407
408 public:
409         BlenderBulletCharacterController(btMotionState *motionState, btPairCachingGhostObject *ghost, btConvexShape* shape, float stepHeight);
410
411         virtual void updateAction(btCollisionWorld *collisionWorld, btScalar dt);
412
413         int getMaxJumps() const;
414
415         void setMaxJumps(int maxJumps);
416
417         int getJumpCount() const;
418
419         virtual bool canJump() const;
420
421         virtual void jump();
422
423         const btVector3& getWalkDirection();
424
425         // PHY_ICharacter interface
426         virtual void Jump()     { jump(); }
427         virtual bool OnGround(){ return onGround(); }
428         virtual float GetGravity() { return getGravity(); }
429         virtual void SetGravity(float gravity) { setGravity(gravity); }
430         virtual int GetMaxJumps() { return getMaxJumps(); }
431         virtual void SetMaxJumps(int maxJumps) { setMaxJumps(maxJumps); }
432         virtual int GetJumpCount() { return getJumpCount(); }
433         virtual void SetWalkDirection(const MT_Vector3& dir)
434         {
435                 btVector3 vec = btVector3(dir[0], dir[1], dir[2]);
436                 setWalkDirection(vec);
437         }
438         virtual MT_Vector3 GetWalkDirection()
439         {
440                 btVector3 vec = getWalkDirection();
441                 return MT_Vector3(vec[0], vec[1], vec[2]);
442         }
443
444 #ifdef WITH_CXX_GUARDEDALLOC
445         using PHY_ICharacter::operator new;
446         using PHY_ICharacter::operator delete;
447 #endif
448 };
449
450 ///CcdPhysicsController is a physics object that supports continuous collision detection and time of impact based physics resolution.
451 class CcdPhysicsController : public PHY_IPhysicsController
452 {
453 protected:
454         btCollisionObject* m_object;
455         BlenderBulletCharacterController* m_characterController;
456         
457
458         class PHY_IMotionState*         m_MotionState;
459         btMotionState*  m_bulletMotionState;
460         class btCollisionShape* m_collisionShape;
461         class CcdShapeConstructionInfo* m_shapeInfo;
462         btCollisionShape* m_bulletChildShape;
463
464         btAlignedObjectArray<btTypedConstraint*> m_ccdConstraintRefs; // keep track of typed constraints referencing this rigid body
465         friend class CcdPhysicsEnvironment;     // needed when updating the controller
466
467         //some book keeping for replication
468         bool    m_softbodyMappingDone;
469         bool    m_softBodyTransformInitialized;
470         bool    m_prototypeTransformInitialized;
471         btTransform     m_softbodyStartTrans;
472
473
474         void*           m_newClientInfo;
475         int                     m_registerCount;        // needed when multiple sensors use the same controller
476         CcdConstructionInfo     m_cci;//needed for replication
477
478         CcdPhysicsController* m_parentCtrl;
479
480         int m_savedCollisionFlags;
481         short m_savedCollisionFilterGroup;
482         short m_savedCollisionFilterMask;
483         MT_Scalar m_savedMass;
484         bool m_savedDyna;
485         bool m_suspended;
486
487
488         void GetWorldOrientation(btMatrix3x3& mat);
489
490         void CreateRigidbody();
491         bool CreateSoftbody();
492         bool CreateCharacterController();
493
494         bool Register() { 
495                 return (m_registerCount++ == 0) ? true : false;
496         }
497         bool Unregister() {
498                 return (--m_registerCount == 0) ? true : false;
499         }
500
501         void addCcdConstraintRef(btTypedConstraint* c);
502         void removeCcdConstraintRef(btTypedConstraint* c);
503         btTypedConstraint* getCcdConstraintRef(int index);
504         int getNumCcdConstraintRefs() const;
505
506         void SetWorldOrientation(const btMatrix3x3& mat);
507         void ForceWorldTransform(const btMatrix3x3& mat, const btVector3& pos);
508
509         public:
510         
511                 int                             m_collisionDelay;
512         
513
514                 CcdPhysicsController (const CcdConstructionInfo& ci);
515
516                 bool DeleteControllerShape();
517                 bool ReplaceControllerShape(btCollisionShape *newShape);
518
519                 virtual ~CcdPhysicsController();
520
521                 CcdConstructionInfo& GetConstructionInfo()
522                 {
523                         return m_cci;
524                 }
525                 const CcdConstructionInfo& GetConstructionInfo() const
526                 {
527                         return m_cci;
528                 }
529
530
531                 btRigidBody* GetRigidBody();
532                 const btRigidBody*      GetRigidBody() const;
533                 btCollisionObject*      GetCollisionObject();
534                 btSoftBody* GetSoftBody();
535                 btKinematicCharacterController* GetCharacterController();
536
537                 CcdShapeConstructionInfo* GetShapeInfo() { return m_shapeInfo; }
538
539                 btCollisionShape*       GetCollisionShape() { 
540                         return m_object->getCollisionShape();
541                 }
542                 ////////////////////////////////////
543                 // PHY_IPhysicsController interface
544                 ////////////////////////////////////
545
546
547                 /**
548                  * SynchronizeMotionStates ynchronizes dynas, kinematic and deformable entities (and do 'late binding')
549                  */
550                 virtual bool            SynchronizeMotionStates(float time);
551                 /**
552                  * WriteMotionStateToDynamics ynchronizes dynas, kinematic and deformable entities (and do 'late binding')
553                  */
554                 
555                 virtual void            WriteMotionStateToDynamics(bool nondynaonly);
556                 virtual void            WriteDynamicsToMotionState();
557
558                 // controller replication
559                 virtual void            PostProcessReplica(class PHY_IMotionState* motionstate,class PHY_IPhysicsController* parentctrl);
560                 virtual void            SetPhysicsEnvironment(class PHY_IPhysicsEnvironment *env);
561
562                 // kinematic methods
563                 virtual void            RelativeTranslate(const MT_Vector3& dloc,bool local);
564                 virtual void            RelativeRotate(const MT_Matrix3x3&rotval, bool local);
565                 virtual MT_Matrix3x3    GetOrientation();
566                 virtual void            SetOrientation(const MT_Matrix3x3& orn);
567                 virtual void            SetPosition(const MT_Vector3& pos);
568                 virtual void            GetPosition(MT_Vector3& pos) const;
569                 virtual void            SetScaling(const MT_Vector3& scale);
570                 virtual void            SetTransform();
571
572                 virtual MT_Scalar       GetMass();
573                 virtual void    SetMass(MT_Scalar newmass);
574                 
575                 // physics methods
576                 virtual void            ApplyImpulse(const MT_Point3& attach, const MT_Vector3& impulsein, bool local);
577                 virtual void            ApplyTorque(const MT_Vector3& torque,bool local);
578                 virtual void            ApplyForce(const MT_Vector3& force,bool local);
579                 virtual void            SetAngularVelocity(const MT_Vector3& ang_vel,bool local);
580                 virtual void            SetLinearVelocity(const MT_Vector3& lin_vel,bool local);
581                 virtual void            Jump();
582                 virtual void            SetActive(bool active);
583
584                 virtual float           GetLinearDamping() const;
585                 virtual float           GetAngularDamping() const;
586                 virtual void            SetLinearDamping(float damping);
587                 virtual void            SetAngularDamping(float damping);
588                 virtual void            SetDamping(float linear, float angular);
589
590                 // reading out information from physics
591                 virtual MT_Vector3      GetLinearVelocity();
592                 virtual MT_Vector3      GetAngularVelocity();
593                 virtual MT_Vector3      GetVelocity(const MT_Point3& posin);
594                 virtual MT_Vector3      GetLocalInertia();
595
596                 // dyna's that are rigidbody are free in orientation, dyna's with non-rigidbody are restricted 
597                 virtual void            SetRigidBody(bool rigid);
598
599                 
600                 virtual void            ResolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ);
601
602                 virtual void            SuspendDynamics(bool ghost);
603                 virtual void            RestoreDynamics();
604
605                 // Shape control
606                 virtual void    AddCompoundChild(PHY_IPhysicsController* child);
607                 virtual void    RemoveCompoundChild(PHY_IPhysicsController* child);
608
609                 // clientinfo for raycasts for example
610                 virtual void*                           GetNewClientInfo();
611                 virtual void                            SetNewClientInfo(void* clientinfo);
612                 virtual PHY_IPhysicsController* GetReplica();
613                 virtual PHY_IPhysicsController* GetReplicaForSensors();
614                 
615                 ///There should be no 'SetCollisionFilterGroup' method, as changing this during run-time is will result in errors
616                 short int       GetCollisionFilterGroup() const
617                 {
618                         return m_cci.m_collisionFilterGroup;
619                 }
620                 ///There should be no 'SetCollisionFilterGroup' method, as changing this during run-time is will result in errors
621                 short int       GetCollisionFilterMask() const
622                 {
623                         return m_cci.m_collisionFilterMask;
624                 }
625
626                 virtual void CalcXform() {}
627                 virtual void SetMargin(float margin) 
628                 {
629                         if (m_collisionShape)
630                                 m_collisionShape->setMargin(btScalar(margin));
631                 }
632                 virtual float GetMargin() const 
633                 {
634                         return (m_collisionShape) ? m_collisionShape->getMargin() : 0.f;
635                 }
636                 virtual float GetRadius() const 
637                 { 
638                         // this is not the actual shape radius, it's only used for Fh support
639                         return m_cci.m_radius;
640                 }
641                 virtual void  SetRadius(float margin) 
642                 {
643                         if (m_collisionShape && m_collisionShape->getShapeType() == SPHERE_SHAPE_PROXYTYPE)
644                         {
645                                 btSphereShape* sphereShape = static_cast<btSphereShape*>(m_collisionShape);
646                                 sphereShape->setUnscaledRadius(margin);
647                         }
648                         m_cci.m_radius = margin;
649                 }
650                 
651                 // velocity clamping
652                 virtual void SetLinVelocityMin(float val) 
653                 {
654                         m_cci.m_clamp_vel_min= val;
655                 }
656                 virtual float GetLinVelocityMin() const 
657                 {
658                         return m_cci.m_clamp_vel_min;
659                 }
660                 virtual void SetLinVelocityMax(float val) 
661                 {
662                         m_cci.m_clamp_vel_max= val;
663                 }
664                 virtual float GetLinVelocityMax() const 
665                 {
666                         return m_cci.m_clamp_vel_max;
667                 }
668
669                 bool    WantsSleeping();
670
671                 void    UpdateDeactivation(float timeStep);
672
673                 void    SetCenterOfMassTransform(btTransform& xform);
674
675                 static btTransform&     GetTransformFromMotionState(PHY_IMotionState* motionState);
676
677                 void    setAabb(const btVector3& aabbMin,const btVector3& aabbMax);
678
679
680                 class   PHY_IMotionState*                       GetMotionState()
681                 {
682                         return m_MotionState;
683                 }
684
685                 const class     PHY_IMotionState*                       GetMotionState() const
686                 {
687                         return m_MotionState;
688                 }
689
690                 class CcdPhysicsEnvironment* GetPhysicsEnvironment()
691                 {
692                         return m_cci.m_physicsEnv;
693                 }
694
695                 void    SetParentCtrl(CcdPhysicsController* parentCtrl)
696                 {
697                         m_parentCtrl = parentCtrl;
698                 }
699
700                 CcdPhysicsController*   GetParentCtrl()
701                 {
702                         return m_parentCtrl;
703                 }
704
705                 const CcdPhysicsController*     GetParentCtrl() const
706                 {
707                         return m_parentCtrl;
708                 }
709
710                 virtual bool IsDynamic()
711                 {
712                         return GetConstructionInfo().m_bDyna;
713                 }
714
715                 virtual bool IsCompound()
716                 {
717                         return GetConstructionInfo().m_shapeInfo->m_shapeType == PHY_SHAPE_COMPOUND;
718                 }
719
720                 virtual bool ReinstancePhysicsShape(KX_GameObject *from_gameobj, RAS_MeshObject* from_meshobj);
721
722                 /* Method to replicate rigid body joint contraints for group instances. */
723                 virtual void ReplicateConstraints(KX_GameObject *gameobj, std::vector<KX_GameObject*> constobj);
724
725 #ifdef WITH_CXX_GUARDEDALLOC
726         MEM_CXX_CLASS_ALLOC_FUNCS("GE:CcdPhysicsController")
727 #endif
728 };
729
730
731
732
733 ///DefaultMotionState implements standard motionstate, using btTransform
734 class   DefaultMotionState : public PHY_IMotionState
735
736 {
737         public:
738                 DefaultMotionState();
739
740                 virtual ~DefaultMotionState();
741
742                 virtual void    GetWorldPosition(float& posX,float& posY,float& posZ);
743                 virtual void    GetWorldScaling(float& scaleX,float& scaleY,float& scaleZ);
744                 virtual void    GetWorldOrientation(float& quatIma0,float& quatIma1,float& quatIma2,float& quatReal);
745                 
746                 virtual void    SetWorldPosition(float posX,float posY,float posZ);
747                 virtual void    SetWorldOrientation(float quatIma0,float quatIma1,float quatIma2,float quatReal);
748                 virtual void    GetWorldOrientation(float* ori);
749                 virtual void    SetWorldOrientation(const float* ori);
750                 
751                 virtual void    CalculateWorldTransformations();
752                 
753                 btTransform     m_worldTransform;
754                 btVector3               m_localScaling;
755         
756         
757 #ifdef WITH_CXX_GUARDEDALLOC
758         MEM_CXX_CLASS_ALLOC_FUNCS("GE:DefaultMotionState")
759 #endif
760 };
761
762
763 #endif  /* __CCDPHYSICSCONTROLLER_H__ */