File indexing completed on 2024-05-19 14:56:33

0001 /*
0002 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
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 b2Body;
0028 class b2Joint;
0029 class b2Contact;
0030 struct b2ContactResult;
0031 struct b2Manifold;
0032 
0033 /// Joints and fixtures are destroyed when their associated
0034 /// body is destroyed. Implement this listener so that you
0035 /// may nullify references to these joints and shapes.
0036 class b2DestructionListener
0037 {
0038 public:
0039     virtual ~b2DestructionListener() {}
0040 
0041     /// Called when any joint is about to be destroyed due
0042     /// to the destruction of one of its attached bodies.
0043     virtual void SayGoodbye(b2Joint* joint) = 0;
0044 
0045     /// Called when any fixture is about to be destroyed due
0046     /// to the destruction of its parent body.
0047     virtual void SayGoodbye(b2Fixture* fixture) = 0;
0048 };
0049 
0050 /// Implement this class to provide collision filtering. In other words, you can implement
0051 /// this class if you want finer control over contact creation.
0052 class b2ContactFilter
0053 {
0054 public:
0055     virtual ~b2ContactFilter() {}
0056 
0057     /// Return true if contact calculations should be performed between these two shapes.
0058     /// @warning for performance reasons this is only called when the AABBs begin to overlap.
0059     virtual bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB);
0060 };
0061 
0062 /// Contact impulses for reporting. Impulses are used instead of forces because
0063 /// sub-step forces may approach infinity for rigid body collisions. These
0064 /// match up one-to-one with the contact points in b2Manifold.
0065 struct b2ContactImpulse
0066 {
0067     float32 normalImpulses[b2_maxManifoldPoints];
0068     float32 tangentImpulses[b2_maxManifoldPoints];
0069     int32 count;
0070 };
0071 
0072 /// Implement this class to get contact information. You can use these results for
0073 /// things like sounds and game logic. You can also get contact results by
0074 /// traversing the contact lists after the time step. However, you might miss
0075 /// some contacts because continuous physics leads to sub-stepping.
0076 /// Additionally you may receive multiple callbacks for the same contact in a
0077 /// single time step.
0078 /// You should strive to make your callbacks efficient because there may be
0079 /// many callbacks per time step.
0080 /// @warning You cannot create/destroy Box2D entities inside these callbacks.
0081 class b2ContactListener
0082 {
0083 public:
0084     virtual ~b2ContactListener() {}
0085 
0086     /// Called when two fixtures begin to touch.
0087     virtual void BeginContact(b2Contact* contact) { B2_NOT_USED(contact); }
0088 
0089     /// Called when two fixtures cease to touch.
0090     virtual void EndContact(b2Contact* contact) { B2_NOT_USED(contact); }
0091 
0092     /// This is called after a contact is updated. This allows you to inspect a
0093     /// contact before it goes to the solver. If you are careful, you can modify the
0094     /// contact manifold (e.g. disable contact).
0095     /// A copy of the old manifold is provided so that you can detect changes.
0096     /// Note: this is called only for awake bodies.
0097     /// Note: this is called even when the number of contact points is zero.
0098     /// Note: this is not called for sensors.
0099     /// Note: if you set the number of contact points to zero, you will not
0100     /// get an EndContact callback. However, you may get a BeginContact callback
0101     /// the next step.
0102     virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
0103     {
0104         B2_NOT_USED(contact);
0105         B2_NOT_USED(oldManifold);
0106     }
0107 
0108     /// This lets you inspect a contact after the solver is finished. This is useful
0109     /// for inspecting impulses.
0110     /// Note: the contact manifold does not include time of impact impulses, which can be
0111     /// arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly
0112     /// in a separate data structure.
0113     /// Note: this is only called for contacts that are touching, solid, and awake.
0114     virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
0115     {
0116         B2_NOT_USED(contact);
0117         B2_NOT_USED(impulse);
0118     }
0119 };
0120 
0121 /// Callback class for AABB queries.
0122 /// See b2World::Query
0123 class b2QueryCallback
0124 {
0125 public:
0126     virtual ~b2QueryCallback() {}
0127 
0128     /// Called for each fixture found in the query AABB.
0129     /// @return false to terminate the query.
0130     virtual bool ReportFixture(b2Fixture* fixture) = 0;
0131 };
0132 
0133 /// Callback class for ray casts.
0134 /// See b2World::RayCast
0135 class b2RayCastCallback
0136 {
0137 public:
0138     virtual ~b2RayCastCallback() {}
0139 
0140     /// Called for each fixture found in the query. You control how the ray cast
0141     /// proceeds by returning a float:
0142     /// return -1: ignore this fixture and continue
0143     /// return 0: terminate the ray cast
0144     /// return fraction: clip the ray to this point
0145     /// return 1: don't clip the ray and continue
0146     /// @param fixture the fixture hit by the ray
0147     /// @param point the point of initial intersection
0148     /// @param normal the normal vector at the point of intersection
0149     /// @return -1 to filter, 0 to terminate, fraction to clip the ray for
0150     /// closest hit, 1 to continue
0151     virtual float32 ReportFixture(  b2Fixture* fixture, const b2Vec2& point,
0152                                     const b2Vec2& normal, float32 fraction) = 0;
0153 };
0154 
0155 #endif