first commit
[blok] / Box2D / Source / Collision / Shapes / b2PolygonShape.h
1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
3 *
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.
17 */
18
19 #ifndef B2_POLYGON_SHAPE_H
20 #define B2_POLYGON_SHAPE_H
21
22 #include "b2Shape.h"
23
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
27 {
28         b2PolygonDef()
29         {
30                 type = e_polygonShape;
31                 vertexCount = 0;
32         }
33
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);
38
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);
45
46         /// The polygon vertices in local coordinates.
47         b2Vec2 vertices[b2_maxPolygonVertices];
48
49         /// The number of polygon vertices.
50         int32 vertexCount;
51 };
52
53
54 /// A convex polygon.
55 class b2PolygonShape : public b2Shape
56 {
57 public:
58         /// @see b2Shape::TestPoint
59         bool TestPoint(const b2XForm& transform, const b2Vec2& p) const;
60
61         /// @see b2Shape::TestSegment
62         bool TestSegment(       const b2XForm& transform,
63                 float32* lambda,
64                 b2Vec2* normal,
65                 const b2Segment& segment,
66                 float32 maxLambda) const;
67
68         /// @see b2Shape::ComputeAABB
69         void ComputeAABB(b2AABB* aabb, const b2XForm& transform) const;
70
71         /// @see b2Shape::ComputeSweptAABB
72         void ComputeSweptAABB(  b2AABB* aabb,
73                 const b2XForm& transform1,
74                 const b2XForm& transform2) const;
75
76         /// @see b2Shape::ComputeMass
77         void ComputeMass(b2MassData* massData) const;
78
79         /// Get the oriented bounding box relative to the parent body.
80         const b2OBB& GetOBB() const;
81
82         /// Get local centroid relative to the parent body.
83         const b2Vec2& GetCentroid() const;
84
85         /// Get the vertex count.
86         int32 GetVertexCount() const;
87
88         /// Get the vertices in local coordinates.
89         const b2Vec2* GetVertices() const;
90
91         /// Get the core vertices in local coordinates. These vertices
92         /// represent a smaller polygon that is used for time of impact
93         /// computations.
94         const b2Vec2* GetCoreVertices() const;
95
96         /// Get the edge normal vectors. There is one for each vertex.
97         const b2Vec2* GetNormals() const;
98
99         /// Get the first vertex and apply the supplied transform.
100         b2Vec2 GetFirstVertex(const b2XForm& xf) const;
101
102         /// Get the centroid and apply the supplied transform.
103         b2Vec2 Centroid(const b2XForm& xf) const;
104
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;
108
109 private:
110
111         friend class b2Shape;
112
113         b2PolygonShape(const b2ShapeDef* def);
114
115         void UpdateSweepRadius(const b2Vec2& center);
116
117         // Local position of the polygon centroid.
118         b2Vec2 m_centroid;
119
120         b2OBB m_obb;
121
122         b2Vec2 m_vertices[b2_maxPolygonVertices];
123         b2Vec2 m_normals[b2_maxPolygonVertices];
124         b2Vec2 m_coreVertices[b2_maxPolygonVertices];
125         int32 m_vertexCount;
126 };
127
128 inline b2Vec2 b2PolygonShape::GetFirstVertex(const b2XForm& xf) const
129 {
130         return b2Mul(xf, m_coreVertices[0]);
131 }
132
133 inline const b2OBB& b2PolygonShape::GetOBB() const
134 {
135         return m_obb;
136 }
137
138 inline const b2Vec2& b2PolygonShape::GetCentroid() const
139 {
140         return m_centroid;
141 }
142
143 inline int32 b2PolygonShape::GetVertexCount() const
144 {
145         return m_vertexCount;
146 }
147
148 inline const b2Vec2* b2PolygonShape::GetVertices() const
149 {
150         return m_vertices;
151 }
152
153 inline const b2Vec2* b2PolygonShape::GetCoreVertices() const
154 {
155         return m_coreVertices;
156 }
157
158 inline const b2Vec2* b2PolygonShape::GetNormals() const
159 {
160         return m_normals;
161 }
162
163 #endif