- Main Page
- Classes
- Files

- File List
- File Members

# Box2D/Box2D/Collision/b2Collision.h

Go to the documentation of this file.

    00001 /*
    00002 * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
    00003 *
    00004 * This software is provided 'as-is', without any express or implied
    00005 * warranty.  In no event will the authors be held liable for any damages
    00006 * arising from the use of this software.
    00007 * Permission is granted to anyone to use this software for any purpose,
    00008 * including commercial applications, and to alter it and redistribute it
    00009 * freely, subject to the following restrictions:
    00010 * 1. The origin of this software must not be misrepresented; you must not
    00011 * claim that you wrote the original software. If you use this software
    00012 * in a product, an acknowledgment in the product documentation would be
    00013 * appreciated but is not required.
    00014 * 2. Altered source versions must be plainly marked as such, and must not be
    00015 * misrepresented as being the original software.
    00016 * 3. This notice may not be removed or altered from any source distribution.
    00017 */
    00018
    00019 #ifndef B2_COLLISION_H
    00020 #define B2_COLLISION_H
    00021
    00022 #include <Box2D/Common/b2Math.h>
    00023 #include <climits>
    00024
    00028
    00029 class b2Shape;
    00030 class b2CircleShape;
    00031 class b2PolygonShape;
    00032
    00033 const uint8 b2_nullFeature = UCHAR_MAX;
    00034
    00036 union b2ContactID
    00037 {
    00039         struct Features
    00040         {
    00041                 uint8 referenceEdge;
    00042                 uint8 incidentEdge;
    00043                 uint8 incidentVertex;
    00044                 uint8 flip;
    00045         } features;
    00046         uint32 key;
    00047 };
    00048
    00059 struct b2ManifoldPoint
    00060 {
    00061         b2Vec2 localPoint;
    00062         float32 normalImpulse;
    00063         float32 tangentImpulse;
    00064         b2ContactID id;
    00065 };
    00066
    00083 struct b2Manifold
    00084 {
    00085         enum Type
    00086         {
    00087                 e_circles,
    00088                 e_faceA,
    00089                 e_faceB
    00090         };
    00091
    00092         b2ManifoldPoint points[b2_maxManifoldPoints];
    00093         b2Vec2 localNormal;
    00094         b2Vec2 localPoint;
    00095         Type type;
    00096         int32 pointCount;
    00097 };
    00098
    00100 struct b2WorldManifold
    00101 {
    00106         void Initialize(const b2Manifold* manifold,
    00107                                         const b2Transform& xfA, float32 radiusA,
    00108                                         const b2Transform& xfB, float32 radiusB);
    00109
    00110         b2Vec2 normal;
    00111         b2Vec2 points[b2_maxManifoldPoints];
    00112 };
    00113
    00115 enum b2PointState
    00116 {
    00117         b2_nullState,
    00118         b2_addState,
    00119         b2_persistState,
    00120         b2_removeState
    00121 };
    00122
    00125 void b2GetPointStates(b2PointState state1[b2_maxManifoldPoints], b2PointState state2[b2_maxManifoldPoints],
    00126                                           const b2Manifold* manifold1, const b2Manifold* manifold2);
    00127
    00129 struct b2ClipVertex
    00130 {
    00131         b2Vec2 v;
    00132         b2ContactID id;
    00133 };
    00134
    00136 struct b2RayCastInput
    00137 {
    00138         b2Vec2 p1, p2;
    00139         float32 maxFraction;
    00140 };
    00141
    00144 struct b2RayCastOutput
    00145 {
    00146         b2Vec2 normal;
    00147         float32 fraction;
    00148 };
    00149
    00151 struct b2AABB
    00152 {
    00154         bool IsValid() const;
    00155
    00157         b2Vec2 GetCenter() const
    00158         {
    00159                 return 0.5f * (lowerBound + upperBound);
    00160         }
    00161
    00163         b2Vec2 GetExtents() const
    00164         {
    00165                 return 0.5f * (upperBound - lowerBound);
    00166         }
    00167
    00169         void Combine(const b2AABB& aabb1, const b2AABB& aabb2)
    00170         {
    00171                 lowerBound = b2Min(aabb1.lowerBound, aabb2.lowerBound);
    00172                 upperBound = b2Max(aabb1.upperBound, aabb2.upperBound);
    00173         }
    00174
    00176         bool Contains(const b2AABB& aabb) const
    00177         {
    00178                 bool result = true;
    00179                 result = result && lowerBound.x <= aabb.lowerBound.x;
    00180                 result = result && lowerBound.y <= aabb.lowerBound.y;
    00181                 result = result && aabb.upperBound.x <= upperBound.x;
    00182                 result = result && aabb.upperBound.y <= upperBound.y;
    00183                 return result;
    00184         }
    00185
    00186         bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input) const;
    00187
    00188         b2Vec2 lowerBound;
    00189         b2Vec2 upperBound;
    00190 };
    00191
    00193 void b2CollideCircles(b2Manifold* manifold,
    00194                                           const b2CircleShape* circle1, const b2Transform& xf1,
    00195                                           const b2CircleShape* circle2, const b2Transform& xf2);
    00196
    00198 void b2CollidePolygonAndCircle(b2Manifold* manifold,
    00199                                                            const b2PolygonShape* polygon, const b2Transform& xf1,
    00200                                                            const b2CircleShape* circle, const b2Transform& xf2);
    00201
    00203 void b2CollidePolygons(b2Manifold* manifold,
    00204                                            const b2PolygonShape* polygon1, const b2Transform& xf1,
    00205                                            const b2PolygonShape* polygon2, const b2Transform& xf2);
    00206
    00208 int32 b2ClipSegmentToLine(b2ClipVertex vOut[2], const b2ClipVertex vIn[2],
    00209                                                         const b2Vec2& normal, float32 offset);
    00210
    00212 bool b2TestOverlap(const b2Shape* shapeA, const b2Shape* shapeB,
    00213                                    const b2Transform& xfA, const b2Transform& xfB);
    00214
    00215 // ---------------- Inline Functions ------------------------------------------
    00216
    00217 inline bool b2AABB::IsValid() const
    00218 {
    00219         b2Vec2 d = upperBound - lowerBound;
    00220         bool valid = d.x >= 0.0f && d.y >= 0.0f;
    00221         valid = valid && lowerBound.IsValid() && upperBound.IsValid();
    00222         return valid;
    00223 }
    00224
    00225 inline bool b2TestOverlap(const b2AABB& a, const b2AABB& b)
    00226 {
    00227         b2Vec2 d1, d2;
    00228         d1 = b.lowerBound - a.upperBound;
    00229         d2 = a.lowerBound - b.upperBound;
    00230
    00231         if (d1.x > 0.0f || d1.y > 0.0f)
    00232                 return false;
    00233
    00234         if (d2.x > 0.0f || d2.y > 0.0f)
    00235                 return false;
    00236
    00237         return true;
    00238 }
    00239
    00240 #endif