Box2D: /build/box2d/src/box2d-adaedf1772ede7420bc9ee569c8cba7952db91a0/include/box2d/b2_collision.h Source File

| Box2D 2.4.0 A 2D physics engine for games

b2_collision.h
Go to the documentation of this file.
1 // MIT License
2
3 // Copyright (c) 2019 Erin Catto
4
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22
23 #ifndef B2_COLLISION_H
24 #define B2_COLLISION_H
25
26 #include <climits>
27
28 #include "b2_api.h"
29 #include "b2_math.h"
30
34
35 class b2Shape;
36 class b2CircleShape;
37 class b2EdgeShape;
38 class b2PolygonShape;
39
40 const uint8 b2_nullFeature = UCHAR_MAX;
41
44 struct B2_API b2ContactFeature
45 {
46 enum Type
47 {
48 e_vertex = 0,
49 e_face = 1
50 };
51
52 uint8 indexA;
53 uint8 indexB;
54 uint8 typeA;
55 uint8 typeB;
56 };
57
59 union B2_API b2ContactID
60 {
61 b2ContactFeature cf;
62 uint32 key;
63 };
64
75 struct B2_API b2ManifoldPoint
76 {
77 b2Vec2 localPoint;
78 float normalImpulse;
79 float tangentImpulse;
80 b2ContactID id;
81 };
82
99 struct B2_API b2Manifold
100 {
101 enum Type
102 {
103 e_circles,
104 e_faceA,
105 e_faceB
106 };
107
108 b2ManifoldPoint points[b2_maxManifoldPoints];
109 b2Vec2 localNormal;
110 b2Vec2 localPoint;
111 Type type;
112 int32 pointCount;
113 };
114
116 struct B2_API b2WorldManifold
117 {
122 void Initialize(const b2Manifold* manifold,
123 const b2Transform& xfA, float radiusA,
124 const b2Transform& xfB, float radiusB);
125
126 b2Vec2 normal;
127 b2Vec2 points[b2_maxManifoldPoints];
128 float separations[b2_maxManifoldPoints];
129 };
130
132 enum b2PointState
133 {
134 b2_nullState,
135 b2_addState,
136 b2_persistState,
137 b2_removeState
138 };
139
142 B2_API void b2GetPointStates(b2PointState state1[b2_maxManifoldPoints], b2PointState state2[b2_maxManifoldPoints],
143 const b2Manifold* manifold1, const b2Manifold* manifold2);
144
146 struct B2_API b2ClipVertex
147 {
148 b2Vec2 v;
149 b2ContactID id;
150 };
151
153 struct B2_API b2RayCastInput
154 {
155 b2Vec2 p1, p2;
156 float maxFraction;
157 };
158
161 struct B2_API b2RayCastOutput
162 {
163 b2Vec2 normal;
164 float fraction;
165 };
166
168 struct B2_API b2AABB
169 {
171 bool IsValid() const;
172
174 b2Vec2 GetCenter() const
175 {
176 return 0.5f * (lowerBound + upperBound);
177 }
178
180 b2Vec2 GetExtents() const
181 {
182 return 0.5f * (upperBound - lowerBound);
183 }
184
186 float GetPerimeter() const
187 {
188 float wx = upperBound.x - lowerBound.x;
189 float wy = upperBound.y - lowerBound.y;
190 return 2.0f * (wx + wy);
191 }
192
194 void Combine(const b2AABB& aabb)
195 {
196 lowerBound = b2Min(lowerBound, aabb.lowerBound);
197 upperBound = b2Max(upperBound, aabb.upperBound);
198 }
199
201 void Combine(const b2AABB& aabb1, const b2AABB& aabb2)
202 {
203 lowerBound = b2Min(aabb1.lowerBound, aabb2.lowerBound);
204 upperBound = b2Max(aabb1.upperBound, aabb2.upperBound);
205 }
206
208 bool Contains(const b2AABB& aabb) const
209 {
210 bool result = true;
211 result = result && lowerBound.x <= aabb.lowerBound.x;
212 result = result && lowerBound.y <= aabb.lowerBound.y;
213 result = result && aabb.upperBound.x <= upperBound.x;
214 result = result && aabb.upperBound.y <= upperBound.y;
215 return result;
216 }
217
218 bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input) const;
219
220 b2Vec2 lowerBound;
221 b2Vec2 upperBound;
222 };
223
225 B2_API void b2CollideCircles(b2Manifold* manifold,
226 const b2CircleShape* circleA, const b2Transform& xfA,
227 const b2CircleShape* circleB, const b2Transform& xfB);
228
230 B2_API void b2CollidePolygonAndCircle(b2Manifold* manifold,
231 const b2PolygonShape* polygonA, const b2Transform& xfA,
232 const b2CircleShape* circleB, const b2Transform& xfB);
233
235 B2_API void b2CollidePolygons(b2Manifold* manifold,
236 const b2PolygonShape* polygonA, const b2Transform& xfA,
237 const b2PolygonShape* polygonB, const b2Transform& xfB);
238
240 B2_API void b2CollideEdgeAndCircle(b2Manifold* manifold,
241 const b2EdgeShape* polygonA, const b2Transform& xfA,
242 const b2CircleShape* circleB, const b2Transform& xfB);
243
245 B2_API void b2CollideEdgeAndPolygon(b2Manifold* manifold,
246 const b2EdgeShape* edgeA, const b2Transform& xfA,
247 const b2PolygonShape* circleB, const b2Transform& xfB);
248
250 B2_API int32 b2ClipSegmentToLine(b2ClipVertex vOut[2], const b2ClipVertex vIn[2],
251 const b2Vec2& normal, float offset, int32 vertexIndexA);
252
254 B2_API bool b2TestOverlap( const b2Shape* shapeA, int32 indexA,
255 const b2Shape* shapeB, int32 indexB,
256 const b2Transform& xfA, const b2Transform& xfB);
257
258 // ---------------- Inline Functions ------------------------------------------
259
260 inline bool b2AABB::IsValid() const
261 {
262 b2Vec2 d = upperBound - lowerBound;
263 bool valid = d.x >= 0.0f && d.y >= 0.0f;
264 valid = valid && lowerBound.IsValid() && upperBound.IsValid();
265 return valid;
266 }
267
268 inline bool b2TestOverlap(const b2AABB& a, const b2AABB& b)
269 {
270 b2Vec2 d1, d2;
271 d1 = b.lowerBound - a.upperBound;
272 d2 = a.lowerBound - b.upperBound;
273
274 if (d1.x > 0.0f || d1.y > 0.0f)
275 return false;
276
277 if (d2.x > 0.0f || d2.y > 0.0f)
278 return false;
279
280 return true;
281 }
282
283 #endif
b2ContactID::key
uint32 key
Used to quickly compare contact ids.
Definition: b2_collision.h:62
b2Vec2
A 2D column vector.
Definition: b2_math.h:42
b2_addState
@ b2_addState
point was added in the update
Definition: b2_collision.h:135
b2AABB::GetExtents
b2Vec2 GetExtents() const
Get the extents of the AABB (half-widths).
Definition: b2_collision.h:180
b2_persistState
@ b2_persistState
point persisted across the update
Definition: b2_collision.h:136
b2EdgeShape
Definition: b2_edge_shape.h:33
b2ManifoldPoint::normalImpulse
float normalImpulse
the non-penetration impulse
Definition: b2_collision.h:78
b2AABB::lowerBound
b2Vec2 lowerBound
the lower vertex
Definition: b2_collision.h:220
b2RayCastInput
Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
Definition: b2_collision.h:154
b2ManifoldPoint::tangentImpulse
float tangentImpulse
the friction impulse
Definition: b2_collision.h:79
b2Transform
Definition: b2_math.h:339
b2AABB::GetCenter
b2Vec2 GetCenter() const
Get the center of the AABB.
Definition: b2_collision.h:174
b2Manifold
Definition: b2_collision.h:100
b2Manifold::pointCount
int32 pointCount
the number of manifold points
Definition: b2_collision.h:112
b2AABB::Contains
bool Contains(const b2AABB &aabb) const
Does this aabb contain the provided AABB.
Definition: b2_collision.h:208
b2PointState
b2PointState
This is used for determining the state of contact points.
Definition: b2_collision.h:133
b2ManifoldPoint
Definition: b2_collision.h:76
b2WorldManifold
This is used to compute the current state of a contact manifold.
Definition: b2_collision.h:117
b2ClipVertex
Used for computing contact manifolds.
Definition: b2_collision.h:147
b2ClipSegmentToLine
B2_API int32 b2ClipSegmentToLine(b2ClipVertex vOut[2], const b2ClipVertex vIn[2], const b2Vec2 &normal, float offset, int32 vertexIndexA)
Clipping for contact manifolds.
b2AABB
An axis aligned bounding box.
Definition: b2_collision.h:169
b2Manifold::localNormal
b2Vec2 localNormal
not use for Type::e_points
Definition: b2_collision.h:109
b2ContactID
Contact ids to facilitate warm starting.
Definition: b2_collision.h:60
b2ContactFeature::indexB
uint8 indexB
Feature index on shapeB.
Definition: b2_collision.h:53
b2CollideEdgeAndCircle
B2_API void b2CollideEdgeAndCircle(b2Manifold *manifold, const b2EdgeShape *polygonA, const b2Transform &xfA, const b2CircleShape *circleB, const b2Transform &xfB)
Compute the collision manifold between an edge and a circle.
b2ManifoldPoint::id
b2ContactID id
uniquely identifies a contact point between two shapes
Definition: b2_collision.h:80
b2ManifoldPoint::localPoint
b2Vec2 localPoint
usage depends on manifold type
Definition: b2_collision.h:77
b2TestOverlap
B2_API bool b2TestOverlap(const b2Shape *shapeA, int32 indexA, const b2Shape *shapeB, int32 indexB, const b2Transform &xfA, const b2Transform &xfB)
Determine if two generic shapes overlap.
b2GetPointStates
B2_API void b2GetPointStates(b2PointState state1[b2_maxManifoldPoints], b2PointState state2[b2_maxManifoldPoints], const b2Manifold *manifold1, const b2Manifold *manifold2)
b2CollideEdgeAndPolygon
B2_API void b2CollideEdgeAndPolygon(b2Manifold *manifold, const b2EdgeShape *edgeA, const b2Transform &xfA, const b2PolygonShape *circleB, const b2Transform &xfB)
Compute the collision manifold between an edge and a polygon.
b2_maxManifoldPoints
#define b2_maxManifoldPoints
Definition: b2_settings.h:58
b2AABB::upperBound
b2Vec2 upperBound
the upper vertex
Definition: b2_collision.h:221
b2PolygonShape
Definition: b2_polygon_shape.h:33
b2AABB::Combine
void Combine(const b2AABB &aabb)
Combine an AABB into this one.
Definition: b2_collision.h:194
b2CollidePolygons
B2_API void b2CollidePolygons(b2Manifold *manifold, const b2PolygonShape *polygonA, const b2Transform &xfA, const b2PolygonShape *polygonB, const b2Transform &xfB)
Compute the collision manifold between two polygons.
b2ContactFeature::typeA
uint8 typeA
The feature type on shapeA.
Definition: b2_collision.h:54
b2WorldManifold::normal
b2Vec2 normal
world vector pointing from A to B
Definition: b2_collision.h:126
b2CollidePolygonAndCircle
B2_API void b2CollidePolygonAndCircle(b2Manifold *manifold, const b2PolygonShape *polygonA, const b2Transform &xfA, const b2CircleShape *circleB, const b2Transform &xfB)
Compute the collision manifold between a polygon and a circle.
b2CollideCircles
B2_API void b2CollideCircles(b2Manifold *manifold, const b2CircleShape *circleA, const b2Transform &xfA, const b2CircleShape *circleB, const b2Transform &xfB)
Compute the collision manifold between two circles.
b2ContactFeature::indexA
uint8 indexA
Feature index on shapeA.
Definition: b2_collision.h:52
b2AABB::IsValid
bool IsValid() const
Verify that the bounds are sorted.
Definition: b2_collision.h:260
b2WorldManifold::Initialize
void Initialize(const b2Manifold *manifold, const b2Transform &xfA, float radiusA, const b2Transform &xfB, float radiusB)
b2Manifold::localPoint
b2Vec2 localPoint
usage depends on manifold type
Definition: b2_collision.h:110
b2Shape
Definition: b2_shape.h:49
b2AABB::GetPerimeter
float GetPerimeter() const
Get the perimeter length.
Definition: b2_collision.h:186
b2AABB::Combine
void Combine(const b2AABB &aabb1, const b2AABB &aabb2)
Combine two AABBs into this one.
Definition: b2_collision.h:201
b2RayCastOutput
Definition: b2_collision.h:162
b2_nullState
@ b2_nullState
point does not exist
Definition: b2_collision.h:134
b2_removeState
@ b2_removeState
point was removed in the update
Definition: b2_collision.h:137
b2ContactFeature::typeB
uint8 typeB
The feature type on shapeB.
Definition: b2_collision.h:55
b2CircleShape
A solid circle shape.
Definition: b2_circle_shape.h:31
b2ContactFeature
Definition: b2_collision.h:45
b2Vec2::IsValid
bool IsValid() const
Does this vector contain finite coordinates?
Definition: b2_math.h:117