1 /* 2 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 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 b2Body; 28 class b2Joint; 29 class b2Contact; 30 struct b2ContactResult; 31 struct b2Manifold; 32 33 /// Joints and fixtures 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 // emscripten - b2DestructionListener: add constructor and make virtual functions non-pure 37 class b2DestructionListener 38 { 39 public: b2DestructionListener()40 b2DestructionListener() {} ~b2DestructionListener()41 virtual ~b2DestructionListener() {} 42 43 /// Called when any joint is about to be destroyed due 44 /// to the destruction of one of its attached bodies. SayGoodbye(b2Joint * joint)45 virtual void SayGoodbye(b2Joint* joint) {} 46 47 /// Called when any fixture is about to be destroyed due 48 /// to the destruction of its parent body. SayGoodbye(b2Fixture * fixture)49 virtual void SayGoodbye(b2Fixture* fixture) {} 50 }; 51 52 /// Implement this class to provide collision filtering. In other words, you can implement 53 /// this class if you want finer control over contact creation. 54 class b2ContactFilter 55 { 56 public: ~b2ContactFilter()57 virtual ~b2ContactFilter() {} 58 59 /// Return true if contact calculations should be performed between these two shapes. 60 /// @warning for performance reasons this is only called when the AABBs begin to overlap. 61 virtual bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB); 62 }; 63 64 /// Contact impulses for reporting. Impulses are used instead of forces because 65 /// sub-step forces may approach infinity for rigid body collisions. These 66 /// match up one-to-one with the contact points in b2Manifold. 67 struct b2ContactImpulse 68 { 69 float32 normalImpulses[b2_maxManifoldPoints]; 70 float32 tangentImpulses[b2_maxManifoldPoints]; 71 int32 count; 72 }; 73 74 /// Implement this class to get contact information. You can use these results for 75 /// things like sounds and game logic. You can also get contact results by 76 /// traversing the contact lists after the time step. However, you might miss 77 /// some contacts because continuous physics leads to sub-stepping. 78 /// Additionally you may receive multiple callbacks for the same contact in a 79 /// single time step. 80 /// You should strive to make your callbacks efficient because there may be 81 /// many callbacks per time step. 82 /// @warning You cannot create/destroy Box2D entities inside these callbacks. 83 // emscripten - b2ContactListener: add constructor and make virtual functions non-pure 84 class b2ContactListener 85 { 86 public: b2ContactListener()87 b2ContactListener() {} ~b2ContactListener()88 virtual ~b2ContactListener() {} 89 90 /// Called when two fixtures begin to touch. BeginContact(b2Contact * contact)91 virtual void BeginContact(b2Contact* contact) { B2_NOT_USED(contact); } 92 93 /// Called when two fixtures cease to touch. EndContact(b2Contact * contact)94 virtual void EndContact(b2Contact* contact) { B2_NOT_USED(contact); } 95 96 /// This is called after a contact is updated. This allows you to inspect a 97 /// contact before it goes to the solver. If you are careful, you can modify the 98 /// contact manifold (e.g. disable contact). 99 /// A copy of the old manifold is provided so that you can detect changes. 100 /// Note: this is called only for awake bodies. 101 /// Note: this is called even when the number of contact points is zero. 102 /// Note: this is not called for sensors. 103 /// Note: if you set the number of contact points to zero, you will not 104 /// get an EndContact callback. However, you may get a BeginContact callback 105 /// the next step. PreSolve(b2Contact * contact,const b2Manifold * oldManifold)106 virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) 107 { 108 B2_NOT_USED(contact); 109 B2_NOT_USED(oldManifold); 110 } 111 112 /// This lets you inspect a contact after the solver is finished. This is useful 113 /// for inspecting impulses. 114 /// Note: the contact manifold does not include time of impact impulses, which can be 115 /// arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly 116 /// in a separate data structure. 117 /// Note: this is only called for contacts that are touching, solid, and awake. PostSolve(b2Contact * contact,const b2ContactImpulse * impulse)118 virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) 119 { 120 B2_NOT_USED(contact); 121 B2_NOT_USED(impulse); 122 } 123 }; 124 125 /// Callback class for AABB queries. 126 /// See b2World::Query 127 // emscripten - b2QueryCallback: add constructor and make virtual functions non-pure 128 class b2QueryCallback 129 { 130 public: b2QueryCallback()131 b2QueryCallback() {} ~b2QueryCallback()132 virtual ~b2QueryCallback() {} 133 134 /// Called for each fixture found in the query AABB. 135 /// @return false to terminate the query. ReportFixture(b2Fixture * fixture)136 virtual bool ReportFixture(b2Fixture* fixture) { return false; } 137 }; 138 139 /// Callback class for ray casts. 140 /// See b2World::RayCast 141 // emscripten - b2RayCastCallback: add constructor and make virtual functions non-pure 142 class b2RayCastCallback 143 { 144 public: b2RayCastCallback()145 b2RayCastCallback() {} ~b2RayCastCallback()146 virtual ~b2RayCastCallback() {} 147 148 /// Called for each fixture found in the query. You control how the ray cast 149 /// proceeds by returning a float: 150 /// return -1: ignore this fixture and continue 151 /// return 0: terminate the ray cast 152 /// return fraction: clip the ray to this point 153 /// return 1: don't clip the ray and continue 154 /// @param fixture the fixture hit by the ray 155 /// @param point the point of initial intersection 156 /// @param normal the normal vector at the point of intersection 157 /// @return -1 to filter, 0 to terminate, fraction to clip the ray for 158 /// closest hit, 1 to continue ReportFixture(b2Fixture * fixture,const b2Vec2 & point,const b2Vec2 & normal,float32 fraction)159 virtual float32 ReportFixture( b2Fixture* fixture, const b2Vec2& point, 160 const b2Vec2& normal, float32 fraction) { return 0; } 161 }; 162 163 #endif 164