2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
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.
19 #ifndef B2_WORLD_CALLBACKS_H
20 #define B2_WORLD_CALLBACKS_H
22 #include "../Common/b2Settings.h"
30 struct b2ContactPoint;
31 struct b2ContactResult;
33 /// Joints and shapes are destroyed when their associated
34 /// body is destroyed. Implement this listener so that you
35 /// may nullify references to these joints and shapes.
36 class b2DestructionListener
39 virtual ~b2DestructionListener() {}
41 /// Called when any joint is about to be destroyed due
42 /// to the destruction of one of its attached bodies.
43 virtual void SayGoodbye(b2Joint* joint) = 0;
45 /// Called when any shape is about to be destroyed due
46 /// to the destruction of its parent body.
47 virtual void SayGoodbye(b2Shape* shape) = 0;
51 /// This is called when a body's shape passes outside of the world boundary.
52 class b2BoundaryListener
55 virtual ~b2BoundaryListener() {}
57 /// This is called for each body that leaves the world boundary.
58 /// @warning you can't modify the world inside this callback.
59 virtual void Violation(b2Body* body) = 0;
63 /// Implement this class to provide collision filtering. In other words, you can implement
64 /// this class if you want finer control over contact creation.
68 virtual ~b2ContactFilter() {}
70 /// Return true if contact calculations should be performed between these two shapes.
71 /// @warning for performance reasons this is only called when the AABBs begin to overlap.
72 virtual bool ShouldCollide(b2Shape* shape1, b2Shape* shape2);
75 /// The default contact filter.
76 extern b2ContactFilter b2_defaultFilter;
78 /// Implement this class to get collision results. You can use these results for
79 /// things like sounds and game logic. You can also get contact results by
80 /// traversing the contact lists after the time step. However, you might miss
81 /// some contacts because continuous physics leads to sub-stepping.
82 /// Additionally you may receive multiple callbacks for the same contact in a
84 /// You should strive to make your callbacks efficient because there may be
85 /// many callbacks per time step.
86 /// @warning The contact separation is the last computed value.
87 /// @warning You cannot create/destroy Box2D entities inside these callbacks.
88 class b2ContactListener
91 virtual ~b2ContactListener() {}
93 /// Called when a contact point is added. This includes the geometry
95 virtual void Add(const b2ContactPoint* point) { B2_NOT_USED(point); }
97 /// Called when a contact point persists. This includes the geometry
99 virtual void Persist(const b2ContactPoint* point) { B2_NOT_USED(point); }
101 /// Called when a contact point is removed. This includes the last
102 /// computed geometry and forces.
103 virtual void Remove(const b2ContactPoint* point) { B2_NOT_USED(point); }
105 /// Called after a contact point is solved.
106 virtual void Result(const b2ContactResult* point) { B2_NOT_USED(point); }
109 /// Color for debug drawing. Each value has the range [0,1].
113 b2Color(float32 r, float32 g, float32 b) : r(r), g(g), b(b) {}
117 /// Implement and register this class with a b2World to provide debug drawing of physics
118 /// entities in your game.
124 virtual ~b2DebugDraw() {}
128 e_shapeBit = 0x0001, ///< draw shapes
129 e_jointBit = 0x0002, ///< draw joint connections
130 e_coreShapeBit = 0x0004, ///< draw core (TOI) shapes
131 e_aabbBit = 0x0008, ///< draw axis aligned bounding boxes
132 e_obbBit = 0x0010, ///< draw oriented bounding boxes
133 e_pairBit = 0x0020, ///< draw broad-phase pairs
134 e_centerOfMassBit = 0x0040, ///< draw center of mass frame
137 /// Set the drawing flags.
138 void SetFlags(uint32 flags);
140 /// Get the drawing flags.
141 uint32 GetFlags() const;
143 /// Append flags to the current flags.
144 void AppendFlags(uint32 flags);
146 /// Clear flags from the current flags.
147 void ClearFlags(uint32 flags);
149 /// Draw a closed polygon provided in CCW order.
150 virtual void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) = 0;
152 /// Draw a solid closed polygon provided in CCW order.
153 virtual void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) = 0;
156 virtual void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color) = 0;
158 /// Draw a solid circle.
159 virtual void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color) = 0;
161 /// Draw a line segment.
162 virtual void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) = 0;
164 /// Draw a transform. Choose your own length scale.
165 /// @param xf a transform.
166 virtual void DrawXForm(const b2XForm& xf) = 0;