Merging fluidcontrol to trunk from rev16649 fluidcontrol branch. Code provided by...
[blender-staging.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 COLLISION_OBJECT_H
17 #define 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 #include "LinearMath/btMotionState.h"
31 #include "LinearMath/btAlignedAllocator.h"
32
33
34
35 /// btCollisionObject can be used to manage collision detection objects. 
36 /// btCollisionObject maintains all information that is needed for a collision detection: Shape, Transform and AABB proxy.
37 /// They can be added to the btCollisionWorld.
38 ATTRIBUTE_ALIGNED16(class)      btCollisionObject
39 {
40
41 protected:
42
43         btTransform     m_worldTransform;
44
45         ///m_interpolationWorldTransform is used for CCD and interpolation
46         ///it can be either previous or future (predicted) transform
47         btTransform     m_interpolationWorldTransform;
48         //those two are experimental: just added for bullet time effect, so you can still apply impulses (directly modifying velocities) 
49         //without destroying the continuous interpolated motion (which uses this interpolation velocities)
50         btVector3       m_interpolationLinearVelocity;
51         btVector3       m_interpolationAngularVelocity;
52         btBroadphaseProxy*              m_broadphaseHandle;
53         btCollisionShape*               m_collisionShape;
54         
55         ///m_rootCollisionShape is temporarily used to store the original collision shape
56         ///The m_collisionShape might be temporarily replaced by a child collision shape during collision detection purposes
57         ///If it is NULL, the m_collisionShape is not temporarily replaced.
58         btCollisionShape*               m_rootCollisionShape;
59
60         int                             m_collisionFlags;
61
62         int                             m_islandTag1;
63         int                             m_companionId;
64
65         int                             m_activationState1;
66         btScalar                        m_deactivationTime;
67
68         btScalar                m_friction;
69         btScalar                m_restitution;
70
71         ///users can point to their objects, m_userPointer is not used by Bullet, see setUserPointer/getUserPointer
72         void*                   m_userObjectPointer;
73
74         ///m_internalType is reserved to distinguish Bullet's btCollisionObject, btRigidBody, btSoftBody etc.
75         ///do not assign your own m_internalType unless you write a new dynamics object class.
76         int                             m_internalType;
77
78         ///time of impact calculation
79         btScalar                m_hitFraction; 
80         
81         ///Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
82         btScalar                m_ccdSweptSphereRadius;
83
84         /// Don't do continuous collision detection if the motion (in one step) is less then m_ccdMotionThreshold
85         btScalar                m_ccdMotionThreshold;
86         
87         /// If some object should have elaborate collision filtering by sub-classes
88         bool                    m_checkCollideWith;
89
90         char    m_pad[7];
91
92         virtual bool    checkCollideWithOverride(btCollisionObject* /* co */)
93         {
94                 return true;
95         }
96
97 public:
98
99         BT_DECLARE_ALIGNED_ALLOCATOR();
100
101         enum CollisionFlags
102         {
103                 CF_STATIC_OBJECT= 1,
104                 CF_KINEMATIC_OBJECT= 2,
105                 CF_NO_CONTACT_RESPONSE = 4,
106                 CF_CUSTOM_MATERIAL_CALLBACK = 8//this allows per-triangle material (friction/restitution)
107         };
108
109         enum    CollisionObjectTypes
110         {
111                 CO_COLLISION_OBJECT =1,
112                 CO_RIGID_BODY,
113                 CO_SOFT_BODY
114         };
115
116         SIMD_FORCE_INLINE bool mergesSimulationIslands() const
117         {
118                 ///static objects, kinematic and object without contact response don't merge islands
119                 return  ((m_collisionFlags & (CF_STATIC_OBJECT | CF_KINEMATIC_OBJECT | CF_NO_CONTACT_RESPONSE) )==0);
120         }
121
122
123         SIMD_FORCE_INLINE bool          isStaticObject() const {
124                 return (m_collisionFlags & CF_STATIC_OBJECT) != 0;
125         }
126
127         SIMD_FORCE_INLINE bool          isKinematicObject() const
128         {
129                 return (m_collisionFlags & CF_KINEMATIC_OBJECT) != 0;
130         }
131
132         SIMD_FORCE_INLINE bool          isStaticOrKinematicObject() const
133         {
134                 return (m_collisionFlags & (CF_KINEMATIC_OBJECT | CF_STATIC_OBJECT)) != 0 ;
135         }
136
137         SIMD_FORCE_INLINE bool          hasContactResponse() const {
138                 return (m_collisionFlags & CF_NO_CONTACT_RESPONSE)==0;
139         }
140
141         
142         btCollisionObject();
143
144         virtual ~btCollisionObject();
145
146         virtual void    setCollisionShape(btCollisionShape* collisionShape)
147         {
148                 m_collisionShape = collisionShape;
149                 m_rootCollisionShape = collisionShape;
150         }
151
152         SIMD_FORCE_INLINE const btCollisionShape*       getCollisionShape() const
153         {
154                 return m_collisionShape;
155         }
156
157         SIMD_FORCE_INLINE btCollisionShape*     getCollisionShape()
158         {
159                 return m_collisionShape;
160         }
161
162         SIMD_FORCE_INLINE const btCollisionShape*       getRootCollisionShape() const
163         {
164                 return m_rootCollisionShape;
165         }
166
167         SIMD_FORCE_INLINE btCollisionShape*     getRootCollisionShape()
168         {
169                 return m_rootCollisionShape;
170         }
171
172         ///Avoid using this internal API call
173         ///internalSetTemporaryCollisionShape is used to temporary replace the actual collision shape by a child collision shape.
174         void    internalSetTemporaryCollisionShape(btCollisionShape* collisionShape)
175         {
176                 m_collisionShape = collisionShape;
177         }
178
179         int     getActivationState() const { return m_activationState1;}
180         
181         void setActivationState(int newState);
182
183         void    setDeactivationTime(btScalar time)
184         {
185                 m_deactivationTime = time;
186         }
187         btScalar        getDeactivationTime() const
188         {
189                 return m_deactivationTime;
190         }
191
192         void forceActivationState(int newState);
193
194         void    activate(bool forceActivation = false);
195
196         inline bool isActive() const
197         {
198                 return ((getActivationState() != ISLAND_SLEEPING) && (getActivationState() != DISABLE_SIMULATION));
199         }
200
201         void    setRestitution(btScalar rest)
202         {
203                 m_restitution = rest;
204         }
205         btScalar        getRestitution() const
206         {
207                 return m_restitution;
208         }
209         void    setFriction(btScalar frict)
210         {
211                 m_friction = frict;
212         }
213         btScalar        getFriction() const
214         {
215                 return m_friction;
216         }
217
218         ///reserved for Bullet internal usage
219         int     getInternalType() const
220         {
221                 return m_internalType;
222         }
223
224         btTransform&    getWorldTransform()
225         {
226                 return m_worldTransform;
227         }
228
229         const btTransform&      getWorldTransform() const
230         {
231                 return m_worldTransform;
232         }
233
234         void    setWorldTransform(const btTransform& worldTrans)
235         {
236                 m_worldTransform = worldTrans;
237         }
238
239
240         btBroadphaseProxy*      getBroadphaseHandle()
241         {
242                 return m_broadphaseHandle;
243         }
244
245         const btBroadphaseProxy*        getBroadphaseHandle() const
246         {
247                 return m_broadphaseHandle;
248         }
249
250         void    setBroadphaseHandle(btBroadphaseProxy* handle)
251         {
252                 m_broadphaseHandle = handle;
253         }
254
255
256         const btTransform&      getInterpolationWorldTransform() const
257         {
258                 return m_interpolationWorldTransform;
259         }
260
261         btTransform&    getInterpolationWorldTransform()
262         {
263                 return m_interpolationWorldTransform;
264         }
265
266         void    setInterpolationWorldTransform(const btTransform&       trans)
267         {
268                 m_interpolationWorldTransform = trans;
269         }
270
271         void    setInterpolationLinearVelocity(const btVector3& linvel)
272         {
273                 m_interpolationLinearVelocity = linvel;
274         }
275
276         void    setInterpolationAngularVelocity(const btVector3& angvel)
277         {
278                 m_interpolationAngularVelocity = angvel;
279         }
280
281         const btVector3&        getInterpolationLinearVelocity() const
282         {
283                 return m_interpolationLinearVelocity;
284         }
285
286         const btVector3&        getInterpolationAngularVelocity() const
287         {
288                 return m_interpolationAngularVelocity;
289         }
290
291         const int getIslandTag() const
292         {
293                 return  m_islandTag1;
294         }
295
296         void    setIslandTag(int tag)
297         {
298                 m_islandTag1 = tag;
299         }
300
301         const int getCompanionId() const
302         {
303                 return  m_companionId;
304         }
305
306         void    setCompanionId(int id)
307         {
308                 m_companionId = id;
309         }
310
311         const btScalar                  getHitFraction() const
312         {
313                 return m_hitFraction; 
314         }
315
316         void    setHitFraction(btScalar hitFraction)
317         {
318                 m_hitFraction = hitFraction;
319         }
320
321         
322         const int       getCollisionFlags() const
323         {
324                 return m_collisionFlags;
325         }
326
327         void    setCollisionFlags(int flags)
328         {
329                 m_collisionFlags = flags;
330         }
331         
332         ///Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
333         btScalar                        getCcdSweptSphereRadius() const
334         {
335                 return m_ccdSweptSphereRadius;
336         }
337
338         ///Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
339         void    setCcdSweptSphereRadius(btScalar radius)
340         {
341                 m_ccdSweptSphereRadius = radius;
342         }
343
344         btScalar        getCcdMotionThreshold() const
345         {
346                 return m_ccdMotionThreshold;
347         }
348
349         btScalar        getCcdSquareMotionThreshold() const
350         {
351                 return m_ccdMotionThreshold*m_ccdMotionThreshold;
352         }
353
354
355
356         /// Don't do continuous collision detection if the motion (in one step) is less then m_ccdMotionThreshold
357         void    setCcdMotionThreshold(btScalar ccdMotionThreshold)
358         {
359                 m_ccdMotionThreshold = ccdMotionThreshold*ccdMotionThreshold;
360         }
361
362         ///users can point to their objects, userPointer is not used by Bullet
363         void*   getUserPointer() const
364         {
365                 return m_userObjectPointer;
366         }
367         
368         ///users can point to their objects, userPointer is not used by Bullet
369         void    setUserPointer(void* userPointer)
370         {
371                 m_userObjectPointer = userPointer;
372         }
373
374
375         inline bool checkCollideWith(btCollisionObject* co)
376         {
377                 if (m_checkCollideWith)
378                         return checkCollideWithOverride(co);
379
380                 return true;
381         }
382 };
383
384 #endif //COLLISION_OBJECT_H