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