b2PolygonShape.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  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 "b2Shape.h"
  21. /// Convex polygon. The vertices must be in CCW order for a right-handed
  22. /// coordinate system with the z-axis coming out of the screen.
  23. struct b2PolygonDef : public b2ShapeDef
  24. {
  25. b2PolygonDef()
  26. {
  27. type = e_polygonShape;
  28. vertexCount = 0;
  29. }
  30. /// Build vertices to represent an axis-aligned box.
  31. /// @param hx the half-width.
  32. /// @param hy the half-height.
  33. void SetAsBox(float32 hx, float32 hy);
  34. /// Build vertices to represent an oriented box.
  35. /// @param hx the half-width.
  36. /// @param hy the half-height.
  37. /// @param center the center of the box in local coordinates.
  38. /// @param angle the rotation of the box in local coordinates.
  39. void SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle);
  40. /// The polygon vertices in local coordinates.
  41. b2Vec2 vertices[b2_maxPolygonVertices];
  42. /// The number of polygon vertices.
  43. int32 vertexCount;
  44. };
  45. /// A convex polygon.
  46. class b2PolygonShape : public b2Shape
  47. {
  48. public:
  49. /// @see b2Shape::TestPoint
  50. bool TestPoint(const b2XForm& transform, const b2Vec2& p) const;
  51. /// @see b2Shape::TestSegment
  52. b2SegmentCollide TestSegment( const b2XForm& transform,
  53. float32* lambda,
  54. b2Vec2* normal,
  55. const b2Segment& segment,
  56. float32 maxLambda) const;
  57. /// @see b2Shape::ComputeAABB
  58. void ComputeAABB(b2AABB* aabb, const b2XForm& transform) const;
  59. /// @see b2Shape::ComputeSweptAABB
  60. void ComputeSweptAABB( b2AABB* aabb,
  61. const b2XForm& transform1,
  62. const b2XForm& transform2) const;
  63. /// @see b2Shape::ComputeMass
  64. void ComputeMass(b2MassData* massData) const;
  65. /// @see b2Shape::ComputeSubmergedArea
  66. float32 ComputeSubmergedArea( const b2Vec2& normal,
  67. float32 offset,
  68. const b2XForm& xf,
  69. b2Vec2* c) const;
  70. /// Get the oriented bounding box relative to the parent body.
  71. const b2OBB& GetOBB() const;
  72. /// Get local centroid relative to the parent body.
  73. const b2Vec2& GetCentroid() const;
  74. /// Get the vertex count.
  75. int32 GetVertexCount() const;
  76. /// Get the vertices in local coordinates.
  77. const b2Vec2* GetVertices() const;
  78. /// Get the core vertices in local coordinates. These vertices
  79. /// represent a smaller polygon that is used for time of impact
  80. /// computations.
  81. const b2Vec2* GetCoreVertices() const;
  82. /// Get the edge normal vectors. There is one for each vertex.
  83. const b2Vec2* GetNormals() const;
  84. /// Get the first vertex and apply the supplied transform.
  85. b2Vec2 GetFirstVertex(const b2XForm& xf) const;
  86. /// Get the centroid and apply the supplied transform.
  87. b2Vec2 Centroid(const b2XForm& xf) const;
  88. /// Get the support point in the given world direction.
  89. /// Use the supplied transform.
  90. b2Vec2 Support(const b2XForm& xf, const b2Vec2& d) const;
  91. private:
  92. friend class b2Shape;
  93. b2PolygonShape(const b2ShapeDef* def);
  94. void UpdateSweepRadius(const b2Vec2& center);
  95. // Local position of the polygon centroid.
  96. b2Vec2 m_centroid;
  97. b2OBB m_obb;
  98. b2Vec2 m_vertices[b2_maxPolygonVertices];
  99. b2Vec2 m_normals[b2_maxPolygonVertices];
  100. b2Vec2 m_coreVertices[b2_maxPolygonVertices];
  101. int32 m_vertexCount;
  102. };
  103. inline b2Vec2 b2PolygonShape::GetFirstVertex(const b2XForm& xf) const
  104. {
  105. return b2Mul(xf, m_coreVertices[0]);
  106. }
  107. inline const b2OBB& b2PolygonShape::GetOBB() const
  108. {
  109. return m_obb;
  110. }
  111. inline const b2Vec2& b2PolygonShape::GetCentroid() const
  112. {
  113. return m_centroid;
  114. }
  115. inline int32 b2PolygonShape::GetVertexCount() const
  116. {
  117. return m_vertexCount;
  118. }
  119. inline const b2Vec2* b2PolygonShape::GetVertices() const
  120. {
  121. return m_vertices;
  122. }
  123. inline const b2Vec2* b2PolygonShape::GetCoreVertices() const
  124. {
  125. return m_coreVertices;
  126. }
  127. inline const b2Vec2* b2PolygonShape::GetNormals() const
  128. {
  129. return m_normals;
  130. }
  131. #endif