b2_polygon_shape.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. struct b2Hull;
  23. /// A solid convex polygon. It is assumed that the interior of the polygon is to
  24. /// the left of each edge.
  25. /// Polygons have a maximum number of vertices equal to b2_maxPolygonVertices.
  26. /// In most cases you should not need many vertices for a convex polygon.
  27. class B2_API b2PolygonShape : public b2Shape
  28. {
  29. public:
  30. b2PolygonShape();
  31. /// Implement b2Shape.
  32. b2Shape* Clone(b2BlockAllocator* allocator) const override;
  33. /// @see b2Shape::GetChildCount
  34. int32 GetChildCount() const override;
  35. /// Create a convex hull from the given array of local points.
  36. /// The count must be in the range [3, b2_maxPolygonVertices].
  37. /// @warning the points may be re-ordered, even if they form a convex polygon
  38. /// @warning if this fails then the polygon is invalid
  39. /// @returns true if valid
  40. bool Set(const b2Vec2* points, int32 count);
  41. /// Create a polygon from a given convex hull (see b2ComputeHull).
  42. /// @warning the hull must be valid or this will crash or have unexpected behavior
  43. void Set(const b2Hull& hull);
  44. /// Build vertices to represent an axis-aligned box centered on the local origin.
  45. /// @param hx the half-width.
  46. /// @param hy the half-height.
  47. void SetAsBox(float hx, float hy);
  48. /// Build vertices to represent an oriented box.
  49. /// @param hx the half-width.
  50. /// @param hy the half-height.
  51. /// @param center the center of the box in local coordinates.
  52. /// @param angle the rotation of the box in local coordinates.
  53. void SetAsBox(float hx, float hy, const b2Vec2& center, float angle);
  54. /// @see b2Shape::TestPoint
  55. bool TestPoint(const b2Transform& transform, const b2Vec2& p) const override;
  56. /// Implement b2Shape.
  57. /// @note because the polygon is solid, rays that start inside do not hit because the normal is
  58. /// not defined.
  59. bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  60. const b2Transform& transform, int32 childIndex) const override;
  61. /// @see b2Shape::ComputeAABB
  62. void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const override;
  63. /// @see b2Shape::ComputeMass
  64. void ComputeMass(b2MassData* massData, float density) const override;
  65. /// Validate convexity. This is a very time consuming operation.
  66. /// @returns true if valid
  67. bool Validate() const;
  68. b2Vec2 m_centroid;
  69. b2Vec2 m_vertices[b2_maxPolygonVertices];
  70. b2Vec2 m_normals[b2_maxPolygonVertices];
  71. int32 m_count;
  72. };
  73. #endif