b2PolygonShape.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  3. * Copyright (c) 2013 Google, Inc.
  4. *
  5. * This software is provided 'as-is', without any express or implied
  6. * warranty. In no event will the authors be held liable for any damages
  7. * arising from the use of this software.
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. * 1. The origin of this software must not be misrepresented; you must not
  12. * claim that you wrote the original software. If you use this software
  13. * in a product, an acknowledgment in the product documentation would be
  14. * appreciated but is not required.
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. * 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #ifndef B2_POLYGON_SHAPE_H
  20. #define B2_POLYGON_SHAPE_H
  21. #include <Box2D/Collision/Shapes/b2Shape.h>
  22. /// A 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 b2PolygonShape : public b2Shape
  27. {
  28. public:
  29. b2PolygonShape();
  30. /// Implement b2Shape.
  31. b2Shape* Clone(b2BlockAllocator* allocator) const;
  32. /// @see b2Shape::GetChildCount
  33. int32 GetChildCount() const;
  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(float32 hx, float32 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(float32 hx, float32 hy, const b2Vec2& center, float32 angle);
  50. /// @see b2Shape::TestPoint
  51. bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
  52. // @see b2Shape::ComputeDistance
  53. void ComputeDistance(const b2Transform& xf, const b2Vec2& p, float32* distance, b2Vec2* normal, int32 childIndex) const;
  54. /// Implement b2Shape.
  55. bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  56. const b2Transform& transform, int32 childIndex) const;
  57. /// @see b2Shape::ComputeAABB
  58. void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
  59. /// @see b2Shape::ComputeMass
  60. void ComputeMass(b2MassData* massData, float32 density) const;
  61. /// Get the vertex count.
  62. int32 GetVertexCount() const { return m_count; }
  63. /// Get a vertex by index.
  64. const b2Vec2& GetVertex(int32 index) const;
  65. /// Validate convexity. This is a very time consuming operation.
  66. /// @returns true if valid
  67. bool Validate() const;
  68. #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
  69. public:
  70. /// Set centroid with direct floats.
  71. void SetCentroid(float32 x, float32 y);
  72. /// SetAsBox with direct floats for center.
  73. /// @see b2Shape::SetAsBox
  74. void SetAsBox(float32 hx,
  75. float32 hy,
  76. float32 centerX,
  77. float32 centerY,
  78. float32 angle);
  79. #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
  80. b2Vec2 m_centroid;
  81. b2Vec2 m_vertices[b2_maxPolygonVertices];
  82. b2Vec2 m_normals[b2_maxPolygonVertices];
  83. int32 m_count;
  84. };
  85. inline b2PolygonShape::b2PolygonShape()
  86. {
  87. m_type = e_polygon;
  88. m_radius = b2_polygonRadius;
  89. m_count = 0;
  90. m_centroid.SetZero();
  91. }
  92. inline const b2Vec2& b2PolygonShape::GetVertex(int32 index) const
  93. {
  94. b2Assert(0 <= index && index < m_count);
  95. return m_vertices[index];
  96. }
  97. #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
  98. inline void b2PolygonShape::SetCentroid(float32 x, float32 y)
  99. {
  100. m_centroid.Set(x, y);
  101. }
  102. inline void b2PolygonShape::SetAsBox(float32 hx,
  103. float32 hy,
  104. float32 centerX,
  105. float32 centerY,
  106. float32 angle) {
  107. SetAsBox(hx, hy, b2Vec2(centerX, centerY), angle);
  108. }
  109. #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
  110. #endif