first commit
[blok] / Box2D / Source / Dynamics / b2WorldCallbacks.h
1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
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_WORLD_CALLBACKS_H
20 #define B2_WORLD_CALLBACKS_H
21
22 #include "../Common/b2Settings.h"
23
24 struct b2Vec2;
25 struct b2XForm;
26 class b2Shape;
27 class b2Body;
28 class b2Joint;
29 class b2Contact;
30 struct b2ContactPoint;
31 struct b2ContactResult;
32
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
37 {
38 public:
39         virtual ~b2DestructionListener() {}
40
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;
44
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;
48 };
49
50
51 /// This is called when a body's shape passes outside of the world boundary.
52 class b2BoundaryListener
53 {
54 public:
55         virtual ~b2BoundaryListener() {}
56
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;
60 };
61
62
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.
65 class b2ContactFilter
66 {
67 public:
68         virtual ~b2ContactFilter() {}
69
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);
73 };
74
75 /// The default contact filter.
76 extern b2ContactFilter b2_defaultFilter;
77
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
83 /// single time step.
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
89 {
90 public:
91         virtual ~b2ContactListener() {}
92
93         /// Called when a contact point is added. This includes the geometry
94         /// and the forces.
95         virtual void Add(const b2ContactPoint* point) { B2_NOT_USED(point); }
96
97         /// Called when a contact point persists. This includes the geometry
98         /// and the forces.
99         virtual void Persist(const b2ContactPoint* point) { B2_NOT_USED(point); }
100
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); }
104
105         /// Called after a contact point is solved.
106         virtual void Result(const b2ContactResult* point) { B2_NOT_USED(point); }
107 };
108
109 /// Color for debug drawing. Each value has the range [0,1].
110 struct b2Color
111 {
112         b2Color() {}
113         b2Color(float32 r, float32 g, float32 b) : r(r), g(g), b(b) {}
114         float32 r, g, b;
115 };
116
117 /// Implement and register this class with a b2World to provide debug drawing of physics
118 /// entities in your game.
119 class b2DebugDraw
120 {
121 public:
122         b2DebugDraw();
123
124         virtual ~b2DebugDraw() {}
125
126         enum
127         {
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
135         };
136
137         /// Set the drawing flags.
138         void SetFlags(uint32 flags);
139
140         /// Get the drawing flags.
141         uint32 GetFlags() const;
142         
143         /// Append flags to the current flags.
144         void AppendFlags(uint32 flags);
145
146         /// Clear flags from the current flags.
147         void ClearFlags(uint32 flags);
148
149         /// Draw a closed polygon provided in CCW order.
150         virtual void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) = 0;
151
152         /// Draw a solid closed polygon provided in CCW order.
153         virtual void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) = 0;
154
155         /// Draw a circle.
156         virtual void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color) = 0;
157         
158         /// Draw a solid circle.
159         virtual void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color) = 0;
160         
161         /// Draw a line segment.
162         virtual void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) = 0;
163
164         /// Draw a transform. Choose your own length scale.
165         /// @param xf a transform.
166         virtual void DrawXForm(const b2XForm& xf) = 0;
167
168 protected:
169         uint32 m_drawFlags;
170 };
171
172 #endif