b2Contact.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * 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
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
19 #ifndef B2_CONTACT_H
20 #define B2_CONTACT_H
21 
22 #include <Box2D/Common/b2Math.h>
26 
27 class b2Body;
28 class b2Contact;
29 class b2Fixture;
30 class b2World;
31 class b2BlockAllocator;
32 class b2StackAllocator;
33 class b2ContactListener;
34 
37 inline float32 b2MixFriction(float32 friction1, float32 friction2)
38 {
39  return b2Sqrt(friction1 * friction2);
40 }
41 
44 inline float32 b2MixRestitution(float32 restitution1, float32 restitution2)
45 {
46  return restitution1 > restitution2 ? restitution1 : restitution2;
47 }
48 
49 typedef b2Contact* b2ContactCreateFcn( b2Fixture* fixtureA, int32 indexA,
50  b2Fixture* fixtureB, int32 indexB,
51  b2BlockAllocator* allocator);
52 typedef void b2ContactDestroyFcn(b2Contact* contact, b2BlockAllocator* allocator);
53 
55 {
58  bool primary;
59 };
60 
67 {
72 };
73 
77 class b2Contact
78 {
79 public:
80 
83  b2Manifold* GetManifold();
84  const b2Manifold* GetManifold() const;
85 
87  void GetWorldManifold(b2WorldManifold* worldManifold) const;
88 
90  bool IsTouching() const;
91 
95  void SetEnabled(bool flag);
96 
98  bool IsEnabled() const;
99 
101  b2Contact* GetNext();
102  const b2Contact* GetNext() const;
103 
105  b2Fixture* GetFixtureA();
106  const b2Fixture* GetFixtureA() const;
107 
109  int32 GetChildIndexA() const;
110 
112  b2Fixture* GetFixtureB();
113  const b2Fixture* GetFixtureB() const;
114 
116  int32 GetChildIndexB() const;
117 
120  void SetFriction(float32 friction);
121 
123  float32 GetFriction() const;
124 
126  void ResetFriction();
127 
130  void SetRestitution(float32 restitution);
131 
133  float32 GetRestitution() const;
134 
136  void ResetRestitution();
137 
139  void SetTangentSpeed(float32 speed);
140 
142  float32 GetTangentSpeed() const;
143 
145  virtual void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) = 0;
146 
147 protected:
148  friend class b2ContactManager;
149  friend class b2World;
150  friend class b2ContactSolver;
151  friend class b2Body;
152  friend class b2Fixture;
153 
154  // Flags stored in m_flags
155  enum
156  {
157  // Used when crawling contact graph when forming islands.
158  e_islandFlag = 0x0001,
159 
160  // Set when the shapes are touching.
161  e_touchingFlag = 0x0002,
162 
163  // This contact can be disabled (by user)
164  e_enabledFlag = 0x0004,
165 
166  // This contact needs filtering because a fixture filter was changed.
167  e_filterFlag = 0x0008,
168 
169  // This bullet contact had a TOI event
170  e_bulletHitFlag = 0x0010,
171 
172  // This contact has a valid TOI in m_toi
173  e_toiFlag = 0x0020
174  };
175 
177  void FlagForFiltering();
178 
180  b2Shape::Type typeA, b2Shape::Type typeB);
181  static void InitializeRegisters();
182  static b2Contact* Create(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator);
183  static void Destroy(b2Contact* contact, b2Shape::Type typeA, b2Shape::Type typeB, b2BlockAllocator* allocator);
184  static void Destroy(b2Contact* contact, b2BlockAllocator* allocator);
185 
186  b2Contact() : m_fixtureA(NULL), m_fixtureB(NULL) {}
187  b2Contact(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB);
188  virtual ~b2Contact() {}
189 
190  void Update(b2ContactListener* listener);
191 
193  static bool s_initialized;
194 
196 
197  // World pool and list pointers.
200 
201  // Nodes for connecting bodies.
204 
207 
210 
212 
215 
218 
220 };
221 
223 {
224  return &m_manifold;
225 }
226 
227 inline const b2Manifold* b2Contact::GetManifold() const
228 {
229  return &m_manifold;
230 }
231 
232 inline void b2Contact::GetWorldManifold(b2WorldManifold* worldManifold) const
233 {
234  const b2Body* bodyA = m_fixtureA->GetBody();
235  const b2Body* bodyB = m_fixtureB->GetBody();
236  const b2Shape* shapeA = m_fixtureA->GetShape();
237  const b2Shape* shapeB = m_fixtureB->GetShape();
238 
239  worldManifold->Initialize(&m_manifold, bodyA->GetTransform(), shapeA->m_radius, bodyB->GetTransform(), shapeB->m_radius);
240 }
241 
242 inline void b2Contact::SetEnabled(bool flag)
243 {
244  if (flag)
245  {
246  m_flags |= e_enabledFlag;
247  }
248  else
249  {
250  m_flags &= ~e_enabledFlag;
251  }
252 }
253 
254 inline bool b2Contact::IsEnabled() const
255 {
256  return (m_flags & e_enabledFlag) == e_enabledFlag;
257 }
258 
259 inline bool b2Contact::IsTouching() const
260 {
261  return (m_flags & e_touchingFlag) == e_touchingFlag;
262 }
263 
265 {
266  return m_next;
267 }
268 
269 inline const b2Contact* b2Contact::GetNext() const
270 {
271  return m_next;
272 }
273 
275 {
276  return m_fixtureA;
277 }
278 
279 inline const b2Fixture* b2Contact::GetFixtureA() const
280 {
281  return m_fixtureA;
282 }
283 
285 {
286  return m_fixtureB;
287 }
288 
290 {
291  return m_indexA;
292 }
293 
294 inline const b2Fixture* b2Contact::GetFixtureB() const
295 {
296  return m_fixtureB;
297 }
298 
300 {
301  return m_indexB;
302 }
303 
305 {
306  m_flags |= e_filterFlag;
307 }
308 
309 inline void b2Contact::SetFriction(float32 friction)
310 {
311  m_friction = friction;
312 }
313 
315 {
316  return m_friction;
317 }
318 
320 {
321  m_friction = b2MixFriction(m_fixtureA->m_friction, m_fixtureB->m_friction);
322 }
323 
324 inline void b2Contact::SetRestitution(float32 restitution)
325 {
326  m_restitution = restitution;
327 }
328 
330 {
331  return m_restitution;
332 }
333 
335 {
336  m_restitution = b2MixRestitution(m_fixtureA->m_restitution, m_fixtureB->m_restitution);
337 }
338 
340 {
341  m_tangentSpeed = speed;
342 }
343 
345 {
346  return m_tangentSpeed;
347 }
348 
349 #endif
friend class b2Contact
Definition: b2Fixture.h:202
float32 m_friction
Definition: b2Fixture.h:225
b2Fixture * m_fixtureB
Definition: b2Contact.h:206
void b2ContactDestroyFcn(b2Contact *contact, b2BlockAllocator *allocator)
Definition: b2Contact.h:52
float32 m_toi
Definition: b2Contact.h:214
float32 m_friction
Definition: b2Contact.h:216
void Create(b2BlockAllocator *allocator, b2Body *body, const b2FixtureDef *def)
Definition: b2Fixture.cpp:41
void SetFriction(float32 friction)
Definition: b2Contact.h:309
float32 m_tangentSpeed
Definition: b2Contact.h:219
virtual ~b2Contact()
Definition: b2Contact.h:188
bool IsTouching() const
Is this contact touching?
Definition: b2Contact.h:259
b2Fixture * m_fixtureA
Definition: b2Contact.h:205
b2Contact * GetNext()
Get the next contact in the world&#39;s contact list.
Definition: b2Contact.h:264
void SetEnabled(bool flag)
Definition: b2Contact.h:242
b2Fixture * GetFixtureB()
Get fixture B in this contact.
Definition: b2Contact.h:284
b2Fixture * m_next
Definition: b2Fixture.h:220
float32 b2MixFriction(float32 friction1, float32 friction2)
Definition: b2Contact.h:37
b2Contact * b2ContactCreateFcn(b2Fixture *fixtureA, int32 indexA, b2Fixture *fixtureB, int32 indexB, b2BlockAllocator *allocator)
Definition: b2Contact.h:49
void SetRestitution(float32 restitution)
Definition: b2Contact.h:324
#define b2Sqrt(x)
Definition: b2Math.h:49
bool IsEnabled() const
Has this contact been disabled?
Definition: b2Contact.h:254
float32 m_restitution
Definition: b2Fixture.h:226
float32 m_radius
Definition: b2Shape.h:93
static bool s_initialized
Definition: b2Contact.h:193
b2ContactEdge * next
the next contact edge in the body&#39;s contact list
Definition: b2Contact.h:71
void FlagForFiltering()
Flag this contact for filtering. Filtering will occur the next time step.
Definition: b2Contact.h:304
b2Contact * contact
the contact
Definition: b2Contact.h:69
signed int int32
Definition: b2Settings.h:31
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
int32 GetChildIndexB() const
Get the child primitive index for fixture B.
Definition: b2Contact.h:299
b2Manifold * GetManifold()
Definition: b2Contact.h:222
unsigned int uint32
Definition: b2Settings.h:34
float32 m_restitution
Definition: b2Contact.h:217
int32 m_indexB
Definition: b2Contact.h:209
void Destroy(b2BlockAllocator *allocator)
Definition: b2Fixture.cpp:69
b2ContactCreateFcn * createFcn
Definition: b2Contact.h:56
int32 m_toiCount
Definition: b2Contact.h:213
b2ContactDestroyFcn * destroyFcn
Definition: b2Contact.h:57
b2Fixture * GetFixtureA()
Get fixture A in this contact.
Definition: b2Contact.h:274
b2ContactEdge m_nodeA
Definition: b2Contact.h:202
b2Body * other
provides quick access to the other body attached.
Definition: b2Contact.h:68
b2Contact * m_prev
Definition: b2Contact.h:198
int32 GetChildIndexA() const
Get the child primitive index for fixture A.
Definition: b2Contact.h:289
void Initialize(const b2Manifold *manifold, const b2Transform &xfA, float32 radiusA, const b2Transform &xfB, float32 radiusB)
Definition: b2Collision.cpp:22
b2ContactEdge m_nodeB
Definition: b2Contact.h:203
void SetTangentSpeed(float32 speed)
Set the desired tangent speed for a conveyor belt behavior. In meters per second. ...
Definition: b2Contact.h:339
float32 GetRestitution() const
Get the restitution.
Definition: b2Contact.h:329
void ResetRestitution()
Reset the restitution to the default value.
Definition: b2Contact.h:334
const b2Transform & GetTransform() const
Definition: b2Body.h:474
int32 m_indexA
Definition: b2Contact.h:208
void ResetFriction()
Reset the friction mixture to the default value.
Definition: b2Contact.h:319
b2Manifold m_manifold
Definition: b2Contact.h:211
uint32 m_flags
Definition: b2Contact.h:195
void GetWorldManifold(b2WorldManifold *worldManifold) const
Get the world manifold.
Definition: b2Contact.h:232
float32 b2MixRestitution(float32 restitution1, float32 restitution2)
Definition: b2Contact.h:44
b2Contact * m_next
Definition: b2Contact.h:199
b2ContactEdge * prev
the previous contact edge in the body&#39;s contact list
Definition: b2Contact.h:70
float float32
Definition: b2Settings.h:35
This is used to compute the current state of a contact manifold.
Definition: b2Collision.h:110
float32 GetFriction() const
Get the friction.
Definition: b2Contact.h:314
float32 GetTangentSpeed() const
Get the desired tangent speed. In meters per second.
Definition: b2Contact.h:344


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 19:36:39