b2ChainShape.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2006-2010 Erin Catto http://www.box2d.org
  3. * Copyright (c) 2013 Google, Inc.
  4. *
  5. * This software is provided 'as-is', without any express or implied
  6. * warranty. In no event will the authors be held liable for any damages
  7. * arising from the use of this software.
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. * 1. The origin of this software must not be misrepresented; you must not
  12. * claim that you wrote the original software. If you use this software
  13. * in a product, an acknowledgment in the product documentation would be
  14. * appreciated but is not required.
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. * 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #ifndef B2_CHAIN_SHAPE_H
  20. #define B2_CHAIN_SHAPE_H
  21. #include <Box2D/Collision/Shapes/b2Shape.h>
  22. class b2EdgeShape;
  23. /// A chain shape is a free form sequence of line segments.
  24. /// The chain has two-sided collision, so you can use inside and outside collision.
  25. /// Therefore, you may use any winding order.
  26. /// Since there may be many vertices, they are allocated using b2Alloc.
  27. /// Connectivity information is used to create smooth collisions.
  28. /// WARNING: The chain will not collide properly if there are self-intersections.
  29. class b2ChainShape : public b2Shape
  30. {
  31. public:
  32. b2ChainShape();
  33. /// The destructor frees the vertices using b2Free.
  34. ~b2ChainShape();
  35. /// Create a loop. This automatically adjusts connectivity.
  36. /// @param vertices an array of vertices, these are copied
  37. /// @param count the vertex count
  38. void CreateLoop(const b2Vec2* vertices, int32 count);
  39. /// Create a chain with isolated end vertices.
  40. /// @param vertices an array of vertices, these are copied
  41. /// @param count the vertex count
  42. void CreateChain(const b2Vec2* vertices, int32 count);
  43. /// Establish connectivity to a vertex that precedes the first vertex.
  44. /// Don't call this for loops.
  45. void SetPrevVertex(const b2Vec2& prevVertex);
  46. /// Establish connectivity to a vertex that follows the last vertex.
  47. /// Don't call this for loops.
  48. void SetNextVertex(const b2Vec2& nextVertex);
  49. /// Implement b2Shape. Vertices are cloned using b2Alloc.
  50. b2Shape* Clone(b2BlockAllocator* allocator) const;
  51. /// @see b2Shape::GetChildCount
  52. int32 GetChildCount() const;
  53. /// Get a child edge.
  54. void GetChildEdge(b2EdgeShape* edge, int32 index) const;
  55. /// This always return false.
  56. /// @see b2Shape::TestPoint
  57. bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
  58. // @see b2Shape::ComputeDistance
  59. void ComputeDistance(const b2Transform& xf, const b2Vec2& p, float32* distance, b2Vec2* normal, int32 childIndex) const;
  60. /// Implement b2Shape.
  61. bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
  62. const b2Transform& transform, int32 childIndex) const;
  63. /// @see b2Shape::ComputeAABB
  64. void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
  65. /// Chains have zero mass.
  66. /// @see b2Shape::ComputeMass
  67. void ComputeMass(b2MassData* massData, float32 density) const;
  68. /// The vertices. Owned by this class.
  69. b2Vec2* m_vertices;
  70. /// The vertex count.
  71. int32 m_count;
  72. b2Vec2 m_prevVertex, m_nextVertex;
  73. bool m_hasPrevVertex, m_hasNextVertex;
  74. };
  75. inline b2ChainShape::b2ChainShape()
  76. {
  77. m_type = e_chain;
  78. m_radius = b2_polygonRadius;
  79. m_vertices = NULL;
  80. m_count = 0;
  81. m_hasPrevVertex = false;
  82. m_hasNextVertex = false;
  83. }
  84. #endif