b2_polygon_shape.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_POLYGON_SHAPE_H
  19. #define B2_POLYGON_SHAPE_H
  20. #include "b2_api.h"
  21. #include "b2_shape.h"
  22. /// A solid convex polygon. It is assumed that the interior of the polygon is to
  23. /// the left of each edge.
  24. /// Polygons have a maximum number of vertices equal to b2_maxPolygonVertices.
  25. /// In most cases you should not need many vertices for a convex polygon.
  26. class B2_API b2PolygonShape : public b2Shape
  27. {
  28. public:
  29. b2PolygonShape();
  30. /// Implement b2Shape.
  31. b2Shape* Clone(b2BlockAllocator* allocator) const override;
  32. /// @see b2Shape::GetChildCount
  33. int32 GetChildCount() const override;
  34. /// Create a convex hull from the given array of local points.
  35. /// The count must be in the range [3, b2_maxPolygonVertices].
  36. /// @warning the points may be re-ordered, even if they form a convex polygon
  37. /// @warning collinear points are handled but not removed. Collinear points
  38. /// may lead to poor stacking behavior.
  39. void Set(const b2Vec2* points, int32 count);
  40. /// Build vertices to represent an axis-aligned box centered on the local origin.
  41. /// @param hx the half-width.
  42. /// @param hy the half-height.
  43. void SetAsBox(float hx, float hy);
  44. /// Build vertices to represent an oriented box.
  45. /// @param hx the half-width.
  46. /// @param hy the half-height.
  47. /// @param center the center of the box in local coordinates.
  48. /// @param angle the rotation of the box in local coordinates.
  49. void SetAsBox(float hx, float hy, const b2Vec2& center, float angle);
  50. /// @see b2Shape::TestPoint
  51. bool TestPoint(const b2Transform& transform, const b2Vec2& p) const override;
  52. /// Implement b2Shape.
  53. /// @note because the polygon is solid, rays that start inside do not hit because the normal is
  54. /// not defined.
  55. bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  56. const b2Transform& transform, int32 childIndex) const override;
  57. /// @see b2Shape::ComputeAABB
  58. void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const override;
  59. /// @see b2Shape::ComputeMass
  60. void ComputeMass(b2MassData* massData, float density) const override;
  61. /// Validate convexity. This is a very time consuming operation.
  62. /// @returns true if valid
  63. bool Validate() const;
  64. b2Vec2 m_centroid;
  65. b2Vec2 m_vertices[b2_maxPolygonVertices];
  66. b2Vec2 m_normals[b2_maxPolygonVertices];
  67. int32 m_count;
  68. };
  69. inline b2PolygonShape::b2PolygonShape()
  70. {
  71. m_type = e_polygon;
  72. m_radius = b2_polygonRadius;
  73. m_count = 0;
  74. m_centroid.SetZero();
  75. }
  76. #endif