File indexing completed on 2024-06-02 03:42:36

0001 /*
0002 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
0003 *
0004 * This software is provided 'as-is', without any express or implied
0005 * warranty.  In no event will the authors be held liable for any damages
0006 * arising from the use of this software.
0007 * Permission is granted to anyone to use this software for any purpose,
0008 * including commercial applications, and to alter it and redistribute it
0009 * freely, subject to the following restrictions:
0010 * 1. The origin of this software must not be misrepresented; you must not
0011 * claim that you wrote the original software. If you use this software
0012 * in a product, an acknowledgment in the product documentation would be
0013 * appreciated but is not required.
0014 * 2. Altered source versions must be plainly marked as such, and must not be
0015 * misrepresented as being the original software.
0016 * 3. This notice may not be removed or altered from any source distribution.
0017 */
0018 
0019 #ifndef B2_POLYGON_SHAPE_H
0020 #define B2_POLYGON_SHAPE_H
0021 
0022 #include <Box2D/Collision/Shapes/b2Shape.h>
0023 
0024 /// A convex polygon. It is assumed that the interior of the polygon is to
0025 /// the left of each edge.
0026 /// Polygons have a maximum number of vertices equal to b2_maxPolygonVertices.
0027 /// In most cases you should not need many vertices for a convex polygon.
0028 class b2PolygonShape : public b2Shape
0029 {
0030 public:
0031     b2PolygonShape();
0032 
0033     /// Implement b2Shape.
0034     b2Shape* Clone(b2BlockAllocator* allocator) const;
0035 
0036     /// @see b2Shape::GetChildCount
0037     int32 GetChildCount() const;
0038 
0039     /// Create a convex hull from the given array of local points.
0040     /// The count must be in the range [3, b2_maxPolygonVertices].
0041     /// @warning the points may be re-ordered, even if they form a convex polygon
0042     /// @warning collinear points are handled but not removed. Collinear points
0043     /// may lead to poor stacking behavior.
0044     void Set(const b2Vec2* points, int32 count);
0045 
0046     /// Build vertices to represent an axis-aligned box centered on the local origin.
0047     /// @param hx the half-width.
0048     /// @param hy the half-height.
0049     void SetAsBox(float32 hx, float32 hy);
0050 
0051     /// Build vertices to represent an oriented box.
0052     /// @param hx the half-width.
0053     /// @param hy the half-height.
0054     /// @param center the center of the box in local coordinates.
0055     /// @param angle the rotation of the box in local coordinates.
0056     void SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle);
0057 
0058     /// @see b2Shape::TestPoint
0059     bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
0060 
0061     /// Implement b2Shape.
0062     bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
0063                     const b2Transform& transform, int32 childIndex) const;
0064 
0065     /// @see b2Shape::ComputeAABB
0066     void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
0067 
0068     /// @see b2Shape::ComputeMass
0069     void ComputeMass(b2MassData* massData, float32 density) const;
0070 
0071     /// Get the vertex count.
0072     int32 GetVertexCount() const { return m_count; }
0073 
0074     /// Get a vertex by index.
0075     const b2Vec2& GetVertex(int32 index) const;
0076 
0077     /// Validate convexity. This is a very time consuming operation.
0078     /// @returns true if valid
0079     bool Validate() const;
0080 
0081     b2Vec2 m_centroid;
0082     b2Vec2 m_vertices[b2_maxPolygonVertices];
0083     b2Vec2 m_normals[b2_maxPolygonVertices];
0084     int32 m_count;
0085 };
0086 
0087 inline b2PolygonShape::b2PolygonShape()
0088 {
0089     m_type = e_polygon;
0090     m_radius = b2_polygonRadius;
0091     m_count = 0;
0092     m_centroid.SetZero();
0093 }
0094 
0095 inline const b2Vec2& b2PolygonShape::GetVertex(int32 index) const
0096 {
0097     b2Assert(0 <= index && index < m_count);
0098     return m_vertices[index];
0099 }
0100 
0101 #endif