File indexing completed on 2025-08-03 03:50:02

0001 /*
0002 * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
0003 *
0004 * This software is provided 'as-is', without any express or implied
0005 * warranty.  In no event will the authors be held liable for any damages
0006 * arising from the use of this software.
0007 * Permission is granted to anyone to use this software for any purpose,
0008 * including commercial applications, and to alter it and redistribute it
0009 * freely, subject to the following restrictions:
0010 * 1. The origin of this software must not be misrepresented; you must not
0011 * claim that you wrote the original software. If you use this software
0012 * in a product, an acknowledgment in the product documentation would be
0013 * appreciated but is not required.
0014 * 2. Altered source versions must be plainly marked as such, and must not be
0015 * misrepresented as being the original software.
0016 * 3. This notice may not be removed or altered from any source distribution.
0017 */
0018 
0019 #ifndef B2_WORLD_CALLBACKS_H
0020 #define B2_WORLD_CALLBACKS_H
0021 
0022 #include <Box2D/Common/b2Settings.h>
0023 
0024 struct b2Vec2;
0025 struct b2Transform;
0026 class b2Fixture;
0027 class b2Joint;
0028 class b2Contact;
0029 struct b2ContactResult;
0030 struct b2Manifold;
0031 
0032 /// Joints and fixtures are destroyed when their associated
0033 /// body is destroyed. Implement this listener so that you
0034 /// may nullify references to these joints and shapes.
0035 class b2DestructionListener
0036 {
0037 public:
0038     virtual ~b2DestructionListener() {}
0039 
0040     /// Called when any joint is about to be destroyed due
0041     /// to the destruction of one of its attached bodies.
0042     virtual void SayGoodbye(b2Joint* joint) = 0;
0043 
0044     /// Called when any fixture is about to be destroyed due
0045     /// to the destruction of its parent body.
0046     virtual void SayGoodbye(b2Fixture* fixture) = 0;
0047 };
0048 
0049 /// Implement this class to provide collision filtering. In other words, you can implement
0050 /// this class if you want finer control over contact creation.
0051 class b2ContactFilter
0052 {
0053 public:
0054     virtual ~b2ContactFilter() {}
0055 
0056     /// Return true if contact calculations should be performed between these two shapes.
0057     /// @warning for performance reasons this is only called when the AABBs begin to overlap.
0058     virtual bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB);
0059 };
0060 
0061 /// Contact impulses for reporting. Impulses are used instead of forces because
0062 /// sub-step forces may approach infinity for rigid body collisions. These
0063 /// match up one-to-one with the contact points in b2Manifold.
0064 struct b2ContactImpulse
0065 {
0066     qreal normalImpulses[b2_maxManifoldPoints];
0067     qreal tangentImpulses[b2_maxManifoldPoints];
0068 };
0069 
0070 /// Implement this class to get contact information. You can use these results for
0071 /// things like sounds and game logic. You can also get contact results by
0072 /// traversing the contact lists after the time step. However, you might miss
0073 /// some contacts because continuous physics leads to sub-stepping.
0074 /// Additionally you may receive multiple callbacks for the same contact in a
0075 /// single time step.
0076 /// You should strive to make your callbacks efficient because there may be
0077 /// many callbacks per time step.
0078 /// @warning You cannot create/destroy Box2D entities inside these callbacks.
0079 class b2ContactListener
0080 {
0081 public:
0082     virtual ~b2ContactListener() {}
0083 
0084     /// Called when two fixtures begin to touch.
0085     virtual void BeginContact(b2Contact* contact) { B2_NOT_USED(contact); }
0086 
0087     /// Called when two fixtures cease to touch.
0088     virtual void EndContact(b2Contact* contact) { B2_NOT_USED(contact); }
0089 
0090     /// This is called after a contact is updated. This allows you to inspect a
0091     /// contact before it goes to the solver. If you are careful, you can modify the
0092     /// contact manifold (e.g. disable contact).
0093     /// A copy of the old manifold is provided so that you can detect changes.
0094     /// Note: this is called only for awake bodies.
0095     /// Note: this is called even when the number of contact points is zero.
0096     /// Note: this is not called for sensors.
0097     /// Note: if you set the number of contact points to zero, you will not
0098     /// get an EndContact callback. However, you may get a BeginContact callback
0099     /// the next step.
0100     virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
0101     {
0102         B2_NOT_USED(contact);
0103         B2_NOT_USED(oldManifold);
0104     }
0105 
0106     /// This lets you inspect a contact after the solver is finished. This is useful
0107     /// for inspecting impulses.
0108     /// Note: the contact manifold does not include time of impact impulses, which can be
0109     /// arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly
0110     /// in a separate data structure.
0111     /// Note: this is only called for contacts that are touching, solid, and awake.
0112     virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
0113     {
0114         B2_NOT_USED(contact);
0115         B2_NOT_USED(impulse);
0116     }
0117 };
0118 
0119 /// Callback class for AABB queries.
0120 /// See b2World::Query
0121 class b2QueryCallback
0122 {
0123 public:
0124     virtual ~b2QueryCallback() {}
0125 
0126     /// Called for each fixture found in the query AABB.
0127     /// @return false to terminate the query.
0128     virtual bool ReportFixture(b2Fixture* fixture) = 0;
0129 };
0130 
0131 /// Callback class for ray casts.
0132 /// See b2World::RayCast
0133 class b2RayCastCallback
0134 {
0135 public:
0136     virtual ~b2RayCastCallback() {}
0137 
0138     /// Called for each fixture found in the query. You control how the ray cast
0139     /// proceeds by returning a float:
0140     /// return -1: ignore this fixture and continue
0141     /// return 0: terminate the ray cast
0142     /// return fraction: clip the ray to this point
0143     /// return 1: don't clip the ray and continue
0144     /// @param fixture the fixture hit by the ray
0145     /// @param point the point of initial intersection
0146     /// @param normal the normal vector at the point of intersection
0147     /// @return -1 to filter, 0 to terminate, fraction to clip the ray for
0148     /// closest hit, 1 to continue
0149     virtual qreal ReportFixture(    b2Fixture* fixture, const b2Vec2& point,
0150                                     const b2Vec2& normal, qreal fraction) = 0;
0151 };
0152 
0153 /// Color for debug drawing. Each value has the range [0,1].
0154 struct b2Color
0155 {
0156     b2Color() {}
0157     b2Color(qreal r, qreal g, qreal b) : r(r), g(g), b(b) {}
0158     void Set(qreal ri, qreal gi, qreal bi) { r = ri; g = gi; b = bi; }
0159     qreal r, g, b;
0160 };
0161 
0162 /// Implement and register this class with a b2World to provide debug drawing of physics
0163 /// entities in your game.
0164 class b2DebugDraw
0165 {
0166 public:
0167     b2DebugDraw();
0168 
0169     virtual ~b2DebugDraw() {}
0170 
0171     enum
0172     {
0173         e_shapeBit              = 0x0001, ///< draw shapes
0174         e_jointBit              = 0x0002, ///< draw joint connections
0175         e_aabbBit               = 0x0004, ///< draw axis aligned bounding boxes
0176         e_pairBit               = 0x0008, ///< draw broad-phase pairs
0177         e_centerOfMassBit       = 0x0010  ///< draw center of mass frame
0178     };
0179 
0180     /// Set the drawing flags.
0181     void SetFlags(uint32 flags);
0182 
0183     /// Get the drawing flags.
0184     uint32 GetFlags() const;
0185     
0186     /// Append flags to the current flags.
0187     void AppendFlags(uint32 flags);
0188 
0189     /// Clear flags from the current flags.
0190     void ClearFlags(uint32 flags);
0191 
0192     /// Draw a closed polygon provided in CCW order.
0193     virtual void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) = 0;
0194 
0195     /// Draw a solid closed polygon provided in CCW order.
0196     virtual void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) = 0;
0197 
0198     /// Draw a circle.
0199     virtual void DrawCircle(const b2Vec2& center, qreal radius, const b2Color& color) = 0;
0200     
0201     /// Draw a solid circle.
0202     virtual void DrawSolidCircle(const b2Vec2& center, qreal radius, const b2Vec2& axis, const b2Color& color) = 0;
0203     
0204     /// Draw a line segment.
0205     virtual void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) = 0;
0206 
0207     /// Draw a transform. Choose your own length scale.
0208     /// @param xf a transform.
0209     virtual void DrawTransform(const b2Transform& xf) = 0;
0210 
0211 protected:
0212     uint32 m_drawFlags;
0213 };
0214 
0215 #endif