bullet: Update to current svn, r2636
[blender.git] / extern / bullet2 / src / BulletCollision / CollisionDispatch / btCollisionObject.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 #ifndef BT_COLLISION_OBJECT_H
17 #define BT_COLLISION_OBJECT_H
18
19 #include "LinearMath/btTransform.h"
20
21 //island management, m_activationState1
22 #define ACTIVE_TAG 1
23 #define ISLAND_SLEEPING 2
24 #define WANTS_DEACTIVATION 3
25 #define DISABLE_DEACTIVATION 4
26 #define DISABLE_SIMULATION 5
27
28 struct  btBroadphaseProxy;
29 class   btCollisionShape;
30 struct btCollisionShapeData;
31 #include "LinearMath/btMotionState.h"
32 #include "LinearMath/btAlignedAllocator.h"
33 #include "LinearMath/btAlignedObjectArray.h"
34
35 typedef btAlignedObjectArray<class btCollisionObject*> btCollisionObjectArray;
36
37 #ifdef BT_USE_DOUBLE_PRECISION
38 #define btCollisionObjectData btCollisionObjectDoubleData
39 #define btCollisionObjectDataName "btCollisionObjectDoubleData"
40 #else
41 #define btCollisionObjectData btCollisionObjectFloatData
42 #define btCollisionObjectDataName "btCollisionObjectFloatData"
43 #endif
44
45
46 /// btCollisionObject can be used to manage collision detection objects. 
47 /// btCollisionObject maintains all information that is needed for a collision detection: Shape, Transform and AABB proxy.
48 /// They can be added to the btCollisionWorld.
49 ATTRIBUTE_ALIGNED16(class)      btCollisionObject
50 {
51
52 protected:
53
54         btTransform     m_worldTransform;
55
56         ///m_interpolationWorldTransform is used for CCD and interpolation
57         ///it can be either previous or future (predicted) transform
58         btTransform     m_interpolationWorldTransform;
59         //those two are experimental: just added for bullet time effect, so you can still apply impulses (directly modifying velocities) 
60         //without destroying the continuous interpolated motion (which uses this interpolation velocities)
61         btVector3       m_interpolationLinearVelocity;
62         btVector3       m_interpolationAngularVelocity;
63         
64         btVector3       m_anisotropicFriction;
65         int                     m_hasAnisotropicFriction;
66         btScalar        m_contactProcessingThreshold;   
67
68         btBroadphaseProxy*              m_broadphaseHandle;
69         btCollisionShape*               m_collisionShape;
70         ///m_extensionPointer is used by some internal low-level Bullet extensions.
71         void*                                   m_extensionPointer;
72         
73         ///m_rootCollisionShape is temporarily used to store the original collision shape
74         ///The m_collisionShape might be temporarily replaced by a child collision shape during collision detection purposes
75         ///If it is NULL, the m_collisionShape is not temporarily replaced.
76         btCollisionShape*               m_rootCollisionShape;
77
78         int                             m_collisionFlags;
79
80         int                             m_islandTag1;
81         int                             m_companionId;
82
83         mutable int                             m_activationState1;
84         mutable btScalar                        m_deactivationTime;
85
86         btScalar                m_friction;
87         btScalar                m_restitution;
88         btScalar                m_rollingFriction;
89
90         ///m_internalType is reserved to distinguish Bullet's btCollisionObject, btRigidBody, btSoftBody, btGhostObject etc.
91         ///do not assign your own m_internalType unless you write a new dynamics object class.
92         int                             m_internalType;
93
94         ///users can point to their objects, m_userPointer is not used by Bullet, see setUserPointer/getUserPointer
95         void*                   m_userObjectPointer;
96
97         ///time of impact calculation
98         btScalar                m_hitFraction; 
99         
100         ///Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
101         btScalar                m_ccdSweptSphereRadius;
102
103         /// Don't do continuous collision detection if the motion (in one step) is less then m_ccdMotionThreshold
104         btScalar                m_ccdMotionThreshold;
105         
106         /// If some object should have elaborate collision filtering by sub-classes
107         int                     m_checkCollideWith;
108
109         virtual bool    checkCollideWithOverride(const btCollisionObject* /* co */) const
110         {
111                 return true;
112         }
113
114 public:
115
116         BT_DECLARE_ALIGNED_ALLOCATOR();
117
118         enum CollisionFlags
119         {
120                 CF_STATIC_OBJECT= 1,
121                 CF_KINEMATIC_OBJECT= 2,
122                 CF_NO_CONTACT_RESPONSE = 4,
123                 CF_CUSTOM_MATERIAL_CALLBACK = 8,//this allows per-triangle material (friction/restitution)
124                 CF_CHARACTER_OBJECT = 16,
125                 CF_DISABLE_VISUALIZE_OBJECT = 32, //disable debug drawing
126                 CF_DISABLE_SPU_COLLISION_PROCESSING = 64//disable parallel/SPU processing
127         };
128
129         enum    CollisionObjectTypes
130         {
131                 CO_COLLISION_OBJECT =1,
132                 CO_RIGID_BODY=2,
133                 ///CO_GHOST_OBJECT keeps track of all objects overlapping its AABB and that pass its collision filter
134                 ///It is useful for collision sensors, explosion objects, character controller etc.
135                 CO_GHOST_OBJECT=4,
136                 CO_SOFT_BODY=8,
137                 CO_HF_FLUID=16,
138                 CO_USER_TYPE=32
139         };
140
141         enum AnisotropicFrictionFlags
142         {
143                 CF_ANISOTROPIC_FRICTION_DISABLED=0,
144                 CF_ANISOTROPIC_FRICTION = 1,
145                 CF_ANISOTROPIC_ROLLING_FRICTION = 2
146         };
147
148         SIMD_FORCE_INLINE bool mergesSimulationIslands() const
149         {
150                 ///static objects, kinematic and object without contact response don't merge islands
151                 return  ((m_collisionFlags & (CF_STATIC_OBJECT | CF_KINEMATIC_OBJECT | CF_NO_CONTACT_RESPONSE) )==0);
152         }
153
154         const btVector3& getAnisotropicFriction() const
155         {
156                 return m_anisotropicFriction;
157         }
158         void    setAnisotropicFriction(const btVector3& anisotropicFriction, int frictionMode = CF_ANISOTROPIC_FRICTION)
159         {
160                 m_anisotropicFriction = anisotropicFriction;
161                 bool isUnity = (anisotropicFriction[0]!=1.f) || (anisotropicFriction[1]!=1.f) || (anisotropicFriction[2]!=1.f);
162                 m_hasAnisotropicFriction = isUnity?frictionMode : 0;
163         }
164         bool    hasAnisotropicFriction(int frictionMode = CF_ANISOTROPIC_FRICTION) const
165         {
166                 return (m_hasAnisotropicFriction&frictionMode)!=0;
167         }
168
169         ///the constraint solver can discard solving contacts, if the distance is above this threshold. 0 by default.
170         ///Note that using contacts with positive distance can improve stability. It increases, however, the chance of colliding with degerate contacts, such as 'interior' triangle edges
171         void    setContactProcessingThreshold( btScalar contactProcessingThreshold)
172         {
173                 m_contactProcessingThreshold = contactProcessingThreshold;
174         }
175         btScalar        getContactProcessingThreshold() const
176         {
177                 return m_contactProcessingThreshold;
178         }
179
180         SIMD_FORCE_INLINE bool          isStaticObject() const {
181                 return (m_collisionFlags & CF_STATIC_OBJECT) != 0;
182         }
183
184         SIMD_FORCE_INLINE bool          isKinematicObject() const
185         {
186                 return (m_collisionFlags & CF_KINEMATIC_OBJECT) != 0;
187         }
188
189         SIMD_FORCE_INLINE bool          isStaticOrKinematicObject() const
190         {
191                 return (m_collisionFlags & (CF_KINEMATIC_OBJECT | CF_STATIC_OBJECT)) != 0 ;
192         }
193
194         SIMD_FORCE_INLINE bool          hasContactResponse() const {
195                 return (m_collisionFlags & CF_NO_CONTACT_RESPONSE)==0;
196         }
197
198         
199         btCollisionObject();
200
201         virtual ~btCollisionObject();
202
203         virtual void    setCollisionShape(btCollisionShape* collisionShape)
204         {
205                 m_collisionShape = collisionShape;
206                 m_rootCollisionShape = collisionShape;
207         }
208
209         SIMD_FORCE_INLINE const btCollisionShape*       getCollisionShape() const
210         {
211                 return m_collisionShape;
212         }
213
214         SIMD_FORCE_INLINE btCollisionShape*     getCollisionShape()
215         {
216                 return m_collisionShape;
217         }
218
219         
220
221         
222
223         ///Avoid using this internal API call, the extension pointer is used by some Bullet extensions. 
224         ///If you need to store your own user pointer, use 'setUserPointer/getUserPointer' instead.
225         void*           internalGetExtensionPointer() const
226         {
227                 return m_extensionPointer;
228         }
229         ///Avoid using this internal API call, the extension pointer is used by some Bullet extensions
230         ///If you need to store your own user pointer, use 'setUserPointer/getUserPointer' instead.
231         void    internalSetExtensionPointer(void* pointer)
232         {
233                 m_extensionPointer = pointer;
234         }
235
236         SIMD_FORCE_INLINE       int     getActivationState() const { return m_activationState1;}
237         
238         void setActivationState(int newState) const;
239
240         void    setDeactivationTime(btScalar time)
241         {
242                 m_deactivationTime = time;
243         }
244         btScalar        getDeactivationTime() const
245         {
246                 return m_deactivationTime;
247         }
248
249         void forceActivationState(int newState) const;
250
251         void    activate(bool forceActivation = false) const;
252
253         SIMD_FORCE_INLINE bool isActive() const
254         {
255                 return ((getActivationState() != ISLAND_SLEEPING) && (getActivationState() != DISABLE_SIMULATION));
256         }
257
258         void    setRestitution(btScalar rest)
259         {
260                 m_restitution = rest;
261         }
262         btScalar        getRestitution() const
263         {
264                 return m_restitution;
265         }
266         void    setFriction(btScalar frict)
267         {
268                 m_friction = frict;
269         }
270         btScalar        getFriction() const
271         {
272                 return m_friction;
273         }
274
275         void    setRollingFriction(btScalar frict)
276         {
277                 m_rollingFriction = frict;
278         }
279         btScalar        getRollingFriction() const
280         {
281                 return m_rollingFriction;
282         }
283
284
285         ///reserved for Bullet internal usage
286         int     getInternalType() const
287         {
288                 return m_internalType;
289         }
290
291         btTransform&    getWorldTransform()
292         {
293                 return m_worldTransform;
294         }
295
296         const btTransform&      getWorldTransform() const
297         {
298                 return m_worldTransform;
299         }
300
301         void    setWorldTransform(const btTransform& worldTrans)
302         {
303                 m_worldTransform = worldTrans;
304         }
305
306
307         SIMD_FORCE_INLINE btBroadphaseProxy*    getBroadphaseHandle()
308         {
309                 return m_broadphaseHandle;
310         }
311
312         SIMD_FORCE_INLINE const btBroadphaseProxy*      getBroadphaseHandle() const
313         {
314                 return m_broadphaseHandle;
315         }
316
317         void    setBroadphaseHandle(btBroadphaseProxy* handle)
318         {
319                 m_broadphaseHandle = handle;
320         }
321
322
323         const btTransform&      getInterpolationWorldTransform() const
324         {
325                 return m_interpolationWorldTransform;
326         }
327
328         btTransform&    getInterpolationWorldTransform()
329         {
330                 return m_interpolationWorldTransform;
331         }
332
333         void    setInterpolationWorldTransform(const btTransform&       trans)
334         {
335                 m_interpolationWorldTransform = trans;
336         }
337
338         void    setInterpolationLinearVelocity(const btVector3& linvel)
339         {
340                 m_interpolationLinearVelocity = linvel;
341         }
342
343         void    setInterpolationAngularVelocity(const btVector3& angvel)
344         {
345                 m_interpolationAngularVelocity = angvel;
346         }
347
348         const btVector3&        getInterpolationLinearVelocity() const
349         {
350                 return m_interpolationLinearVelocity;
351         }
352
353         const btVector3&        getInterpolationAngularVelocity() const
354         {
355                 return m_interpolationAngularVelocity;
356         }
357
358         SIMD_FORCE_INLINE int getIslandTag() const
359         {
360                 return  m_islandTag1;
361         }
362
363         void    setIslandTag(int tag)
364         {
365                 m_islandTag1 = tag;
366         }
367
368         SIMD_FORCE_INLINE int getCompanionId() const
369         {
370                 return  m_companionId;
371         }
372
373         void    setCompanionId(int id)
374         {
375                 m_companionId = id;
376         }
377
378         SIMD_FORCE_INLINE btScalar                      getHitFraction() const
379         {
380                 return m_hitFraction; 
381         }
382
383         void    setHitFraction(btScalar hitFraction)
384         {
385                 m_hitFraction = hitFraction;
386         }
387
388         
389         SIMD_FORCE_INLINE int   getCollisionFlags() const
390         {
391                 return m_collisionFlags;
392         }
393
394         void    setCollisionFlags(int flags)
395         {
396                 m_collisionFlags = flags;
397         }
398         
399         ///Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
400         btScalar                        getCcdSweptSphereRadius() const
401         {
402                 return m_ccdSweptSphereRadius;
403         }
404
405         ///Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
406         void    setCcdSweptSphereRadius(btScalar radius)
407         {
408                 m_ccdSweptSphereRadius = radius;
409         }
410
411         btScalar        getCcdMotionThreshold() const
412         {
413                 return m_ccdMotionThreshold;
414         }
415
416         btScalar        getCcdSquareMotionThreshold() const
417         {
418                 return m_ccdMotionThreshold*m_ccdMotionThreshold;
419         }
420
421
422
423         /// Don't do continuous collision detection if the motion (in one step) is less then m_ccdMotionThreshold
424         void    setCcdMotionThreshold(btScalar ccdMotionThreshold)
425         {
426                 m_ccdMotionThreshold = ccdMotionThreshold;
427         }
428
429         ///users can point to their objects, userPointer is not used by Bullet
430         void*   getUserPointer() const
431         {
432                 return m_userObjectPointer;
433         }
434         
435         ///users can point to their objects, userPointer is not used by Bullet
436         void    setUserPointer(void* userPointer)
437         {
438                 m_userObjectPointer = userPointer;
439         }
440
441
442         inline bool checkCollideWith(const btCollisionObject* co) const
443         {
444                 if (m_checkCollideWith)
445                         return checkCollideWithOverride(co);
446
447                 return true;
448         }
449
450         virtual int     calculateSerializeBufferSize()  const;
451
452         ///fills the dataBuffer and returns the struct name (and 0 on failure)
453         virtual const char*     serialize(void* dataBuffer, class btSerializer* serializer) const;
454
455         virtual void serializeSingleObject(class btSerializer* serializer) const;
456
457 };
458
459 ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
460 struct  btCollisionObjectDoubleData
461 {
462         void                                    *m_broadphaseHandle;
463         void                                    *m_collisionShape;
464         btCollisionShapeData    *m_rootCollisionShape;
465         char                                    *m_name;
466
467         btTransformDoubleData   m_worldTransform;
468         btTransformDoubleData   m_interpolationWorldTransform;
469         btVector3DoubleData             m_interpolationLinearVelocity;
470         btVector3DoubleData             m_interpolationAngularVelocity;
471         btVector3DoubleData             m_anisotropicFriction;
472         double                                  m_contactProcessingThreshold;   
473         double                                  m_deactivationTime;
474         double                                  m_friction;
475         double                                  m_rollingFriction;
476         double                                  m_restitution;
477         double                                  m_hitFraction; 
478         double                                  m_ccdSweptSphereRadius;
479         double                                  m_ccdMotionThreshold;
480
481         int                                             m_hasAnisotropicFriction;
482         int                                             m_collisionFlags;
483         int                                             m_islandTag1;
484         int                                             m_companionId;
485         int                                             m_activationState1;
486         int                                             m_internalType;
487         int                                             m_checkCollideWith;
488
489         char    m_padding[4];
490 };
491
492 ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
493 struct  btCollisionObjectFloatData
494 {
495         void                                    *m_broadphaseHandle;
496         void                                    *m_collisionShape;
497         btCollisionShapeData    *m_rootCollisionShape;
498         char                                    *m_name;
499
500         btTransformFloatData    m_worldTransform;
501         btTransformFloatData    m_interpolationWorldTransform;
502         btVector3FloatData              m_interpolationLinearVelocity;
503         btVector3FloatData              m_interpolationAngularVelocity;
504         btVector3FloatData              m_anisotropicFriction;
505         float                                   m_contactProcessingThreshold;   
506         float                                   m_deactivationTime;
507         float                                   m_friction;
508         float                                   m_rollingFriction;
509
510         float                                   m_restitution;
511         float                                   m_hitFraction; 
512         float                                   m_ccdSweptSphereRadius;
513         float                                   m_ccdMotionThreshold;
514
515         int                                             m_hasAnisotropicFriction;
516         int                                             m_collisionFlags;
517         int                                             m_islandTag1;
518         int                                             m_companionId;
519         int                                             m_activationState1;
520         int                                             m_internalType;
521         int                                             m_checkCollideWith;
522         char                                    m_padding[4];
523 };
524
525
526
527 SIMD_FORCE_INLINE       int     btCollisionObject::calculateSerializeBufferSize() const
528 {
529         return sizeof(btCollisionObjectData);
530 }
531
532
533
534 #endif //BT_COLLISION_OBJECT_H