btCollisionObject.h

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #ifndef BT_COLLISION_OBJECT_H
00017 #define BT_COLLISION_OBJECT_H
00018 
00019 #include "LinearMath/btTransform.h"
00020 
00021 //island management, m_activationState1
00022 #define ACTIVE_TAG 1
00023 #define ISLAND_SLEEPING 2
00024 #define WANTS_DEACTIVATION 3
00025 #define DISABLE_DEACTIVATION 4
00026 #define DISABLE_SIMULATION 5
00027 
00028 struct  btBroadphaseProxy;
00029 class   btCollisionShape;
00030 struct btCollisionShapeData;
00031 #include "LinearMath/btMotionState.h"
00032 #include "LinearMath/btAlignedAllocator.h"
00033 #include "LinearMath/btAlignedObjectArray.h"
00034 
00035 typedef btAlignedObjectArray<class btCollisionObject*> btCollisionObjectArray;
00036 
00037 #ifdef BT_USE_DOUBLE_PRECISION
00038 #define btCollisionObjectData btCollisionObjectDoubleData
00039 #define btCollisionObjectDataName "btCollisionObjectDoubleData"
00040 #else
00041 #define btCollisionObjectData btCollisionObjectFloatData
00042 #define btCollisionObjectDataName "btCollisionObjectFloatData"
00043 #endif
00044 
00045 
00049 ATTRIBUTE_ALIGNED16(class)      btCollisionObject
00050 {
00051 
00052 protected:
00053 
00054         btTransform     m_worldTransform;
00055 
00058         btTransform     m_interpolationWorldTransform;
00059         //those two are experimental: just added for bullet time effect, so you can still apply impulses (directly modifying velocities) 
00060         //without destroying the continuous interpolated motion (which uses this interpolation velocities)
00061         btVector3       m_interpolationLinearVelocity;
00062         btVector3       m_interpolationAngularVelocity;
00063         
00064         btVector3       m_anisotropicFriction;
00065         int                     m_hasAnisotropicFriction;
00066         btScalar        m_contactProcessingThreshold;   
00067 
00068         btBroadphaseProxy*              m_broadphaseHandle;
00069         btCollisionShape*               m_collisionShape;
00071         void*                                   m_extensionPointer;
00072         
00076         btCollisionShape*               m_rootCollisionShape;
00077 
00078         int                             m_collisionFlags;
00079 
00080         int                             m_islandTag1;
00081         int                             m_companionId;
00082 
00083         mutable int                             m_activationState1;
00084         mutable btScalar                        m_deactivationTime;
00085 
00086         btScalar                m_friction;
00087         btScalar                m_restitution;
00088         btScalar                m_rollingFriction;
00089 
00092         int                             m_internalType;
00093 
00095         void*                   m_userObjectPointer;
00096 
00098         btScalar                m_hitFraction; 
00099         
00101         btScalar                m_ccdSweptSphereRadius;
00102 
00104         btScalar                m_ccdMotionThreshold;
00105         
00107         int                     m_checkCollideWith;
00108 
00109         virtual bool    checkCollideWithOverride(const btCollisionObject* /* co */) const
00110         {
00111                 return true;
00112         }
00113 
00114 public:
00115 
00116         BT_DECLARE_ALIGNED_ALLOCATOR();
00117 
00118         enum CollisionFlags
00119         {
00120                 CF_STATIC_OBJECT= 1,
00121                 CF_KINEMATIC_OBJECT= 2,
00122                 CF_NO_CONTACT_RESPONSE = 4,
00123                 CF_CUSTOM_MATERIAL_CALLBACK = 8,//this allows per-triangle material (friction/restitution)
00124                 CF_CHARACTER_OBJECT = 16,
00125                 CF_DISABLE_VISUALIZE_OBJECT = 32, //disable debug drawing
00126                 CF_DISABLE_SPU_COLLISION_PROCESSING = 64//disable parallel/SPU processing
00127         };
00128 
00129         enum    CollisionObjectTypes
00130         {
00131                 CO_COLLISION_OBJECT =1,
00132                 CO_RIGID_BODY=2,
00135                 CO_GHOST_OBJECT=4,
00136                 CO_SOFT_BODY=8,
00137                 CO_HF_FLUID=16,
00138                 CO_USER_TYPE=32
00139         };
00140 
00141         enum AnisotropicFrictionFlags
00142         {
00143                 CF_ANISOTROPIC_FRICTION_DISABLED=0,
00144                 CF_ANISOTROPIC_FRICTION = 1,
00145                 CF_ANISOTROPIC_ROLLING_FRICTION = 2
00146         };
00147 
00148         SIMD_FORCE_INLINE bool mergesSimulationIslands() const
00149         {
00151                 return  ((m_collisionFlags & (CF_STATIC_OBJECT | CF_KINEMATIC_OBJECT | CF_NO_CONTACT_RESPONSE) )==0);
00152         }
00153 
00154         const btVector3& getAnisotropicFriction() const
00155         {
00156                 return m_anisotropicFriction;
00157         }
00158         void    setAnisotropicFriction(const btVector3& anisotropicFriction, int frictionMode = CF_ANISOTROPIC_FRICTION)
00159         {
00160                 m_anisotropicFriction = anisotropicFriction;
00161                 bool isUnity = (anisotropicFriction[0]!=1.f) || (anisotropicFriction[1]!=1.f) || (anisotropicFriction[2]!=1.f);
00162                 m_hasAnisotropicFriction = isUnity?frictionMode : 0;
00163         }
00164         bool    hasAnisotropicFriction(int frictionMode = CF_ANISOTROPIC_FRICTION) const
00165         {
00166                 return (m_hasAnisotropicFriction&frictionMode)!=0;
00167         }
00168 
00171         void    setContactProcessingThreshold( btScalar contactProcessingThreshold)
00172         {
00173                 m_contactProcessingThreshold = contactProcessingThreshold;
00174         }
00175         btScalar        getContactProcessingThreshold() const
00176         {
00177                 return m_contactProcessingThreshold;
00178         }
00179 
00180         SIMD_FORCE_INLINE bool          isStaticObject() const {
00181                 return (m_collisionFlags & CF_STATIC_OBJECT) != 0;
00182         }
00183 
00184         SIMD_FORCE_INLINE bool          isKinematicObject() const
00185         {
00186                 return (m_collisionFlags & CF_KINEMATIC_OBJECT) != 0;
00187         }
00188 
00189         SIMD_FORCE_INLINE bool          isStaticOrKinematicObject() const
00190         {
00191                 return (m_collisionFlags & (CF_KINEMATIC_OBJECT | CF_STATIC_OBJECT)) != 0 ;
00192         }
00193 
00194         SIMD_FORCE_INLINE bool          hasContactResponse() const {
00195                 return (m_collisionFlags & CF_NO_CONTACT_RESPONSE)==0;
00196         }
00197 
00198         
00199         btCollisionObject();
00200 
00201         virtual ~btCollisionObject();
00202 
00203         virtual void    setCollisionShape(btCollisionShape* collisionShape)
00204         {
00205                 m_collisionShape = collisionShape;
00206                 m_rootCollisionShape = collisionShape;
00207         }
00208 
00209         SIMD_FORCE_INLINE const btCollisionShape*       getCollisionShape() const
00210         {
00211                 return m_collisionShape;
00212         }
00213 
00214         SIMD_FORCE_INLINE btCollisionShape*     getCollisionShape()
00215         {
00216                 return m_collisionShape;
00217         }
00218 
00219         
00220 
00221         
00222 
00225         void*           internalGetExtensionPointer() const
00226         {
00227                 return m_extensionPointer;
00228         }
00231         void    internalSetExtensionPointer(void* pointer)
00232         {
00233                 m_extensionPointer = pointer;
00234         }
00235 
00236         SIMD_FORCE_INLINE       int     getActivationState() const { return m_activationState1;}
00237         
00238         void setActivationState(int newState) const;
00239 
00240         void    setDeactivationTime(btScalar time)
00241         {
00242                 m_deactivationTime = time;
00243         }
00244         btScalar        getDeactivationTime() const
00245         {
00246                 return m_deactivationTime;
00247         }
00248 
00249         void forceActivationState(int newState) const;
00250 
00251         void    activate(bool forceActivation = false) const;
00252 
00253         SIMD_FORCE_INLINE bool isActive() const
00254         {
00255                 return ((getActivationState() != ISLAND_SLEEPING) && (getActivationState() != DISABLE_SIMULATION));
00256         }
00257 
00258         void    setRestitution(btScalar rest)
00259         {
00260                 m_restitution = rest;
00261         }
00262         btScalar        getRestitution() const
00263         {
00264                 return m_restitution;
00265         }
00266         void    setFriction(btScalar frict)
00267         {
00268                 m_friction = frict;
00269         }
00270         btScalar        getFriction() const
00271         {
00272                 return m_friction;
00273         }
00274 
00275         void    setRollingFriction(btScalar frict)
00276         {
00277                 m_rollingFriction = frict;
00278         }
00279         btScalar        getRollingFriction() const
00280         {
00281                 return m_rollingFriction;
00282         }
00283 
00284 
00286         int     getInternalType() const
00287         {
00288                 return m_internalType;
00289         }
00290 
00291         btTransform&    getWorldTransform()
00292         {
00293                 return m_worldTransform;
00294         }
00295 
00296         const btTransform&      getWorldTransform() const
00297         {
00298                 return m_worldTransform;
00299         }
00300 
00301         void    setWorldTransform(const btTransform& worldTrans)
00302         {
00303                 m_worldTransform = worldTrans;
00304         }
00305 
00306 
00307         SIMD_FORCE_INLINE btBroadphaseProxy*    getBroadphaseHandle()
00308         {
00309                 return m_broadphaseHandle;
00310         }
00311 
00312         SIMD_FORCE_INLINE const btBroadphaseProxy*      getBroadphaseHandle() const
00313         {
00314                 return m_broadphaseHandle;
00315         }
00316 
00317         void    setBroadphaseHandle(btBroadphaseProxy* handle)
00318         {
00319                 m_broadphaseHandle = handle;
00320         }
00321 
00322 
00323         const btTransform&      getInterpolationWorldTransform() const
00324         {
00325                 return m_interpolationWorldTransform;
00326         }
00327 
00328         btTransform&    getInterpolationWorldTransform()
00329         {
00330                 return m_interpolationWorldTransform;
00331         }
00332 
00333         void    setInterpolationWorldTransform(const btTransform&       trans)
00334         {
00335                 m_interpolationWorldTransform = trans;
00336         }
00337 
00338         void    setInterpolationLinearVelocity(const btVector3& linvel)
00339         {
00340                 m_interpolationLinearVelocity = linvel;
00341         }
00342 
00343         void    setInterpolationAngularVelocity(const btVector3& angvel)
00344         {
00345                 m_interpolationAngularVelocity = angvel;
00346         }
00347 
00348         const btVector3&        getInterpolationLinearVelocity() const
00349         {
00350                 return m_interpolationLinearVelocity;
00351         }
00352 
00353         const btVector3&        getInterpolationAngularVelocity() const
00354         {
00355                 return m_interpolationAngularVelocity;
00356         }
00357 
00358         SIMD_FORCE_INLINE int getIslandTag() const
00359         {
00360                 return  m_islandTag1;
00361         }
00362 
00363         void    setIslandTag(int tag)
00364         {
00365                 m_islandTag1 = tag;
00366         }
00367 
00368         SIMD_FORCE_INLINE int getCompanionId() const
00369         {
00370                 return  m_companionId;
00371         }
00372 
00373         void    setCompanionId(int id)
00374         {
00375                 m_companionId = id;
00376         }
00377 
00378         SIMD_FORCE_INLINE btScalar                      getHitFraction() const
00379         {
00380                 return m_hitFraction; 
00381         }
00382 
00383         void    setHitFraction(btScalar hitFraction)
00384         {
00385                 m_hitFraction = hitFraction;
00386         }
00387 
00388         
00389         SIMD_FORCE_INLINE int   getCollisionFlags() const
00390         {
00391                 return m_collisionFlags;
00392         }
00393 
00394         void    setCollisionFlags(int flags)
00395         {
00396                 m_collisionFlags = flags;
00397         }
00398         
00400         btScalar                        getCcdSweptSphereRadius() const
00401         {
00402                 return m_ccdSweptSphereRadius;
00403         }
00404 
00406         void    setCcdSweptSphereRadius(btScalar radius)
00407         {
00408                 m_ccdSweptSphereRadius = radius;
00409         }
00410 
00411         btScalar        getCcdMotionThreshold() const
00412         {
00413                 return m_ccdMotionThreshold;
00414         }
00415 
00416         btScalar        getCcdSquareMotionThreshold() const
00417         {
00418                 return m_ccdMotionThreshold*m_ccdMotionThreshold;
00419         }
00420 
00421 
00422 
00424         void    setCcdMotionThreshold(btScalar ccdMotionThreshold)
00425         {
00426                 m_ccdMotionThreshold = ccdMotionThreshold;
00427         }
00428 
00430         void*   getUserPointer() const
00431         {
00432                 return m_userObjectPointer;
00433         }
00434         
00436         void    setUserPointer(void* userPointer)
00437         {
00438                 m_userObjectPointer = userPointer;
00439         }
00440 
00441 
00442         inline bool checkCollideWith(const btCollisionObject* co) const
00443         {
00444                 if (m_checkCollideWith)
00445                         return checkCollideWithOverride(co);
00446 
00447                 return true;
00448         }
00449 
00450         virtual int     calculateSerializeBufferSize()  const;
00451 
00453         virtual const char*     serialize(void* dataBuffer, class btSerializer* serializer) const;
00454 
00455         virtual void serializeSingleObject(class btSerializer* serializer) const;
00456 
00457 };
00458 
00460 struct  btCollisionObjectDoubleData
00461 {
00462         void                                    *m_broadphaseHandle;
00463         void                                    *m_collisionShape;
00464         btCollisionShapeData    *m_rootCollisionShape;
00465         char                                    *m_name;
00466 
00467         btTransformDoubleData   m_worldTransform;
00468         btTransformDoubleData   m_interpolationWorldTransform;
00469         btVector3DoubleData             m_interpolationLinearVelocity;
00470         btVector3DoubleData             m_interpolationAngularVelocity;
00471         btVector3DoubleData             m_anisotropicFriction;
00472         double                                  m_contactProcessingThreshold;   
00473         double                                  m_deactivationTime;
00474         double                                  m_friction;
00475         double                                  m_rollingFriction;
00476         double                                  m_restitution;
00477         double                                  m_hitFraction; 
00478         double                                  m_ccdSweptSphereRadius;
00479         double                                  m_ccdMotionThreshold;
00480 
00481         int                                             m_hasAnisotropicFriction;
00482         int                                             m_collisionFlags;
00483         int                                             m_islandTag1;
00484         int                                             m_companionId;
00485         int                                             m_activationState1;
00486         int                                             m_internalType;
00487         int                                             m_checkCollideWith;
00488 
00489         char    m_padding[4];
00490 };
00491 
00493 struct  btCollisionObjectFloatData
00494 {
00495         void                                    *m_broadphaseHandle;
00496         void                                    *m_collisionShape;
00497         btCollisionShapeData    *m_rootCollisionShape;
00498         char                                    *m_name;
00499 
00500         btTransformFloatData    m_worldTransform;
00501         btTransformFloatData    m_interpolationWorldTransform;
00502         btVector3FloatData              m_interpolationLinearVelocity;
00503         btVector3FloatData              m_interpolationAngularVelocity;
00504         btVector3FloatData              m_anisotropicFriction;
00505         float                                   m_contactProcessingThreshold;   
00506         float                                   m_deactivationTime;
00507         float                                   m_friction;
00508         float                                   m_rollingFriction;
00509 
00510         float                                   m_restitution;
00511         float                                   m_hitFraction; 
00512         float                                   m_ccdSweptSphereRadius;
00513         float                                   m_ccdMotionThreshold;
00514 
00515         int                                             m_hasAnisotropicFriction;
00516         int                                             m_collisionFlags;
00517         int                                             m_islandTag1;
00518         int                                             m_companionId;
00519         int                                             m_activationState1;
00520         int                                             m_internalType;
00521         int                                             m_checkCollideWith;
00522         char                                    m_padding[4];
00523 };
00524 
00525 
00526 
00527 SIMD_FORCE_INLINE       int     btCollisionObject::calculateSerializeBufferSize() const
00528 {
00529         return sizeof(btCollisionObjectData);
00530 }
00531 
00532 
00533 
00534 #endif //BT_COLLISION_OBJECT_H