svn merge -r 16453:16485 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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 square motion (in one step) is less then m_ccdSquareMotionThreshold
85         btScalar                m_ccdSquareMotionThreshold;
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         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
272         const btVector3&        getInterpolationLinearVelocity() const
273         {
274                 return m_interpolationLinearVelocity;
275         }
276
277         const btVector3&        getInterpolationAngularVelocity() const
278         {
279                 return m_interpolationAngularVelocity;
280         }
281
282         const int getIslandTag() const
283         {
284                 return  m_islandTag1;
285         }
286
287         void    setIslandTag(int tag)
288         {
289                 m_islandTag1 = tag;
290         }
291
292         const int getCompanionId() const
293         {
294                 return  m_companionId;
295         }
296
297         void    setCompanionId(int id)
298         {
299                 m_companionId = id;
300         }
301
302         const btScalar                  getHitFraction() const
303         {
304                 return m_hitFraction; 
305         }
306
307         void    setHitFraction(btScalar hitFraction)
308         {
309                 m_hitFraction = hitFraction;
310         }
311
312         
313         const int       getCollisionFlags() const
314         {
315                 return m_collisionFlags;
316         }
317
318         void    setCollisionFlags(int flags)
319         {
320                 m_collisionFlags = flags;
321         }
322         
323         ///Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
324         btScalar                        getCcdSweptSphereRadius() const
325         {
326                 return m_ccdSweptSphereRadius;
327         }
328
329         ///Swept sphere radius (0.0 by default), see btConvexConvexAlgorithm::
330         void    setCcdSweptSphereRadius(btScalar radius)
331         {
332                 m_ccdSweptSphereRadius = radius;
333         }
334
335         btScalar        getCcdSquareMotionThreshold() const
336         {
337                 return m_ccdSquareMotionThreshold;
338         }
339
340
341         /// Don't do continuous collision detection if square motion (in one step) is less then m_ccdSquareMotionThreshold
342         void    setCcdSquareMotionThreshold(btScalar ccdSquareMotionThreshold)
343         {
344                 m_ccdSquareMotionThreshold = ccdSquareMotionThreshold;
345         }
346
347         ///users can point to their objects, userPointer is not used by Bullet
348         void*   getUserPointer() const
349         {
350                 return m_userObjectPointer;
351         }
352         
353         ///users can point to their objects, userPointer is not used by Bullet
354         void    setUserPointer(void* userPointer)
355         {
356                 m_userObjectPointer = userPointer;
357         }
358
359
360         inline bool checkCollideWith(btCollisionObject* co)
361         {
362                 if (m_checkCollideWith)
363                         return checkCollideWithOverride(co);
364
365                 return true;
366         }
367 };
368
369 #endif //COLLISION_OBJECT_H