b2Collision.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  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_COLLISION_H
  19. #define B2_COLLISION_H
  20. #include "../Common/b2Math.h"
  21. #include <climits>
  22. /// @file
  23. /// Structures and functions used for computing contact points, distance
  24. /// queries, and TOI queries.
  25. class b2Shape;
  26. class b2CircleShape;
  27. class b2PolygonShape;
  28. class b2EdgeShape;
  29. const uint8 b2_nullFeature = UCHAR_MAX;
  30. /// Contact ids to facilitate warm starting.
  31. union b2ContactID
  32. {
  33. /// The features that intersect to form the contact point
  34. struct Features
  35. {
  36. uint8 referenceEdge; ///< The edge that defines the outward contact normal.
  37. uint8 incidentEdge; ///< The edge most anti-parallel to the reference edge.
  38. uint8 incidentVertex; ///< The vertex (0 or 1) on the incident edge that was clipped.
  39. uint8 flip; ///< A value of 1 indicates that the reference edge is on shape2.
  40. } features;
  41. uint32 key; ///< Used to quickly compare contact ids.
  42. };
  43. /// A manifold point is a contact point belonging to a contact
  44. /// manifold. It holds details related to the geometry and dynamics
  45. /// of the contact points.
  46. /// The point is stored in local coordinates because CCD
  47. /// requires sub-stepping in which the separation is stale.
  48. struct b2ManifoldPoint
  49. {
  50. b2Vec2 localPoint1; ///< local position of the contact point in body1
  51. b2Vec2 localPoint2; ///< local position of the contact point in body2
  52. float32 separation; ///< the separation of the shapes along the normal vector
  53. float32 normalImpulse; ///< the non-penetration impulse
  54. float32 tangentImpulse; ///< the friction impulse
  55. b2ContactID id; ///< uniquely identifies a contact point between two shapes
  56. };
  57. /// A manifold for two touching convex shapes.
  58. struct b2Manifold
  59. {
  60. b2ManifoldPoint points[b2_maxManifoldPoints]; ///< the points of contact
  61. b2Vec2 normal; ///< the shared unit normal vector
  62. int32 pointCount; ///< the number of manifold points
  63. };
  64. /// A line segment.
  65. struct b2Segment
  66. {
  67. /// Ray cast against this segment with another segment.
  68. bool TestSegment(float32* lambda, b2Vec2* normal, const b2Segment& segment, float32 maxLambda) const;
  69. b2Vec2 p1; ///< the starting point
  70. b2Vec2 p2; ///< the ending point
  71. };
  72. /// An axis aligned bounding box.
  73. struct b2AABB
  74. {
  75. /// Verify that the bounds are sorted.
  76. bool IsValid() const;
  77. b2Vec2 lowerBound; ///< the lower vertex
  78. b2Vec2 upperBound; ///< the upper vertex
  79. };
  80. /// An oriented bounding box.
  81. struct b2OBB
  82. {
  83. b2Mat22 R; ///< the rotation matrix
  84. b2Vec2 center; ///< the local centroid
  85. b2Vec2 extents; ///< the half-widths
  86. };
  87. /// Compute the collision manifold between two circles.
  88. void b2CollideCircles(b2Manifold* manifold,
  89. const b2CircleShape* circle1, const b2XForm& xf1,
  90. const b2CircleShape* circle2, const b2XForm& xf2);
  91. /// Compute the collision manifold between a polygon and a circle.
  92. void b2CollidePolygonAndCircle(b2Manifold* manifold,
  93. const b2PolygonShape* polygon, const b2XForm& xf1,
  94. const b2CircleShape* circle, const b2XForm& xf2);
  95. /// Compute the collision manifold between two circles.
  96. void b2CollidePolygons(b2Manifold* manifold,
  97. const b2PolygonShape* polygon1, const b2XForm& xf1,
  98. const b2PolygonShape* polygon2, const b2XForm& xf2);
  99. /// Compute the distance between two shapes and the closest points.
  100. /// @return the distance between the shapes or zero if they are overlapped/touching.
  101. float32 b2Distance(b2Vec2* x1, b2Vec2* x2,
  102. const b2Shape* shape1, const b2XForm& xf1,
  103. const b2Shape* shape2, const b2XForm& xf2);
  104. /// Compute the time when two shapes begin to touch or touch at a closer distance.
  105. /// @warning the sweeps must have the same time interval.
  106. /// @return the fraction between [0,1] in which the shapes first touch.
  107. /// fraction=0 means the shapes begin touching/overlapped, and fraction=1 means the shapes don't touch.
  108. float32 b2TimeOfImpact(const b2Shape* shape1, const b2Sweep& sweep1,
  109. const b2Shape* shape2, const b2Sweep& sweep2);
  110. // ---------------- Inline Functions ------------------------------------------
  111. inline bool b2AABB::IsValid() const
  112. {
  113. b2Vec2 d = upperBound - lowerBound;
  114. bool valid = d.x >= 0.0f && d.y >= 0.0f;
  115. valid = valid && lowerBound.IsValid() && upperBound.IsValid();
  116. return valid;
  117. }
  118. inline bool b2TestOverlap(const b2AABB& a, const b2AABB& b)
  119. {
  120. b2Vec2 d1, d2;
  121. d1 = b.lowerBound - a.upperBound;
  122. d2 = a.lowerBound - b.upperBound;
  123. if (d1.x > 0.0f || d1.y > 0.0f)
  124. return false;
  125. if (d2.x > 0.0f || d2.y > 0.0f)
  126. return false;
  127. return true;
  128. }
  129. #endif