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