Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/bullet/BulletCollision/NarrowPhaseCollision/btPersistentManifold.h @ 1983

Last change on this file since 1983 was 1963, checked in by rgrieder, 16 years ago

Added Bullet physics engine.

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#ifndef PERSISTENT_MANIFOLD_H
17#define PERSISTENT_MANIFOLD_H
18
19
20#include "LinearMath/btVector3.h"
21#include "LinearMath/btTransform.h"
22#include "btManifoldPoint.h"
23#include "LinearMath/btAlignedAllocator.h"
24
25struct btCollisionResult;
26
27///contact breaking and merging threshold
28extern btScalar gContactBreakingThreshold;
29
30typedef bool (*ContactDestroyedCallback)(void* userPersistentData);
31typedef bool (*ContactProcessedCallback)(btManifoldPoint& cp,void* body0,void* body1);
32extern ContactDestroyedCallback gContactDestroyedCallback;
33
34
35
36
37#define MANIFOLD_CACHE_SIZE 4
38
39///btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping in the broadphase.
40///Those contact points are created by the collision narrow phase.
41///The cache can be empty, or hold 1,2,3 or 4 points. Some collision algorithms (GJK) might only add one point at a time.
42///updates/refreshes old contact points, and throw them away if necessary (distance becomes too large)
43///reduces the cache to 4 points, when more then 4 points are added, using following rules:
44///the contact point with deepest penetration is always kept, and it tries to maximuze the area covered by the points
45///note that some pairs of objects might have more then one contact manifold.
46ATTRIBUTE_ALIGNED16( class) btPersistentManifold
47{
48
49        btManifoldPoint m_pointCache[MANIFOLD_CACHE_SIZE];
50
51        /// this two body pointers can point to the physics rigidbody class.
52        /// void* will allow any rigidbody class
53        void* m_body0;
54        void* m_body1;
55        int     m_cachedPoints;
56
57       
58        /// sort cached points so most isolated points come first
59        int     sortCachedPoints(const btManifoldPoint& pt);
60
61        int             findContactPoint(const btManifoldPoint* unUsed, int numUnused,const btManifoldPoint& pt);
62
63public:
64
65        BT_DECLARE_ALIGNED_ALLOCATOR();
66
67        int m_index1a;
68
69        btPersistentManifold();
70
71        btPersistentManifold(void* body0,void* body1,int bla)
72                : m_body0(body0),m_body1(body1),m_cachedPoints(0)
73        {
74                (void)bla;
75        }
76
77        SIMD_FORCE_INLINE void* getBody0() { return m_body0;}
78        SIMD_FORCE_INLINE void* getBody1() { return m_body1;}
79
80        SIMD_FORCE_INLINE const void* getBody0() const { return m_body0;}
81        SIMD_FORCE_INLINE const void* getBody1() const { return m_body1;}
82
83        void    setBodies(void* body0,void* body1)
84        {
85                m_body0 = body0;
86                m_body1 = body1;
87        }
88
89        void clearUserCache(btManifoldPoint& pt);
90
91#ifdef DEBUG_PERSISTENCY
92        void    DebugPersistency();
93#endif //
94       
95        SIMD_FORCE_INLINE int   getNumContacts() const { return m_cachedPoints;}
96
97        SIMD_FORCE_INLINE const btManifoldPoint& getContactPoint(int index) const
98        {
99                btAssert(index < m_cachedPoints);
100                return m_pointCache[index];
101        }
102
103        SIMD_FORCE_INLINE btManifoldPoint& getContactPoint(int index)
104        {
105                btAssert(index < m_cachedPoints);
106                return m_pointCache[index];
107        }
108
109        /// todo: get this margin from the current physics / collision environment
110        btScalar        getContactBreakingThreshold() const;
111       
112        int getCacheEntry(const btManifoldPoint& newPoint) const;
113
114        int addManifoldPoint( const btManifoldPoint& newPoint);
115
116        void removeContactPoint (int index)
117        {
118                clearUserCache(m_pointCache[index]);
119
120                int lastUsedIndex = getNumContacts() - 1;
121//              m_pointCache[index] = m_pointCache[lastUsedIndex];
122                if(index != lastUsedIndex) 
123                {
124                        m_pointCache[index] = m_pointCache[lastUsedIndex]; 
125                        //get rid of duplicated userPersistentData pointer
126                        m_pointCache[lastUsedIndex].m_userPersistentData = 0;
127                        m_pointCache[lastUsedIndex].m_appliedImpulse = 0.f;
128                        m_pointCache[lastUsedIndex].m_lateralFrictionInitialized = false;
129                        m_pointCache[lastUsedIndex].m_appliedImpulseLateral1 = 0.f;
130                        m_pointCache[lastUsedIndex].m_appliedImpulseLateral2 = 0.f;
131                        m_pointCache[lastUsedIndex].m_lifeTime = 0;
132                }
133
134                btAssert(m_pointCache[lastUsedIndex].m_userPersistentData==0);
135                m_cachedPoints--;
136        }
137        void replaceContactPoint(const btManifoldPoint& newPoint,int insertIndex)
138        {
139                btAssert(validContactDistance(newPoint));
140
141#define MAINTAIN_PERSISTENCY 1
142#ifdef MAINTAIN_PERSISTENCY
143                int     lifeTime = m_pointCache[insertIndex].getLifeTime();
144                btScalar        appliedImpulse = m_pointCache[insertIndex].m_appliedImpulse;
145                btScalar        appliedLateralImpulse1 = m_pointCache[insertIndex].m_appliedImpulseLateral1;
146                btScalar        appliedLateralImpulse2 = m_pointCache[insertIndex].m_appliedImpulseLateral2;
147                               
148                btAssert(lifeTime>=0);
149                void* cache = m_pointCache[insertIndex].m_userPersistentData;
150               
151                m_pointCache[insertIndex] = newPoint;
152
153                m_pointCache[insertIndex].m_userPersistentData = cache;
154                m_pointCache[insertIndex].m_appliedImpulse = appliedImpulse;
155                m_pointCache[insertIndex].m_appliedImpulseLateral1 = appliedLateralImpulse1;
156                m_pointCache[insertIndex].m_appliedImpulseLateral2 = appliedLateralImpulse2;
157               
158                m_pointCache[insertIndex].m_lifeTime = lifeTime;
159#else
160                clearUserCache(m_pointCache[insertIndex]);
161                m_pointCache[insertIndex] = newPoint;
162       
163#endif
164        }
165
166        bool validContactDistance(const btManifoldPoint& pt) const
167        {
168                return pt.m_distance1 <= getContactBreakingThreshold();
169        }
170        /// calculated new worldspace coordinates and depth, and reject points that exceed the collision margin
171        void    refreshContactPoints(  const btTransform& trA,const btTransform& trB);
172
173       
174        SIMD_FORCE_INLINE       void    clearManifold()
175        {
176                int i;
177                for (i=0;i<m_cachedPoints;i++)
178                {
179                        clearUserCache(m_pointCache[i]);
180                }
181                m_cachedPoints = 0;
182        }
183
184
185
186}
187;
188
189
190
191
192
193#endif //PERSISTENT_MANIFOLD_H
Note: See TracBrowser for help on using the repository browser.