b2Collision.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_COLLISION_H
  19. #define B2_COLLISION_H
  20. #include <Box2D/Common/b2Math.h>
  21. #include <limits.h>
  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 b2EdgeShape;
  28. class b2PolygonShape;
  29. const uint8 b2_nullFeature = UCHAR_MAX;
  30. /// The features that intersect to form the contact point
  31. /// This must be 4 bytes or less.
  32. struct b2ContactFeature
  33. {
  34. enum Type
  35. {
  36. e_vertex = 0,
  37. e_face = 1
  38. };
  39. uint8 indexA; ///< Feature index on shapeA
  40. uint8 indexB; ///< Feature index on shapeB
  41. uint8 typeA; ///< The feature type on shapeA
  42. uint8 typeB; ///< The feature type on shapeB
  43. };
  44. /// Contact ids to facilitate warm starting.
  45. union b2ContactID
  46. {
  47. b2ContactFeature cf;
  48. uint32 key; ///< Used to quickly compare contact ids.
  49. };
  50. /// A manifold point is a contact point belonging to a contact
  51. /// manifold. It holds details related to the geometry and dynamics
  52. /// of the contact points.
  53. /// The local point usage depends on the manifold type:
  54. /// -e_circles: the local center of circleB
  55. /// -e_faceA: the local center of cirlceB or the clip point of polygonB
  56. /// -e_faceB: the clip point of polygonA
  57. /// This structure is stored across time steps, so we keep it small.
  58. /// Note: the impulses are used for internal caching and may not
  59. /// provide reliable contact forces, especially for high speed collisions.
  60. struct b2ManifoldPoint
  61. {
  62. b2Vec2 localPoint; ///< usage depends on manifold type
  63. float32 normalImpulse; ///< the non-penetration impulse
  64. float32 tangentImpulse; ///< the friction impulse
  65. b2ContactID id; ///< uniquely identifies a contact point between two shapes
  66. };
  67. /// A manifold for two touching convex shapes.
  68. /// Box2D supports multiple types of contact:
  69. /// - clip point versus plane with radius
  70. /// - point versus point with radius (circles)
  71. /// The local point usage depends on the manifold type:
  72. /// -e_circles: the local center of circleA
  73. /// -e_faceA: the center of faceA
  74. /// -e_faceB: the center of faceB
  75. /// Similarly the local normal usage:
  76. /// -e_circles: not used
  77. /// -e_faceA: the normal on polygonA
  78. /// -e_faceB: the normal on polygonB
  79. /// We store contacts in this way so that position correction can
  80. /// account for movement, which is critical for continuous physics.
  81. /// All contact scenarios must be expressed in one of these types.
  82. /// This structure is stored across time steps, so we keep it small.
  83. struct b2Manifold
  84. {
  85. enum Type
  86. {
  87. e_circles,
  88. e_faceA,
  89. e_faceB
  90. };
  91. b2ManifoldPoint points[b2_maxManifoldPoints]; ///< the points of contact
  92. b2Vec2 localNormal; ///< not use for Type::e_points
  93. b2Vec2 localPoint; ///< usage depends on manifold type
  94. Type type;
  95. int32 pointCount; ///< the number of manifold points
  96. };
  97. /// This is used to compute the current state of a contact manifold.
  98. struct b2WorldManifold
  99. {
  100. /// Evaluate the manifold with supplied transforms. This assumes
  101. /// modest motion from the original state. This does not change the
  102. /// point count, impulses, etc. The radii must come from the shapes
  103. /// that generated the manifold.
  104. void Initialize(const b2Manifold* manifold,
  105. const b2Transform& xfA, float32 radiusA,
  106. const b2Transform& xfB, float32 radiusB);
  107. b2Vec2 normal; ///< world vector pointing from A to B
  108. b2Vec2 points[b2_maxManifoldPoints]; ///< world contact point (point of intersection)
  109. float32 separations[b2_maxManifoldPoints]; ///< a negative value indicates overlap, in meters
  110. };
  111. /// This is used for determining the state of contact points.
  112. enum b2PointState
  113. {
  114. b2_nullState, ///< point does not exist
  115. b2_addState, ///< point was added in the update
  116. b2_persistState, ///< point persisted across the update
  117. b2_removeState ///< point was removed in the update
  118. };
  119. /// Compute the point states given two manifolds. The states pertain to the transition from manifold1
  120. /// to manifold2. So state1 is either persist or remove while state2 is either add or persist.
  121. void b2GetPointStates(b2PointState state1[b2_maxManifoldPoints], b2PointState state2[b2_maxManifoldPoints],
  122. const b2Manifold* manifold1, const b2Manifold* manifold2);
  123. /// Used for computing contact manifolds.
  124. struct b2ClipVertex
  125. {
  126. b2Vec2 v;
  127. b2ContactID id;
  128. };
  129. /// Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
  130. struct b2RayCastInput
  131. {
  132. b2Vec2 p1, p2;
  133. float32 maxFraction;
  134. };
  135. /// Ray-cast output data. The ray hits at p1 + fraction * (p2 - p1), where p1 and p2
  136. /// come from b2RayCastInput.
  137. struct b2RayCastOutput
  138. {
  139. b2Vec2 normal;
  140. float32 fraction;
  141. };
  142. /// An axis aligned bounding box.
  143. struct b2AABB
  144. {
  145. /// Verify that the bounds are sorted.
  146. bool IsValid() const;
  147. /// Get the center of the AABB.
  148. b2Vec2 GetCenter() const
  149. {
  150. return 0.5f * (lowerBound + upperBound);
  151. }
  152. /// Get the extents of the AABB (half-widths).
  153. b2Vec2 GetExtents() const
  154. {
  155. return 0.5f * (upperBound - lowerBound);
  156. }
  157. /// Get the perimeter length
  158. float32 GetPerimeter() const
  159. {
  160. float32 wx = upperBound.x - lowerBound.x;
  161. float32 wy = upperBound.y - lowerBound.y;
  162. return 2.0f * (wx + wy);
  163. }
  164. /// Combine an AABB into this one.
  165. void Combine(const b2AABB& aabb)
  166. {
  167. lowerBound = b2Min(lowerBound, aabb.lowerBound);
  168. upperBound = b2Max(upperBound, aabb.upperBound);
  169. }
  170. /// Combine two AABBs into this one.
  171. void Combine(const b2AABB& aabb1, const b2AABB& aabb2)
  172. {
  173. lowerBound = b2Min(aabb1.lowerBound, aabb2.lowerBound);
  174. upperBound = b2Max(aabb1.upperBound, aabb2.upperBound);
  175. }
  176. /// Does this aabb contain the provided AABB.
  177. bool Contains(const b2AABB& aabb) const
  178. {
  179. bool result = true;
  180. result = result && lowerBound.x <= aabb.lowerBound.x;
  181. result = result && lowerBound.y <= aabb.lowerBound.y;
  182. result = result && aabb.upperBound.x <= upperBound.x;
  183. result = result && aabb.upperBound.y <= upperBound.y;
  184. return result;
  185. }
  186. bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input) const;
  187. b2Vec2 lowerBound; ///< the lower vertex
  188. b2Vec2 upperBound; ///< the upper vertex
  189. };
  190. /// Compute the collision manifold between two circles.
  191. void b2CollideCircles(b2Manifold* manifold,
  192. const b2CircleShape* circleA, const b2Transform& xfA,
  193. const b2CircleShape* circleB, const b2Transform& xfB);
  194. /// Compute the collision manifold between a polygon and a circle.
  195. void b2CollidePolygonAndCircle(b2Manifold* manifold,
  196. const b2PolygonShape* polygonA, const b2Transform& xfA,
  197. const b2CircleShape* circleB, const b2Transform& xfB);
  198. /// Compute the collision manifold between two polygons.
  199. void b2CollidePolygons(b2Manifold* manifold,
  200. const b2PolygonShape* polygonA, const b2Transform& xfA,
  201. const b2PolygonShape* polygonB, const b2Transform& xfB);
  202. /// Compute the collision manifold between an edge and a circle.
  203. void b2CollideEdgeAndCircle(b2Manifold* manifold,
  204. const b2EdgeShape* polygonA, const b2Transform& xfA,
  205. const b2CircleShape* circleB, const b2Transform& xfB);
  206. /// Compute the collision manifold between an edge and a circle.
  207. void b2CollideEdgeAndPolygon(b2Manifold* manifold,
  208. const b2EdgeShape* edgeA, const b2Transform& xfA,
  209. const b2PolygonShape* circleB, const b2Transform& xfB);
  210. /// Clipping for contact manifolds.
  211. int32 b2ClipSegmentToLine(b2ClipVertex vOut[2], const b2ClipVertex vIn[2],
  212. const b2Vec2& normal, float32 offset, int32 vertexIndexA);
  213. /// Determine if two generic shapes overlap.
  214. bool b2TestOverlap( const b2Shape* shapeA, int32 indexA,
  215. const b2Shape* shapeB, int32 indexB,
  216. const b2Transform& xfA, const b2Transform& xfB);
  217. // ---------------- Inline Functions ------------------------------------------
  218. inline bool b2AABB::IsValid() const
  219. {
  220. b2Vec2 d = upperBound - lowerBound;
  221. bool valid = d.x >= 0.0f && d.y >= 0.0f;
  222. valid = valid && lowerBound.IsValid() && upperBound.IsValid();
  223. return valid;
  224. }
  225. inline bool b2TestOverlap(const b2AABB& a, const b2AABB& b)
  226. {
  227. b2Vec2 d1, d2;
  228. d1 = b.lowerBound - a.upperBound;
  229. d2 = a.lowerBound - b.upperBound;
  230. if (d1.x > 0.0f || d1.y > 0.0f)
  231. return false;
  232. if (d2.x > 0.0f || d2.y > 0.0f)
  233. return false;
  234. return true;
  235. }
  236. #endif