b2PolygonShape.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  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. #ifndef B2_POLYGON_SHAPE_H
  19. #define B2_POLYGON_SHAPE_H
  20. #include <Box2D/Collision/Shapes/b2Shape.h>
  21. /// A convex polygon. It is assumed that the interior of the polygon is to
  22. /// the left of each edge.
  23. /// Polygons have a maximum number of vertices equal to b2_maxPolygonVertices.
  24. /// In most cases you should not need many vertices for a convex polygon.
  25. class b2PolygonShape : public b2Shape
  26. {
  27. public:
  28. b2PolygonShape();
  29. /// Implement b2Shape.
  30. b2Shape* Clone(b2BlockAllocator* allocator) const;
  31. /// @see b2Shape::GetChildCount
  32. int32 GetChildCount() const;
  33. /// Create a convex hull from the given array of local points.
  34. /// The count must be in the range [3, b2_maxPolygonVertices].
  35. /// @warning the points may be re-ordered, even if they form a convex polygon
  36. /// @warning collinear points are handled but not removed. Collinear points
  37. /// may lead to poor stacking behavior.
  38. void Set(const b2Vec2* points, int32 count);
  39. /// Build vertices to represent an axis-aligned box centered on the local origin.
  40. /// @param hx the half-width.
  41. /// @param hy the half-height.
  42. void SetAsBox(float32 hx, float32 hy);
  43. /// Build vertices to represent an oriented box.
  44. /// @param hx the half-width.
  45. /// @param hy the half-height.
  46. /// @param center the center of the box in local coordinates.
  47. /// @param angle the rotation of the box in local coordinates.
  48. void SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle);
  49. /// @see b2Shape::TestPoint
  50. bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
  51. /// Implement b2Shape.
  52. bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  53. const b2Transform& transform, int32 childIndex) const;
  54. /// @see b2Shape::ComputeAABB
  55. void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
  56. /// @see b2Shape::ComputeMass
  57. void ComputeMass(b2MassData* massData, float32 density) const;
  58. /// Get the vertex count.
  59. int32 GetVertexCount() const { return m_count; }
  60. /// Get a vertex by index.
  61. const b2Vec2& GetVertex(int32 index) const;
  62. /// Validate convexity. This is a very time consuming operation.
  63. /// @returns true if valid
  64. bool Validate() const;
  65. b2Vec2 m_centroid;
  66. b2Vec2 m_vertices[b2_maxPolygonVertices];
  67. b2Vec2 m_normals[b2_maxPolygonVertices];
  68. int32 m_count;
  69. };
  70. inline b2PolygonShape::b2PolygonShape()
  71. {
  72. m_type = e_polygon;
  73. m_radius = b2_polygonRadius;
  74. m_count = 0;
  75. m_centroid.SetZero();
  76. }
  77. inline const b2Vec2& b2PolygonShape::GetVertex(int32 index) const
  78. {
  79. b2Assert(0 <= index && index < m_count);
  80. return m_vertices[index];
  81. }
  82. #endif