123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- #ifndef B2_COLLISION_H
- #define B2_COLLISION_H
- #include <Box2D/Common/b2Math.h>
- #include <limits.h>
- class b2Shape;
- class b2CircleShape;
- class b2EdgeShape;
- class b2PolygonShape;
- const uint8 b2_nullFeature = UCHAR_MAX;
- struct b2ContactFeature
- {
- enum Type
- {
- e_vertex = 0,
- e_face = 1
- };
- uint8 indexA;
- uint8 indexB;
- uint8 typeA;
- uint8 typeB;
- };
- union b2ContactID
- {
- b2ContactFeature cf;
- uint32 key;
- };
- struct b2ManifoldPoint
- {
- b2Vec2 localPoint;
- float32 normalImpulse;
- float32 tangentImpulse;
- b2ContactID id;
- };
- struct b2Manifold
- {
- enum Type
- {
- e_circles,
- e_faceA,
- e_faceB
- };
- b2ManifoldPoint points[b2_maxManifoldPoints];
- b2Vec2 localNormal;
- b2Vec2 localPoint;
- Type type;
- int32 pointCount;
- };
- struct b2WorldManifold
- {
-
-
-
-
- void Initialize(const b2Manifold* manifold,
- const b2Transform& xfA, float32 radiusA,
- const b2Transform& xfB, float32 radiusB);
- b2Vec2 normal;
- b2Vec2 points[b2_maxManifoldPoints];
- float32 separations[b2_maxManifoldPoints];
- };
- enum b2PointState
- {
- b2_nullState,
- b2_addState,
- b2_persistState,
- b2_removeState
- };
- void b2GetPointStates(b2PointState state1[b2_maxManifoldPoints], b2PointState state2[b2_maxManifoldPoints],
- const b2Manifold* manifold1, const b2Manifold* manifold2);
- struct b2ClipVertex
- {
- b2Vec2 v;
- b2ContactID id;
- };
- struct b2RayCastInput
- {
- b2Vec2 p1, p2;
- float32 maxFraction;
- };
- struct b2RayCastOutput
- {
- b2Vec2 normal;
- float32 fraction;
- };
- struct b2AABB
- {
-
- bool IsValid() const;
-
- b2Vec2 GetCenter() const
- {
- return 0.5f * (lowerBound + upperBound);
- }
-
- b2Vec2 GetExtents() const
- {
- return 0.5f * (upperBound - lowerBound);
- }
-
- float32 GetPerimeter() const
- {
- float32 wx = upperBound.x - lowerBound.x;
- float32 wy = upperBound.y - lowerBound.y;
- return 2.0f * (wx + wy);
- }
-
- void Combine(const b2AABB& aabb)
- {
- lowerBound = b2Min(lowerBound, aabb.lowerBound);
- upperBound = b2Max(upperBound, aabb.upperBound);
- }
-
- void Combine(const b2AABB& aabb1, const b2AABB& aabb2)
- {
- lowerBound = b2Min(aabb1.lowerBound, aabb2.lowerBound);
- upperBound = b2Max(aabb1.upperBound, aabb2.upperBound);
- }
-
- bool Contains(const b2AABB& aabb) const
- {
- bool result = true;
- result = result && lowerBound.x <= aabb.lowerBound.x;
- result = result && lowerBound.y <= aabb.lowerBound.y;
- result = result && aabb.upperBound.x <= upperBound.x;
- result = result && aabb.upperBound.y <= upperBound.y;
- return result;
- }
- bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input) const;
- b2Vec2 lowerBound;
- b2Vec2 upperBound;
- };
- void b2CollideCircles(b2Manifold* manifold,
- const b2CircleShape* circleA, const b2Transform& xfA,
- const b2CircleShape* circleB, const b2Transform& xfB);
- void b2CollidePolygonAndCircle(b2Manifold* manifold,
- const b2PolygonShape* polygonA, const b2Transform& xfA,
- const b2CircleShape* circleB, const b2Transform& xfB);
- void b2CollidePolygons(b2Manifold* manifold,
- const b2PolygonShape* polygonA, const b2Transform& xfA,
- const b2PolygonShape* polygonB, const b2Transform& xfB);
- void b2CollideEdgeAndCircle(b2Manifold* manifold,
- const b2EdgeShape* polygonA, const b2Transform& xfA,
- const b2CircleShape* circleB, const b2Transform& xfB);
- void b2CollideEdgeAndPolygon(b2Manifold* manifold,
- const b2EdgeShape* edgeA, const b2Transform& xfA,
- const b2PolygonShape* circleB, const b2Transform& xfB);
- int32 b2ClipSegmentToLine(b2ClipVertex vOut[2], const b2ClipVertex vIn[2],
- const b2Vec2& normal, float32 offset, int32 vertexIndexA);
- bool b2TestOverlap( const b2Shape* shapeA, int32 indexA,
- const b2Shape* shapeB, int32 indexB,
- const b2Transform& xfA, const b2Transform& xfB);
- inline bool b2AABB::IsValid() const
- {
- b2Vec2 d = upperBound - lowerBound;
- bool valid = d.x >= 0.0f && d.y >= 0.0f;
- valid = valid && lowerBound.IsValid() && upperBound.IsValid();
- return valid;
- }
- inline bool b2TestOverlap(const b2AABB& a, const b2AABB& b)
- {
- b2Vec2 d1, d2;
- d1 = b.lowerBound - a.upperBound;
- d2 = a.lowerBound - b.upperBound;
- if (d1.x > 0.0f || d1.y > 0.0f)
- return false;
- if (d2.x > 0.0f || d2.y > 0.0f)
- return false;
- return true;
- }
- #endif
|