1 /* 2 * Copyright (c) 2006-2009 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 <Box2D/Common/b2Settings.h> 23 24 struct b2Vec2; 25 struct b2Transform; 26 class b2Fixture; 27 class b2Joint; 28 class b2Contact; 29 struct b2ContactResult; 30 struct b2Manifold; 31 32 /// Joints and fixtures are destroyed when their associated 33 /// body is destroyed. Implement this listener so that you 34 /// may nullify references to these joints and shapes. 35 class b2DestructionListener 36 { 37 public: ~b2DestructionListener()38 virtual ~b2DestructionListener() {} 39 40 /// Called when any joint is about to be destroyed due 41 /// to the destruction of one of its attached bodies. 42 virtual void SayGoodbye(b2Joint* joint) = 0; 43 44 /// Called when any fixture is about to be destroyed due 45 /// to the destruction of its parent body. 46 virtual void SayGoodbye(b2Fixture* fixture) = 0; 47 }; 48 49 /// Implement this class to provide collision filtering. In other words, you can implement 50 /// this class if you want finer control over contact creation. 51 class b2ContactFilter 52 { 53 public: ~b2ContactFilter()54 virtual ~b2ContactFilter() {} 55 56 /// Return true if contact calculations should be performed between these two shapes. 57 /// @warning for performance reasons this is only called when the AABBs begin to overlap. 58 virtual bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB); 59 }; 60 61 /// Contact impulses for reporting. Impulses are used instead of forces because 62 /// sub-step forces may approach infinity for rigid body collisions. These 63 /// match up one-to-one with the contact points in b2Manifold. 64 struct b2ContactImpulse 65 { 66 qreal normalImpulses[b2_maxManifoldPoints]; 67 qreal tangentImpulses[b2_maxManifoldPoints]; 68 }; 69 70 /// Implement this class to get contact information. You can use these results for 71 /// things like sounds and game logic. You can also get contact results by 72 /// traversing the contact lists after the time step. However, you might miss 73 /// some contacts because continuous physics leads to sub-stepping. 74 /// Additionally you may receive multiple callbacks for the same contact in a 75 /// single time step. 76 /// You should strive to make your callbacks efficient because there may be 77 /// many callbacks per time step. 78 /// @warning You cannot create/destroy Box2D entities inside these callbacks. 79 class b2ContactListener 80 { 81 public: ~b2ContactListener()82 virtual ~b2ContactListener() {} 83 84 /// Called when two fixtures begin to touch. BeginContact(b2Contact * contact)85 virtual void BeginContact(b2Contact* contact) { B2_NOT_USED(contact); } 86 87 /// Called when two fixtures cease to touch. EndContact(b2Contact * contact)88 virtual void EndContact(b2Contact* contact) { B2_NOT_USED(contact); } 89 90 /// This is called after a contact is updated. This allows you to inspect a 91 /// contact before it goes to the solver. If you are careful, you can modify the 92 /// contact manifold (e.g. disable contact). 93 /// A copy of the old manifold is provided so that you can detect changes. 94 /// Note: this is called only for awake bodies. 95 /// Note: this is called even when the number of contact points is zero. 96 /// Note: this is not called for sensors. 97 /// Note: if you set the number of contact points to zero, you will not 98 /// get an EndContact callback. However, you may get a BeginContact callback 99 /// the next step. PreSolve(b2Contact * contact,const b2Manifold * oldManifold)100 virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) 101 { 102 B2_NOT_USED(contact); 103 B2_NOT_USED(oldManifold); 104 } 105 106 /// This lets you inspect a contact after the solver is finished. This is useful 107 /// for inspecting impulses. 108 /// Note: the contact manifold does not include time of impact impulses, which can be 109 /// arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly 110 /// in a separate data structure. 111 /// Note: this is only called for contacts that are touching, solid, and awake. PostSolve(b2Contact * contact,const b2ContactImpulse * impulse)112 virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) 113 { 114 B2_NOT_USED(contact); 115 B2_NOT_USED(impulse); 116 } 117 }; 118 119 /// Callback class for AABB queries. 120 /// See b2World::Query 121 class b2QueryCallback 122 { 123 public: ~b2QueryCallback()124 virtual ~b2QueryCallback() {} 125 126 /// Called for each fixture found in the query AABB. 127 /// @return false to terminate the query. 128 virtual bool ReportFixture(b2Fixture* fixture) = 0; 129 }; 130 131 /// Callback class for ray casts. 132 /// See b2World::RayCast 133 class b2RayCastCallback 134 { 135 public: ~b2RayCastCallback()136 virtual ~b2RayCastCallback() {} 137 138 /// Called for each fixture found in the query. You control how the ray cast 139 /// proceeds by returning a float: 140 /// return -1: ignore this fixture and continue 141 /// return 0: terminate the ray cast 142 /// return fraction: clip the ray to this point 143 /// return 1: don't clip the ray and continue 144 /// @param fixture the fixture hit by the ray 145 /// @param point the point of initial intersection 146 /// @param normal the normal vector at the point of intersection 147 /// @return -1 to filter, 0 to terminate, fraction to clip the ray for 148 /// closest hit, 1 to continue 149 virtual qreal ReportFixture( b2Fixture* fixture, const b2Vec2& point, 150 const b2Vec2& normal, qreal fraction) = 0; 151 }; 152 153 /// Color for debug drawing. Each value has the range [0,1]. 154 struct b2Color 155 { b2Colorb2Color156 b2Color() {} b2Colorb2Color157 b2Color(qreal r, qreal g, qreal b) : r(r), g(g), b(b) {} Setb2Color158 void Set(qreal ri, qreal gi, qreal bi) { r = ri; g = gi; b = bi; } 159 qreal r, g, b; 160 }; 161 162 /// Implement and register this class with a b2World to provide debug drawing of physics 163 /// entities in your game. 164 class b2DebugDraw 165 { 166 public: 167 b2DebugDraw(); 168 ~b2DebugDraw()169 virtual ~b2DebugDraw() {} 170 171 enum 172 { 173 e_shapeBit = 0x0001, ///< draw shapes 174 e_jointBit = 0x0002, ///< draw joint connections 175 e_aabbBit = 0x0004, ///< draw axis aligned bounding boxes 176 e_pairBit = 0x0008, ///< draw broad-phase pairs 177 e_centerOfMassBit = 0x0010 ///< draw center of mass frame 178 }; 179 180 /// Set the drawing flags. 181 void SetFlags(uint32 flags); 182 183 /// Get the drawing flags. 184 uint32 GetFlags() const; 185 186 /// Append flags to the current flags. 187 void AppendFlags(uint32 flags); 188 189 /// Clear flags from the current flags. 190 void ClearFlags(uint32 flags); 191 192 /// Draw a closed polygon provided in CCW order. 193 virtual void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) = 0; 194 195 /// Draw a solid closed polygon provided in CCW order. 196 virtual void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) = 0; 197 198 /// Draw a circle. 199 virtual void DrawCircle(const b2Vec2& center, qreal radius, const b2Color& color) = 0; 200 201 /// Draw a solid circle. 202 virtual void DrawSolidCircle(const b2Vec2& center, qreal radius, const b2Vec2& axis, const b2Color& color) = 0; 203 204 /// Draw a line segment. 205 virtual void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) = 0; 206 207 /// Draw a transform. Choose your own length scale. 208 /// @param xf a transform. 209 virtual void DrawTransform(const b2Transform& xf) = 0; 210 211 protected: 212 uint32 m_drawFlags; 213 }; 214 215 #endif 216