Box2D: Box2D/Box2D/Collision/Shapes/b2PolygonShape.h Source File

- Main Page
- Classes
- Files

- File List
- File Members

# Box2D/Box2D/Collision/Shapes/b2PolygonShape.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_POLYGON_SHAPE_H
    00020 #define B2_POLYGON_SHAPE_H
    00021
    00022 #include <Box2D/Collision/Shapes/b2Shape.h>
    00023
    00026 class b2PolygonShape : public b2Shape
    00027 {
    00028 public:
    00029         b2PolygonShape();
    00030
    00032         b2Shape* Clone(b2BlockAllocator* allocator) const;
    00033
    00036         void Set(const b2Vec2* vertices, int32 vertexCount);
    00037
    00041         void SetAsBox(float32 hx, float32 hy);
    00042
    00048         void SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle);
    00049
    00051         void SetAsEdge(const b2Vec2& v1, const b2Vec2& v2);
    00052
    00054         bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
    00055
    00057         bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input, const b2Transform& transform) const;
    00058
    00060         void ComputeAABB(b2AABB* aabb, const b2Transform& transform) const;
    00061
    00063         void ComputeMass(b2MassData* massData, float32 density) const;
    00064
    00066         int32 GetSupport(const b2Vec2& d) const;
    00067
    00069         const b2Vec2& GetSupportVertex(const b2Vec2& d) const;
    00070
    00072         int32 GetVertexCount() const { return m_vertexCount; }
    00073
    00075         const b2Vec2& GetVertex(int32 index) const;
    00076
    00077         b2Vec2 m_centroid;
    00078         b2Vec2 m_vertices[b2_maxPolygonVertices];
    00079         b2Vec2 m_normals[b2_maxPolygonVertices];
    00080         int32 m_vertexCount;
    00081 };
    00082
    00083 inline b2PolygonShape::b2PolygonShape()
    00084 {
    00085         m_type = e_polygon;
    00086         m_radius = b2_polygonRadius;
    00087         m_vertexCount = 0;
    00088         m_centroid.SetZero();
    00089 }
    00090
    00091 inline int32 b2PolygonShape::GetSupport(const b2Vec2& d) const
    00092 {
    00093         int32 bestIndex = 0;
    00094         float32 bestValue = b2Dot(m_vertices[0], d);
    00095         for (int32 i = 1; i < m_vertexCount; ++i)
    00096         {
    00097                 float32 value = b2Dot(m_vertices[i], d);
    00098                 if (value > bestValue)
    00099                 {
    00100                         bestIndex = i;
    00101                         bestValue = value;
    00102                 }
    00103         }
    00104
    00105         return bestIndex;
    00106 }
    00107
    00108 inline const b2Vec2& b2PolygonShape::GetSupportVertex(const b2Vec2& d) const
    00109 {
    00110         int32 bestIndex = 0;
    00111         float32 bestValue = b2Dot(m_vertices[0], d);
    00112         for (int32 i = 1; i < m_vertexCount; ++i)
    00113         {
    00114                 float32 value = b2Dot(m_vertices[i], d);
    00115                 if (value > bestValue)
    00116                 {
    00117                         bestIndex = i;
    00118                         bestValue = value;
    00119                 }
    00120         }
    00121
    00122         return m_vertices[bestIndex];
    00123 }
    00124
    00125 inline const b2Vec2& b2PolygonShape::GetVertex(int32 index) const
    00126 {
    00127         b2Assert(0 <= index && index < m_vertexCount);
    00128         return m_vertices[index];
    00129 }
    00130
    00131 #endif