b2WorldCallbacks.h 5.7 KB

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