b2EdgeShape.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_EDGE_SHAPE_H
  19. #define B2_EDGE_SHAPE_H
  20. #include "b2Shape.h"
  21. /// This structure is used to build circle shapes.
  22. struct b2EdgeChainDef : public b2ShapeDef
  23. {
  24. b2EdgeChainDef()
  25. {
  26. type = e_edgeShape;
  27. vertexCount = 0;
  28. isALoop = true;
  29. vertices = NULL;
  30. }
  31. /// The vertices in local coordinates. You must manage the memory
  32. /// of this array on your own, outside of Box2D.
  33. b2Vec2* vertices;
  34. /// The number of vertices in the chain.
  35. int32 vertexCount;
  36. /// Whether to create an extra edge between the first and last vertices:
  37. bool isALoop;
  38. };
  39. /// A circle shape.
  40. class b2EdgeShape : public b2Shape
  41. {
  42. public:
  43. /// @see b2Shape::TestPoint
  44. bool TestPoint(const b2XForm& transform, const b2Vec2& p) const;
  45. /// @see b2Shape::TestSegment
  46. b2SegmentCollide TestSegment( const b2XForm& transform,
  47. float32* lambda,
  48. b2Vec2* normal,
  49. const b2Segment& segment,
  50. float32 maxLambda) const;
  51. /// @see b2Shape::ComputeAABB
  52. void ComputeAABB(b2AABB* aabb, const b2XForm& transform) const;
  53. /// @see b2Shape::ComputeSweptAABB
  54. void ComputeSweptAABB( b2AABB* aabb,
  55. const b2XForm& transform1,
  56. const b2XForm& transform2) const;
  57. /// @see b2Shape::ComputeMass
  58. void ComputeMass(b2MassData* massData) const;
  59. /// @warning This only gives a consistent and sensible answer when when summed over a body only contains loops of edges
  60. /// @see b2Shape::ComputeSubmergedArea
  61. float32 ComputeSubmergedArea( const b2Vec2& normal,
  62. float32 offset,
  63. const b2XForm& xf,
  64. b2Vec2* c) const;
  65. /// Linear distance from vertex1 to vertex2:
  66. float32 GetLength() const;
  67. /// Local position of vertex in parent body
  68. const b2Vec2& GetVertex1() const;
  69. /// Local position of vertex in parent body
  70. const b2Vec2& GetVertex2() const;
  71. /// "Core" vertex with TOI slop for b2Distance functions:
  72. const b2Vec2& GetCoreVertex1() const;
  73. /// "Core" vertex with TOI slop for b2Distance functions:
  74. const b2Vec2& GetCoreVertex2() const;
  75. /// Perpendicular unit vector point, pointing from the solid side to the empty side:
  76. const b2Vec2& GetNormalVector() const;
  77. /// Parallel unit vector, pointing from vertex1 to vertex2:
  78. const b2Vec2& GetDirectionVector() const;
  79. const b2Vec2& GetCorner1Vector() const;
  80. const b2Vec2& GetCorner2Vector() const;
  81. bool Corner1IsConvex() const;
  82. bool Corner2IsConvex() const;
  83. b2Vec2 GetFirstVertex(const b2XForm& xf) const;
  84. b2Vec2 Support(const b2XForm& xf, const b2Vec2& d) const;
  85. /// Get the next edge in the chain.
  86. b2EdgeShape* GetNextEdge() const;
  87. /// Get the previous edge in the chain.
  88. b2EdgeShape* GetPrevEdge() const;
  89. void SetPrevEdge(b2EdgeShape* edge, const b2Vec2& core, const b2Vec2& cornerDir, bool convex);
  90. void SetNextEdge(b2EdgeShape* edge, const b2Vec2& core, const b2Vec2& cornerDir, bool convex);
  91. private:
  92. friend class b2Shape;
  93. friend class b2Body;
  94. b2EdgeShape(const b2Vec2& v1, const b2Vec2& v2, const b2ShapeDef* def);
  95. void UpdateSweepRadius(const b2Vec2& center);
  96. b2Vec2 m_v1;
  97. b2Vec2 m_v2;
  98. b2Vec2 m_coreV1;
  99. b2Vec2 m_coreV2;
  100. float32 m_length;
  101. b2Vec2 m_normal;
  102. b2Vec2 m_direction;
  103. // Unit vector halfway between m_direction and m_prevEdge.m_direction:
  104. b2Vec2 m_cornerDir1;
  105. // Unit vector halfway between m_direction and m_nextEdge.m_direction:
  106. b2Vec2 m_cornerDir2;
  107. bool m_cornerConvex1;
  108. bool m_cornerConvex2;
  109. b2EdgeShape* m_nextEdge;
  110. b2EdgeShape* m_prevEdge;
  111. };
  112. inline float32 b2EdgeShape::GetLength() const
  113. {
  114. return m_length;
  115. }
  116. inline const b2Vec2& b2EdgeShape::GetVertex1() const
  117. {
  118. return m_v1;
  119. }
  120. inline const b2Vec2& b2EdgeShape::GetVertex2() const
  121. {
  122. return m_v2;
  123. }
  124. inline const b2Vec2& b2EdgeShape::GetCoreVertex1() const
  125. {
  126. return m_coreV1;
  127. }
  128. inline const b2Vec2& b2EdgeShape::GetCoreVertex2() const
  129. {
  130. return m_coreV2;
  131. }
  132. inline const b2Vec2& b2EdgeShape::GetNormalVector() const
  133. {
  134. return m_normal;
  135. }
  136. inline const b2Vec2& b2EdgeShape::GetDirectionVector() const
  137. {
  138. return m_direction;
  139. }
  140. inline const b2Vec2& b2EdgeShape::GetCorner1Vector() const
  141. {
  142. return m_cornerDir1;
  143. }
  144. inline const b2Vec2& b2EdgeShape::GetCorner2Vector() const
  145. {
  146. return m_cornerDir2;
  147. }
  148. inline b2EdgeShape* b2EdgeShape::GetNextEdge() const
  149. {
  150. return m_nextEdge;
  151. }
  152. inline b2EdgeShape* b2EdgeShape::GetPrevEdge() const
  153. {
  154. return m_prevEdge;
  155. }
  156. inline b2Vec2 b2EdgeShape::GetFirstVertex(const b2XForm& xf) const
  157. {
  158. return b2Mul(xf, m_coreV1);
  159. }
  160. inline bool b2EdgeShape::Corner1IsConvex() const
  161. {
  162. return m_cornerConvex1;
  163. }
  164. inline bool b2EdgeShape::Corner2IsConvex() const
  165. {
  166. return m_cornerConvex2;
  167. }
  168. #endif