File indexing completed on 2025-08-03 03:49:55
0001 /* 0002 * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com 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 override; 0035 0036 /// @see b2Shape::GetChildCount 0037 int32 GetChildCount() const override; 0038 0039 /// Copy vertices. This assumes the vertices define a convex polygon. 0040 /// It is assumed that the exterior is the the right of each edge. 0041 /// The count must be in the range [3, b2_maxPolygonVertices]. 0042 void Set(const b2Vec2* vertices, int32 vertexCount); 0043 0044 /// Build vertices to represent an axis-aligned box. 0045 /// @param hx the half-width. 0046 /// @param hy the half-height. 0047 void SetAsBox(qreal hx, qreal hy); 0048 0049 /// Build vertices to represent an oriented box. 0050 /// @param hx the half-width. 0051 /// @param hy the half-height. 0052 /// @param center the center of the box in local coordinates. 0053 /// @param angle the rotation of the box in local coordinates. 0054 void SetAsBox(qreal hx, qreal hy, const b2Vec2& center, qreal angle); 0055 0056 /// @see b2Shape::TestPoint 0057 bool TestPoint(const b2Transform& transform, const b2Vec2& p) const override; 0058 0059 /// Implement b2Shape. 0060 bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input, 0061 const b2Transform& transform, int32 childIndex) const override; 0062 0063 /// @see b2Shape::ComputeAABB 0064 void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const override; 0065 0066 /// @see b2Shape::ComputeMass 0067 void ComputeMass(b2MassData* massData, qreal density) const override; 0068 0069 /// Get the vertex count. 0070 int32 GetVertexCount() const { return m_vertexCount; } 0071 0072 /// Get a vertex by index. 0073 const b2Vec2& GetVertex(int32 index) const; 0074 0075 b2Vec2 m_centroid; 0076 b2Vec2 m_vertices[b2_maxPolygonVertices]; 0077 b2Vec2 m_normals[b2_maxPolygonVertices]; 0078 int32 m_vertexCount; 0079 }; 0080 0081 inline b2PolygonShape::b2PolygonShape() 0082 { 0083 m_type = e_polygon; 0084 m_radius = b2_polygonRadius; 0085 m_vertexCount = 0; 0086 m_centroid.SetZero(); 0087 } 0088 0089 inline const b2Vec2& b2PolygonShape::GetVertex(int32 index) const 0090 { 0091 b2Assert(0 <= index && index < m_vertexCount); 0092 return m_vertices[index]; 0093 } 0094 0095 #endif