b2_chain_shape.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_CHAIN_SHAPE_H
  19. #define B2_CHAIN_SHAPE_H
  20. #include "b2_api.h"
  21. #include "b2_shape.h"
  22. class b2EdgeShape;
  23. /// A chain shape is a free form sequence of line segments.
  24. /// The chain has one-sided collision, with the surface normal pointing to the right of the edge.
  25. /// This provides a counter-clockwise winding like the polygon shape.
  26. /// Connectivity information is used to create smooth collisions.
  27. /// @warning the chain will not collide properly if there are self-intersections.
  28. class B2_API b2ChainShape : public b2Shape
  29. {
  30. public:
  31. b2ChainShape();
  32. /// The destructor frees the vertices using b2Free.
  33. ~b2ChainShape();
  34. /// Clear all data.
  35. void Clear();
  36. /// Create a loop. This automatically adjusts connectivity.
  37. /// @param vertices an array of vertices, these are copied
  38. /// @param count the vertex count
  39. void CreateLoop(const b2Vec2* vertices, int32 count);
  40. /// Create a chain with ghost vertices to connect multiple chains together.
  41. /// @param vertices an array of vertices, these are copied
  42. /// @param count the vertex count
  43. /// @param prevVertex previous vertex from chain that connects to the start
  44. /// @param nextVertex next vertex from chain that connects to the end
  45. void CreateChain(const b2Vec2* vertices, int32 count,
  46. const b2Vec2& prevVertex, const b2Vec2& nextVertex);
  47. /// Implement b2Shape. Vertices are cloned using b2Alloc.
  48. b2Shape* Clone(b2BlockAllocator* allocator) const override;
  49. /// @see b2Shape::GetChildCount
  50. int32 GetChildCount() const override;
  51. /// Get a child edge.
  52. void GetChildEdge(b2EdgeShape* edge, int32 index) const;
  53. /// This always return false.
  54. /// @see b2Shape::TestPoint
  55. bool TestPoint(const b2Transform& transform, const b2Vec2& p) const override;
  56. /// Implement b2Shape.
  57. bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  58. const b2Transform& transform, int32 childIndex) const override;
  59. /// @see b2Shape::ComputeAABB
  60. void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const override;
  61. /// Chains have zero mass.
  62. /// @see b2Shape::ComputeMass
  63. void ComputeMass(b2MassData* massData, float density) const override;
  64. /// The vertices. Owned by this class.
  65. b2Vec2* m_vertices;
  66. /// The vertex count.
  67. int32 m_count;
  68. b2Vec2 m_prevVertex, m_nextVertex;
  69. };
  70. inline b2ChainShape::b2ChainShape()
  71. {
  72. m_type = e_chain;
  73. m_radius = b2_polygonRadius;
  74. m_vertices = nullptr;
  75. m_count = 0;
  76. }
  77. #endif