b2ChainShape.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /// Create a loop. This automatically adjusts connectivity.
  35. /// @param vertices an array of vertices, these are copied
  36. /// @param count the vertex count
  37. void CreateLoop(const b2Vec2* vertices, int32 count);
  38. /// Create a chain with isolated end vertices.
  39. /// @param vertices an array of vertices, these are copied
  40. /// @param count the vertex count
  41. void CreateChain(const b2Vec2* vertices, int32 count);
  42. /// Establish connectivity to a vertex that precedes the first vertex.
  43. /// Don't call this for loops.
  44. void SetPrevVertex(const b2Vec2& prevVertex);
  45. /// Establish connectivity to a vertex that follows the last vertex.
  46. /// Don't call this for loops.
  47. void SetNextVertex(const b2Vec2& nextVertex);
  48. /// Implement b2Shape. Vertices are cloned using b2Alloc.
  49. b2Shape* Clone(b2BlockAllocator* allocator) const;
  50. /// @see b2Shape::GetChildCount
  51. int32 GetChildCount() const;
  52. /// Get a child edge.
  53. void GetChildEdge(b2EdgeShape* edge, int32 index) const;
  54. /// This always return false.
  55. /// @see b2Shape::TestPoint
  56. bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
  57. /// Implement b2Shape.
  58. bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  59. const b2Transform& transform, int32 childIndex) const;
  60. /// @see b2Shape::ComputeAABB
  61. void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
  62. /// Chains have zero mass.
  63. /// @see b2Shape::ComputeMass
  64. void ComputeMass(b2MassData* massData, float32 density) const;
  65. /// The vertices. Owned by this class.
  66. b2Vec2* m_vertices;
  67. /// The vertex count.
  68. int32 m_count;
  69. b2Vec2 m_prevVertex, m_nextVertex;
  70. bool m_hasPrevVertex, m_hasNextVertex;
  71. };
  72. inline b2ChainShape::b2ChainShape()
  73. {
  74. m_type = e_chain;
  75. m_radius = b2_polygonRadius;
  76. m_vertices = NULL;
  77. m_count = 0;
  78. m_hasPrevVertex = false;
  79. m_hasNextVertex = false;
  80. }
  81. #endif