Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/bullet/BulletCollision/NarrowPhaseCollision/btPersistentManifold.h @ 2731

Last change on this file since 2731 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

  • Property svn:eol-style set to native
File size: 6.3 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///maximum 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        btScalar        m_contactBreakingThreshold;
58
59       
60        /// sort cached points so most isolated points come first
61        int     sortCachedPoints(const btManifoldPoint& pt);
62
63        int             findContactPoint(const btManifoldPoint* unUsed, int numUnused,const btManifoldPoint& pt);
64
65public:
66
67        BT_DECLARE_ALIGNED_ALLOCATOR();
68
69        int m_index1a;
70
71        btPersistentManifold();
72
73        btPersistentManifold(void* body0,void* body1,int , btScalar contactBreakingThreshold)
74                : m_body0(body0),m_body1(body1),m_cachedPoints(0),
75                m_contactBreakingThreshold(contactBreakingThreshold)
76        {
77               
78        }
79
80        SIMD_FORCE_INLINE void* getBody0() { return m_body0;}
81        SIMD_FORCE_INLINE void* getBody1() { return m_body1;}
82
83        SIMD_FORCE_INLINE const void* getBody0() const { return m_body0;}
84        SIMD_FORCE_INLINE const void* getBody1() const { return m_body1;}
85
86        void    setBodies(void* body0,void* body1)
87        {
88                m_body0 = body0;
89                m_body1 = body1;
90        }
91
92        void clearUserCache(btManifoldPoint& pt);
93
94#ifdef DEBUG_PERSISTENCY
95        void    DebugPersistency();
96#endif //
97       
98        SIMD_FORCE_INLINE int   getNumContacts() const { return m_cachedPoints;}
99
100        SIMD_FORCE_INLINE const btManifoldPoint& getContactPoint(int index) const
101        {
102                btAssert(index < m_cachedPoints);
103                return m_pointCache[index];
104        }
105
106        SIMD_FORCE_INLINE btManifoldPoint& getContactPoint(int index)
107        {
108                btAssert(index < m_cachedPoints);
109                return m_pointCache[index];
110        }
111
112        ///@todo: get this margin from the current physics / collision environment
113        btScalar        getContactBreakingThreshold() const;
114       
115        int getCacheEntry(const btManifoldPoint& newPoint) const;
116
117        int addManifoldPoint( const btManifoldPoint& newPoint);
118
119        void removeContactPoint (int index)
120        {
121                clearUserCache(m_pointCache[index]);
122
123                int lastUsedIndex = getNumContacts() - 1;
124//              m_pointCache[index] = m_pointCache[lastUsedIndex];
125                if(index != lastUsedIndex) 
126                {
127                        m_pointCache[index] = m_pointCache[lastUsedIndex]; 
128                        //get rid of duplicated userPersistentData pointer
129                        m_pointCache[lastUsedIndex].m_userPersistentData = 0;
130                        m_pointCache[lastUsedIndex].m_appliedImpulse = 0.f;
131                        m_pointCache[lastUsedIndex].m_lateralFrictionInitialized = false;
132                        m_pointCache[lastUsedIndex].m_appliedImpulseLateral1 = 0.f;
133                        m_pointCache[lastUsedIndex].m_appliedImpulseLateral2 = 0.f;
134                        m_pointCache[lastUsedIndex].m_lifeTime = 0;
135                }
136
137                btAssert(m_pointCache[lastUsedIndex].m_userPersistentData==0);
138                m_cachedPoints--;
139        }
140        void replaceContactPoint(const btManifoldPoint& newPoint,int insertIndex)
141        {
142                btAssert(validContactDistance(newPoint));
143
144#define MAINTAIN_PERSISTENCY 1
145#ifdef MAINTAIN_PERSISTENCY
146                int     lifeTime = m_pointCache[insertIndex].getLifeTime();
147                btScalar        appliedImpulse = m_pointCache[insertIndex].m_appliedImpulse;
148                btScalar        appliedLateralImpulse1 = m_pointCache[insertIndex].m_appliedImpulseLateral1;
149                btScalar        appliedLateralImpulse2 = m_pointCache[insertIndex].m_appliedImpulseLateral2;
150                               
151                btAssert(lifeTime>=0);
152                void* cache = m_pointCache[insertIndex].m_userPersistentData;
153               
154                m_pointCache[insertIndex] = newPoint;
155
156                m_pointCache[insertIndex].m_userPersistentData = cache;
157                m_pointCache[insertIndex].m_appliedImpulse = appliedImpulse;
158                m_pointCache[insertIndex].m_appliedImpulseLateral1 = appliedLateralImpulse1;
159                m_pointCache[insertIndex].m_appliedImpulseLateral2 = appliedLateralImpulse2;
160               
161                m_pointCache[insertIndex].m_lifeTime = lifeTime;
162#else
163                clearUserCache(m_pointCache[insertIndex]);
164                m_pointCache[insertIndex] = newPoint;
165       
166#endif
167        }
168
169        bool validContactDistance(const btManifoldPoint& pt) const
170        {
171                return pt.m_distance1 <= getContactBreakingThreshold();
172        }
173        /// calculated new worldspace coordinates and depth, and reject points that exceed the collision margin
174        void    refreshContactPoints(  const btTransform& trA,const btTransform& trB);
175
176       
177        SIMD_FORCE_INLINE       void    clearManifold()
178        {
179                int i;
180                for (i=0;i<m_cachedPoints;i++)
181                {
182                        clearUserCache(m_pointCache[i]);
183                }
184                m_cachedPoints = 0;
185        }
186
187
188
189}
190;
191
192
193
194
195
196#endif //PERSISTENT_MANIFOLD_H
Note: See TracBrowser for help on using the repository browser.