WorldCallbacks.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Box2D.XNA port of Box2D:
  3. * Copyright (c) 2009 Brandon Furtwangler, Nathan Furtwangler
  4. *
  5. * Original source Box2D:
  6. * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
  7. *
  8. * This software is provided 'as-is', without any express or implied
  9. * warranty. In no event will the authors be held liable for any damages
  10. * arising from the use of this software.
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. * 1. The origin of this software must not be misrepresented; you must not
  15. * claim that you wrote the original software. If you use this software
  16. * in a product, an acknowledgment in the product documentation would be
  17. * appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must not be
  19. * misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source distribution.
  21. */
  22. using System;
  23. using Microsoft.Xna.Framework;
  24. using Microsoft.Xna.Framework.Graphics;
  25. namespace Box2D.XNA
  26. {
  27. /// Called for each fixture found in the query. You control how the ray cast
  28. /// proceeds by returning a float:
  29. /// return -1: ignore this fixture and continue
  30. /// return 0: terminate the ray cast
  31. /// return fraction: clip the ray to this point
  32. /// return 1: don't clip the ray and continue
  33. /// @param fixture the fixture hit by the ray
  34. /// @param point the point of initial intersection
  35. /// @param normal the normal vector at the point of intersection
  36. /// @return -1 to filter, 0 to terminate, fraction to clip the ray for
  37. /// closest hit, 1 to continue
  38. public delegate float RayCastCallback(Fixture fixture, Vector2 point, Vector2 normal, float fraction);
  39. public interface IDestructionListener
  40. {
  41. void SayGoodbye(Joint joint);
  42. void SayGoodbye(Fixture fixture);
  43. }
  44. public interface IContactFilter
  45. {
  46. bool ShouldCollide(Fixture fixtureA, Fixture fixtureB);
  47. }
  48. public class DefaultContactFilter : IContactFilter
  49. {
  50. public bool ShouldCollide(Fixture fixtureA, Fixture fixtureB)
  51. {
  52. Filter filterA;
  53. fixtureA.GetFilterData(out filterA);
  54. Filter filterB;
  55. fixtureB.GetFilterData(out filterB);
  56. if (filterA.groupIndex == filterB.groupIndex && filterA.groupIndex != 0)
  57. {
  58. return filterA.groupIndex > 0;
  59. }
  60. bool collide = (filterA.maskBits & filterB.categoryBits) != 0 && (filterA.categoryBits & filterB.maskBits) != 0;
  61. return collide;
  62. }
  63. }
  64. public struct ContactImpulse
  65. {
  66. public FixedArray2<float> normalImpulses;
  67. public FixedArray2<float> tangentImpulses;
  68. }
  69. public interface IContactListener
  70. {
  71. void BeginContact(Contact contact);
  72. void EndContact(Contact contact);
  73. void PreSolve(Contact contact, ref Manifold oldManifold);
  74. void PostSolve(Contact contact, ref ContactImpulse impulse);
  75. }
  76. public class DefaultContactListener : IContactListener
  77. {
  78. public void BeginContact(Contact contact) { }
  79. public void EndContact(Contact contact) { }
  80. public void PreSolve(Contact contact, ref Manifold oldManifold) { }
  81. public void PostSolve(Contact contact, ref ContactImpulse impulse) { }
  82. }
  83. [Flags]
  84. public enum DebugDrawFlags
  85. {
  86. Shape = (1 << 0), ///< draw shapes
  87. Joint = (1 << 1), ///< draw joint connections
  88. AABB = (1 << 2), ///< draw axis aligned bounding boxes
  89. Pair = (1 << 3), ///< draw broad-phase pairs
  90. CenterOfMass = (1 << 4), ///< draw center of mass frame
  91. };
  92. /// Implement and register this class with a World to provide debug drawing of physics
  93. /// entities in your game.
  94. public abstract class DebugDraw
  95. {
  96. public DebugDrawFlags Flags { get; set; }
  97. /// Append flags to the current flags.
  98. public void AppendFlags(DebugDrawFlags flags)
  99. {
  100. Flags |= flags;
  101. }
  102. /// Clear flags from the current flags.
  103. public void ClearFlags(DebugDrawFlags flags)
  104. {
  105. Flags &= ~flags;
  106. }
  107. /// Draw a closed polygon provided in CCW order.
  108. public abstract void DrawPolygon(ref FixedArray8<Vector2> vertices, int count, Color color);
  109. /// Draw a solid closed polygon provided in CCW order.
  110. public abstract void DrawSolidPolygon(ref FixedArray8<Vector2> vertices, int count, Color color);
  111. /// Draw a circle.
  112. public abstract void DrawCircle(Vector2 center, float radius, Color color);
  113. /// Draw a solid circle.
  114. public abstract void DrawSolidCircle(Vector2 center, float radius, Vector2 axis, Color color);
  115. /// Draw a line segment.
  116. public abstract void DrawSegment(Vector2 p1, Vector2 p2, Color color);
  117. /// Draw a transform. Choose your own length scale.
  118. /// @param xf a transform.
  119. public abstract void DrawTransform(ref Transform xf);
  120. }
  121. }