Box2D  2.3.0
A 2D Physics Engine for Games
b2Contact.h
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>
24 #include <Box2D/Collision/Shapes/b2Shape.h>
25 #include <Box2D/Dynamics/b2Fixture.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 {
56  b2ContactCreateFcn* createFcn;
57  b2ContactDestroyFcn* destroyFcn;
58  bool primary;
59 };
60 
67 {
72 };
73 
77 class b2Contact
78 {
79 public:
80 
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 
106  const b2Fixture* GetFixtureA() const;
107 
109  int32 GetChildIndexA() const;
110 
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 
179  static void AddType(b2ContactCreateFcn* createFcn, b2ContactDestroyFcn* destroyFcn,
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 
192  static b2ContactRegister s_registers[b2Shape::e_typeCount][b2Shape::e_typeCount];
193  static bool s_initialized;
194 
195  uint32 m_flags;
196 
197  // World pool and list pointers.
198  b2Contact* m_prev;
199  b2Contact* m_next;
200 
201  // Nodes for connecting bodies.
202  b2ContactEdge m_nodeA;
203  b2ContactEdge m_nodeB;
204 
205  b2Fixture* m_fixtureA;
206  b2Fixture* m_fixtureB;
207 
208  int32 m_indexA;
209  int32 m_indexB;
210 
211  b2Manifold m_manifold;
212 
213  int32 m_toiCount;
214  float32 m_toi;
215 
216  float32 m_friction;
217  float32 m_restitution;
218 
219  float32 m_tangentSpeed;
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 
289 inline int32 b2Contact::GetChildIndexA() const
290 {
291  return m_indexA;
292 }
293 
294 inline const b2Fixture* b2Contact::GetFixtureB() const
295 {
296  return m_fixtureB;
297 }
298 
299 inline int32 b2Contact::GetChildIndexB() const
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 
314 inline float32 b2Contact::GetFriction() const
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 
329 inline float32 b2Contact::GetRestitution() const
330 {
331  return m_restitution;
332 }
333 
335 {
336  m_restitution = b2MixRestitution(m_fixtureA->m_restitution, m_fixtureB->m_restitution);
337 }
338 
339 inline void b2Contact::SetTangentSpeed(float32 speed)
340 {
341  m_tangentSpeed = speed;
342 }
343 
344 inline float32 b2Contact::GetTangentSpeed() const
345 {
346  return m_tangentSpeed;
347 }
348 
349 #endif
Definition: b2Math.h:350
int32 GetChildIndexB() const
Get the child primitive index for fixture B.
Definition: b2Contact.h:299
b2Body * other
provides quick access to the other body attached.
Definition: b2Contact.h:68
This is used to compute the current state of a contact manifold.
Definition: b2Collision.h:110
Definition: b2StackAllocator.h:37
bool IsTouching() const
Is this contact touching?
Definition: b2Contact.h:259
Definition: b2World.h:41
b2Contact * contact
the contact
Definition: b2Contact.h:69
void Initialize(const b2Manifold *manifold, const b2Transform &xfA, float32 radiusA, const b2Transform &xfB, float32 radiusB)
Definition: b2Collision.cpp:22
Definition: b2WorldCallbacks.h:81
Definition: b2BlockAllocator.h:35
Definition: b2Shape.h:42
void SetRestitution(float32 restitution)
Definition: b2Contact.h:324
Definition: b2Contact.h:66
void ResetFriction()
Reset the friction mixture to the default value.
Definition: b2Contact.h:319
int32 GetChildIndexA() const
Get the child primitive index for fixture A.
Definition: b2Contact.h:289
b2Body * GetBody()
Definition: b2Fixture.h:273
b2Shape * GetShape()
Definition: b2Fixture.h:243
void FlagForFiltering()
Flag this contact for filtering. Filtering will occur the next time step.
Definition: b2Contact.h:304
b2Fixture * GetFixtureB()
Get fixture B in this contact.
Definition: b2Contact.h:284
b2Manifold * GetManifold()
Definition: b2Contact.h:222
float32 GetFriction() const
Get the friction.
Definition: b2Contact.h:314
void SetEnabled(bool flag)
Definition: b2Contact.h:242
Definition: b2Contact.h:54
b2Fixture * GetFixtureA()
Get fixture A in this contact.
Definition: b2Contact.h:274
float32 GetRestitution() const
Get the restitution.
Definition: b2Contact.h:329
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
b2ContactEdge * prev
the previous contact edge in the body's contact list
Definition: b2Contact.h:70
Definition: b2Collision.h:93
const b2Transform & GetTransform() const
Definition: b2Body.h:474
void SetFriction(float32 friction)
Definition: b2Contact.h:309
bool IsEnabled() const
Has this contact been disabled?
Definition: b2Contact.h:254
void SetTangentSpeed(float32 speed)
Set the desired tangent speed for a conveyor belt behavior. In meters per second. ...
Definition: b2Contact.h:339
virtual void Evaluate(b2Manifold *manifold, const b2Transform &xfA, const b2Transform &xfB)=0
Evaluate this contact with your own manifold and transforms.
void GetWorldManifold(b2WorldManifold *worldManifold) const
Get the world manifold.
Definition: b2Contact.h:232
void ResetRestitution()
Reset the restitution to the default value.
Definition: b2Contact.h:334
Definition: b2Contact.h:77
Definition: b2ContactManager.h:30
b2Contact * GetNext()
Get the next contact in the world's contact list.
Definition: b2Contact.h:264
Definition: b2ContactSolver.h:69
float32 GetTangentSpeed() const
Get the desired tangent speed. In meters per second.
Definition: b2Contact.h:344
b2ContactEdge * next
the next contact edge in the body's contact list
Definition: b2Contact.h:71
Definition: b2Fixture.h:107