b2WorldCallbacks.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  3. * Copyright (c) 2013 Google, Inc.
  4. *
  5. * This software is provided 'as-is', without any express or implied
  6. * warranty. In no event will the authors be held liable for any damages
  7. * arising from the use of this software.
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. * 1. The origin of this software must not be misrepresented; you must not
  12. * claim that you wrote the original software. If you use this software
  13. * in a product, an acknowledgment in the product documentation would be
  14. * appreciated but is not required.
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. * 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #ifndef B2_WORLD_CALLBACKS_H
  20. #define B2_WORLD_CALLBACKS_H
  21. #include <Box2D/Common/b2Settings.h>
  22. struct b2Vec2;
  23. struct b2Transform;
  24. class b2Fixture;
  25. class b2Body;
  26. class b2Joint;
  27. class b2Contact;
  28. class b2ParticleSystem;
  29. struct b2ContactResult;
  30. struct b2Manifold;
  31. class b2ParticleGroup;
  32. struct b2ParticleBodyContact;
  33. struct b2ParticleContact;
  34. /// Joints and fixtures are destroyed when their associated
  35. /// body is destroyed. Implement this listener so that you
  36. /// may nullify references to these joints and shapes.
  37. class b2DestructionListener
  38. {
  39. public:
  40. virtual ~b2DestructionListener() {}
  41. /// Called when any joint is about to be destroyed due
  42. /// to the destruction of one of its attached bodies.
  43. virtual void SayGoodbye(b2Joint* joint) = 0;
  44. /// Called when any fixture is about to be destroyed due
  45. /// to the destruction of its parent body.
  46. virtual void SayGoodbye(b2Fixture* fixture) = 0;
  47. /// Called when any particle group is about to be destroyed.
  48. virtual void SayGoodbye(b2ParticleGroup* group)
  49. {
  50. B2_NOT_USED(group);
  51. }
  52. /// Called when a particle is about to be destroyed.
  53. /// The index can be used in conjunction with
  54. /// b2ParticleSystem::GetUserDataBuffer() or
  55. /// b2ParticleSystem::GetParticleHandleFromIndex() to determine which
  56. /// particle has been destroyed.
  57. virtual void SayGoodbye(b2ParticleSystem* particleSystem, int32 index)
  58. {
  59. B2_NOT_USED(particleSystem);
  60. B2_NOT_USED(index);
  61. }
  62. };
  63. /// Implement this class to provide collision filtering. In other words, you can implement
  64. /// this class if you want finer control over contact creation.
  65. class b2ContactFilter
  66. {
  67. public:
  68. virtual ~b2ContactFilter() {}
  69. /// Return true if contact calculations should be performed between these two shapes.
  70. /// @warning for performance reasons this is only called when the AABBs begin to overlap.
  71. virtual bool ShouldCollide(b2Fixture* fixtureA, b2Fixture* fixtureB);
  72. /// Return true if contact calculations should be performed between a
  73. /// fixture and particle. This is only called if the
  74. /// b2_fixtureContactListenerParticle flag is set on the particle.
  75. virtual bool ShouldCollide(b2Fixture* fixture,
  76. b2ParticleSystem* particleSystem,
  77. int32 particleIndex)
  78. {
  79. B2_NOT_USED(fixture);
  80. B2_NOT_USED(particleIndex);
  81. B2_NOT_USED(particleSystem);
  82. return true;
  83. }
  84. /// Return true if contact calculations should be performed between two
  85. /// particles. This is only called if the
  86. /// b2_particleContactListenerParticle flag is set on the particle.
  87. virtual bool ShouldCollide(b2ParticleSystem* particleSystem,
  88. int32 particleIndexA, int32 particleIndexB)
  89. {
  90. B2_NOT_USED(particleSystem);
  91. B2_NOT_USED(particleIndexA);
  92. B2_NOT_USED(particleIndexB);
  93. return true;
  94. }
  95. };
  96. /// Contact impulses for reporting. Impulses are used instead of forces because
  97. /// sub-step forces may approach infinity for rigid body collisions. These
  98. /// match up one-to-one with the contact points in b2Manifold.
  99. struct b2ContactImpulse
  100. {
  101. float32 normalImpulses[b2_maxManifoldPoints];
  102. float32 tangentImpulses[b2_maxManifoldPoints];
  103. int32 count;
  104. };
  105. /// Implement this class to get contact information. You can use these results for
  106. /// things like sounds and game logic. You can also get contact results by
  107. /// traversing the contact lists after the time step. However, you might miss
  108. /// some contacts because continuous physics leads to sub-stepping.
  109. /// Additionally you may receive multiple callbacks for the same contact in a
  110. /// single time step.
  111. /// You should strive to make your callbacks efficient because there may be
  112. /// many callbacks per time step.
  113. /// @warning You cannot create/destroy Box2D entities inside these callbacks.
  114. class b2ContactListener
  115. {
  116. public:
  117. virtual ~b2ContactListener() {}
  118. /// Called when two fixtures begin to touch.
  119. virtual void BeginContact(b2Contact* contact) { B2_NOT_USED(contact); }
  120. /// Called when two fixtures cease to touch.
  121. virtual void EndContact(b2Contact* contact) { B2_NOT_USED(contact); }
  122. /// Called when a fixture and particle start touching if the
  123. /// b2_fixtureContactFilterParticle flag is set on the particle.
  124. virtual void BeginContact(b2ParticleSystem* particleSystem,
  125. b2ParticleBodyContact* particleBodyContact)
  126. {
  127. B2_NOT_USED(particleSystem);
  128. B2_NOT_USED(particleBodyContact);
  129. }
  130. /// Called when a fixture and particle stop touching if the
  131. /// b2_fixtureContactFilterParticle flag is set on the particle.
  132. virtual void EndContact(b2Fixture* fixture,
  133. b2ParticleSystem* particleSystem, int32 index)
  134. {
  135. B2_NOT_USED(fixture);
  136. B2_NOT_USED(particleSystem);
  137. B2_NOT_USED(index);
  138. }
  139. /// Called when two particles start touching if
  140. /// b2_particleContactFilterParticle flag is set on either particle.
  141. virtual void BeginContact(b2ParticleSystem* particleSystem,
  142. b2ParticleContact* particleContact)
  143. {
  144. B2_NOT_USED(particleSystem);
  145. B2_NOT_USED(particleContact);
  146. }
  147. /// Called when two particles start touching if
  148. /// b2_particleContactFilterParticle flag is set on either particle.
  149. virtual void EndContact(b2ParticleSystem* particleSystem,
  150. int32 indexA, int32 indexB)
  151. {
  152. B2_NOT_USED(particleSystem);
  153. B2_NOT_USED(indexA);
  154. B2_NOT_USED(indexB);
  155. }
  156. /// This is called after a contact is updated. This allows you to inspect a
  157. /// contact before it goes to the solver. If you are careful, you can modify the
  158. /// contact manifold (e.g. disable contact).
  159. /// A copy of the old manifold is provided so that you can detect changes.
  160. /// Note: this is called only for awake bodies.
  161. /// Note: this is called even when the number of contact points is zero.
  162. /// Note: this is not called for sensors.
  163. /// Note: if you set the number of contact points to zero, you will not
  164. /// get an EndContact callback. However, you may get a BeginContact callback
  165. /// the next step.
  166. virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
  167. {
  168. B2_NOT_USED(contact);
  169. B2_NOT_USED(oldManifold);
  170. }
  171. /// This lets you inspect a contact after the solver is finished. This is useful
  172. /// for inspecting impulses.
  173. /// Note: the contact manifold does not include time of impact impulses, which can be
  174. /// arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly
  175. /// in a separate data structure.
  176. /// Note: this is only called for contacts that are touching, solid, and awake.
  177. virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
  178. {
  179. B2_NOT_USED(contact);
  180. B2_NOT_USED(impulse);
  181. }
  182. };
  183. /// Callback class for AABB queries.
  184. /// See b2World::Query
  185. class b2QueryCallback
  186. {
  187. public:
  188. virtual ~b2QueryCallback() {}
  189. /// Called for each fixture found in the query AABB.
  190. /// @return false to terminate the query.
  191. virtual bool ReportFixture(b2Fixture* fixture) = 0;
  192. /// Called for each particle found in the query AABB.
  193. /// @return false to terminate the query.
  194. virtual bool ReportParticle(const b2ParticleSystem* particleSystem,
  195. int32 index)
  196. {
  197. B2_NOT_USED(particleSystem);
  198. B2_NOT_USED(index);
  199. return false;
  200. }
  201. /// Cull an entire particle system from b2World::QueryAABB. Ignored for
  202. /// b2ParticleSystem::QueryAABB.
  203. /// @return true if you want to include particleSystem in the AABB query,
  204. /// or false to cull particleSystem from the AABB query.
  205. virtual bool ShouldQueryParticleSystem(
  206. const b2ParticleSystem* particleSystem)
  207. {
  208. B2_NOT_USED(particleSystem);
  209. return true;
  210. }
  211. };
  212. /// Callback class for ray casts.
  213. /// See b2World::RayCast
  214. class b2RayCastCallback
  215. {
  216. public:
  217. virtual ~b2RayCastCallback() {}
  218. /// Called for each fixture found in the query. You control how the ray cast
  219. /// proceeds by returning a float:
  220. /// return -1: ignore this fixture and continue
  221. /// return 0: terminate the ray cast
  222. /// return fraction: clip the ray to this point
  223. /// return 1: don't clip the ray and continue
  224. /// @param fixture the fixture hit by the ray
  225. /// @param point the point of initial intersection
  226. /// @param normal the normal vector at the point of intersection
  227. /// @return -1 to filter, 0 to terminate, fraction to clip the ray for
  228. /// closest hit, 1 to continue
  229. virtual float32 ReportFixture( b2Fixture* fixture, const b2Vec2& point,
  230. const b2Vec2& normal, float32 fraction) = 0;
  231. /// Called for each particle found in the query. You control how the ray
  232. /// cast proceeds by returning a float:
  233. /// return <=0: ignore the remaining particles in this particle system
  234. /// return fraction: ignore particles that are 'fraction' percent farther
  235. /// along the line from 'point1' to 'point2'. Note that 'point1' and
  236. /// 'point2' are parameters to b2World::RayCast.
  237. /// @param particleSystem the particle system containing the particle
  238. /// @param index the index of the particle in particleSystem
  239. /// @param point the point of intersection bt the ray and the particle
  240. /// @param normal the normal vector at the point of intersection
  241. /// @param fraction percent (0.0~1.0) from 'point0' to 'point1' along the
  242. /// ray. Note that 'point1' and 'point2' are parameters to
  243. /// b2World::RayCast.
  244. /// @return <=0 to ignore rest of particle system, fraction to ignore
  245. /// particles that are farther away.
  246. virtual float32 ReportParticle(const b2ParticleSystem* particleSystem,
  247. int32 index, const b2Vec2& point,
  248. const b2Vec2& normal, float32 fraction)
  249. {
  250. B2_NOT_USED(particleSystem);
  251. B2_NOT_USED(index);
  252. B2_NOT_USED(&point);
  253. B2_NOT_USED(&normal);
  254. B2_NOT_USED(fraction);
  255. return 0;
  256. }
  257. /// Cull an entire particle system from b2World::RayCast. Ignored in
  258. /// b2ParticleSystem::RayCast.
  259. /// @return true if you want to include particleSystem in the RayCast, or
  260. /// false to cull particleSystem from the RayCast.
  261. virtual bool ShouldQueryParticleSystem(
  262. const b2ParticleSystem* particleSystem)
  263. {
  264. B2_NOT_USED(particleSystem);
  265. return true;
  266. }
  267. };
  268. #endif