b2_world_callbacks.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_WORLD_CALLBACKS_H
  19. #define B2_WORLD_CALLBACKS_H
  20. #include "b2_api.h"
  21. #include "b2_settings.h"
  22. struct b2Vec2;
  23. struct b2Transform;
  24. class b2Fixture;
  25. class b2Body;
  26. class b2Joint;
  27. class b2Contact;
  28. struct b2ContactResult;
  29. struct b2Manifold;
  30. /// Joints and fixtures are destroyed when their associated
  31. /// body is destroyed. Implement this listener so that you
  32. /// may nullify references to these joints and shapes.
  33. class B2_API b2DestructionListener
  34. {
  35. public:
  36. virtual ~b2DestructionListener() {}
  37. /// Called when any joint is about to be destroyed due
  38. /// to the destruction of one of its attached bodies.
  39. virtual void SayGoodbye(b2Joint* joint) = 0;
  40. /// Called when any fixture is about to be destroyed due
  41. /// to the destruction of its parent body.
  42. virtual void SayGoodbye(b2Fixture* fixture) = 0;
  43. };
  44. /// Implement this class to provide collision filtering. In other words, you can implement
  45. /// this class if you want finer control over contact creation.
  46. class B2_API b2ContactFilter
  47. {
  48. public:
  49. virtual ~b2ContactFilter() {}
  50. /// Return true if contact calculations should be performed between these two shapes.
  51. /// @warning for performance reasons this is only called when the AABBs begin to overlap.
  52. virtual bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB);
  53. };
  54. /// Contact impulses for reporting. Impulses are used instead of forces because
  55. /// sub-step forces may approach infinity for rigid body collisions. These
  56. /// match up one-to-one with the contact points in b2Manifold.
  57. struct B2_API b2ContactImpulse
  58. {
  59. float normalImpulses[b2_maxManifoldPoints];
  60. float tangentImpulses[b2_maxManifoldPoints];
  61. int32 count;
  62. };
  63. /// Implement this class to get contact information. You can use these results for
  64. /// things like sounds and game logic. You can also get contact results by
  65. /// traversing the contact lists after the time step. However, you might miss
  66. /// some contacts because continuous physics leads to sub-stepping.
  67. /// Additionally you may receive multiple callbacks for the same contact in a
  68. /// single time step.
  69. /// You should strive to make your callbacks efficient because there may be
  70. /// many callbacks per time step.
  71. /// @warning You cannot create/destroy Box2D entities inside these callbacks.
  72. class B2_API b2ContactListener
  73. {
  74. public:
  75. virtual ~b2ContactListener() {}
  76. /// Called when two fixtures begin to touch.
  77. virtual void BeginContact(b2Contact* contact) { B2_NOT_USED(contact); }
  78. /// Called when two fixtures cease to touch.
  79. virtual void EndContact(b2Contact* contact) { B2_NOT_USED(contact); }
  80. /// This is called after a contact is updated. This allows you to inspect a
  81. /// contact before it goes to the solver. If you are careful, you can modify the
  82. /// contact manifold (e.g. disable contact).
  83. /// A copy of the old manifold is provided so that you can detect changes.
  84. /// Note: this is called only for awake bodies.
  85. /// Note: this is called even when the number of contact points is zero.
  86. /// Note: this is not called for sensors.
  87. /// Note: if you set the number of contact points to zero, you will not
  88. /// get an EndContact callback. However, you may get a BeginContact callback
  89. /// the next step.
  90. virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
  91. {
  92. B2_NOT_USED(contact);
  93. B2_NOT_USED(oldManifold);
  94. }
  95. /// This lets you inspect a contact after the solver is finished. This is useful
  96. /// for inspecting impulses.
  97. /// Note: the contact manifold does not include time of impact impulses, which can be
  98. /// arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly
  99. /// in a separate data structure.
  100. /// Note: this is only called for contacts that are touching, solid, and awake.
  101. virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
  102. {
  103. B2_NOT_USED(contact);
  104. B2_NOT_USED(impulse);
  105. }
  106. };
  107. /// Callback class for AABB queries.
  108. /// See b2World::Query
  109. class B2_API b2QueryCallback
  110. {
  111. public:
  112. virtual ~b2QueryCallback() {}
  113. /// Called for each fixture found in the query AABB.
  114. /// @return false to terminate the query.
  115. virtual bool ReportFixture(b2Fixture* fixture) = 0;
  116. };
  117. /// Callback class for ray casts.
  118. /// See b2World::RayCast
  119. class B2_API b2RayCastCallback
  120. {
  121. public:
  122. virtual ~b2RayCastCallback() {}
  123. /// Called for each fixture found in the query. You control how the ray cast
  124. /// proceeds by returning a float:
  125. /// return -1: ignore this fixture and continue
  126. /// return 0: terminate the ray cast
  127. /// return fraction: clip the ray to this point
  128. /// return 1: don't clip the ray and continue
  129. /// @param fixture the fixture hit by the ray
  130. /// @param point the point of initial intersection
  131. /// @param normal the normal vector at the point of intersection
  132. /// @param fraction the fraction along the ray at the point of intersection
  133. /// @return -1 to filter, 0 to terminate, fraction to clip the ray for
  134. /// closest hit, 1 to continue
  135. virtual float ReportFixture( b2Fixture* fixture, const b2Vec2& point,
  136. const b2Vec2& normal, float fraction) = 0;
  137. };
  138. #endif