2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
19 #ifndef B2_POLYGON_SHAPE_H
20 #define B2_POLYGON_SHAPE_H
24 /// Convex polygon. The vertices must be in CCW order for a right-handed
25 /// coordinate system with the z-axis coming out of the screen.
26 struct b2PolygonDef : public b2ShapeDef
30 type = e_polygonShape;
34 /// Build vertices to represent an axis-aligned box.
35 /// @param hx the half-width.
36 /// @param hy the half-height.
37 void SetAsBox(float32 hx, float32 hy);
39 /// Build vertices to represent an oriented box.
40 /// @param hx the half-width.
41 /// @param hy the half-height.
42 /// @param center the center of the box in local coordinates.
43 /// @param angle the rotation of the box in local coordinates.
44 void SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle);
46 /// The polygon vertices in local coordinates.
47 b2Vec2 vertices[b2_maxPolygonVertices];
49 /// The number of polygon vertices.
55 class b2PolygonShape : public b2Shape
58 /// @see b2Shape::TestPoint
59 bool TestPoint(const b2XForm& transform, const b2Vec2& p) const;
61 /// @see b2Shape::TestSegment
62 bool TestSegment( const b2XForm& transform,
65 const b2Segment& segment,
66 float32 maxLambda) const;
68 /// @see b2Shape::ComputeAABB
69 void ComputeAABB(b2AABB* aabb, const b2XForm& transform) const;
71 /// @see b2Shape::ComputeSweptAABB
72 void ComputeSweptAABB( b2AABB* aabb,
73 const b2XForm& transform1,
74 const b2XForm& transform2) const;
76 /// @see b2Shape::ComputeMass
77 void ComputeMass(b2MassData* massData) const;
79 /// Get the oriented bounding box relative to the parent body.
80 const b2OBB& GetOBB() const;
82 /// Get local centroid relative to the parent body.
83 const b2Vec2& GetCentroid() const;
85 /// Get the vertex count.
86 int32 GetVertexCount() const;
88 /// Get the vertices in local coordinates.
89 const b2Vec2* GetVertices() const;
91 /// Get the core vertices in local coordinates. These vertices
92 /// represent a smaller polygon that is used for time of impact
94 const b2Vec2* GetCoreVertices() const;
96 /// Get the edge normal vectors. There is one for each vertex.
97 const b2Vec2* GetNormals() const;
99 /// Get the first vertex and apply the supplied transform.
100 b2Vec2 GetFirstVertex(const b2XForm& xf) const;
102 /// Get the centroid and apply the supplied transform.
103 b2Vec2 Centroid(const b2XForm& xf) const;
105 /// Get the support point in the given world direction.
106 /// Use the supplied transform.
107 b2Vec2 Support(const b2XForm& xf, const b2Vec2& d) const;
111 friend class b2Shape;
113 b2PolygonShape(const b2ShapeDef* def);
115 void UpdateSweepRadius(const b2Vec2& center);
117 // Local position of the polygon centroid.
122 b2Vec2 m_vertices[b2_maxPolygonVertices];
123 b2Vec2 m_normals[b2_maxPolygonVertices];
124 b2Vec2 m_coreVertices[b2_maxPolygonVertices];
128 inline b2Vec2 b2PolygonShape::GetFirstVertex(const b2XForm& xf) const
130 return b2Mul(xf, m_coreVertices[0]);
133 inline const b2OBB& b2PolygonShape::GetOBB() const
138 inline const b2Vec2& b2PolygonShape::GetCentroid() const
143 inline int32 b2PolygonShape::GetVertexCount() const
145 return m_vertexCount;
148 inline const b2Vec2* b2PolygonShape::GetVertices() const
153 inline const b2Vec2* b2PolygonShape::GetCoreVertices() const
155 return m_coreVertices;
158 inline const b2Vec2* b2PolygonShape::GetNormals() const