00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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