b2ChainShape.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2006-2010 Erin Catto http://www.box2d.org
  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_CHAIN_SHAPE_H
  19. #define B2_CHAIN_SHAPE_H
  20. #include <Box2D/Collision/Shapes/b2Shape.h>
  21. class b2EdgeShape;
  22. /// A chain shape is a free form sequence of line segments.
  23. /// The chain has two-sided collision, so you can use inside and outside collision.
  24. /// Therefore, you may use any winding order.
  25. /// Since there may be many vertices, they are allocated using b2Alloc.
  26. /// Connectivity information is used to create smooth collisions.
  27. /// WARNING: The chain will not collide properly if there are self-intersections.
  28. class 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 isolated end vertices.
  41. /// @param vertices an array of vertices, these are copied
  42. /// @param count the vertex count
  43. void CreateChain(const b2Vec2* vertices, int32 count);
  44. /// Establish connectivity to a vertex that precedes the first vertex.
  45. /// Don't call this for loops.
  46. void SetPrevVertex(const b2Vec2& prevVertex);
  47. /// Establish connectivity to a vertex that follows the last vertex.
  48. /// Don't call this for loops.
  49. void SetNextVertex(const b2Vec2& nextVertex);
  50. /// Implement b2Shape. Vertices are cloned using b2Alloc.
  51. b2Shape* Clone(b2BlockAllocator* allocator) const;
  52. /// @see b2Shape::GetChildCount
  53. int32 GetChildCount() const;
  54. /// Get a child edge.
  55. void GetChildEdge(b2EdgeShape* edge, int32 index) const;
  56. /// This always return false.
  57. /// @see b2Shape::TestPoint
  58. bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
  59. /// Implement b2Shape.
  60. bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  61. const b2Transform& transform, int32 childIndex) const;
  62. /// @see b2Shape::ComputeAABB
  63. void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
  64. /// Chains have zero mass.
  65. /// @see b2Shape::ComputeMass
  66. void ComputeMass(b2MassData* massData, float32 density) const;
  67. /// The vertices. Owned by this class.
  68. b2Vec2* m_vertices;
  69. /// The vertex count.
  70. int32 m_count;
  71. b2Vec2 m_prevVertex, m_nextVertex;
  72. bool m_hasPrevVertex, m_hasNextVertex;
  73. };
  74. inline b2ChainShape::b2ChainShape()
  75. {
  76. m_type = e_chain;
  77. m_radius = b2_polygonRadius;
  78. m_vertices = NULL;
  79. m_count = 0;
  80. m_hasPrevVertex = false;
  81. m_hasNextVertex = false;
  82. }
  83. #endif