File indexing completed on 2025-02-16 03:29:32

0001 /*
0002 * Copyright (c) 2006-2010 Erin Catto http://www.box2d.org
0003 *
0004 * This software is provided 'as-is', without any express or implied
0005 * warranty.  In no event will the authors be held liable for any damages
0006 * arising from the use of this software.
0007 * Permission is granted to anyone to use this software for any purpose,
0008 * including commercial applications, and to alter it and redistribute it
0009 * freely, subject to the following restrictions:
0010 * 1. The origin of this software must not be misrepresented; you must not
0011 * claim that you wrote the original software. If you use this software
0012 * in a product, an acknowledgment in the product documentation would be
0013 * appreciated but is not required.
0014 * 2. Altered source versions must be plainly marked as such, and must not be
0015 * misrepresented as being the original software.
0016 * 3. This notice may not be removed or altered from any source distribution.
0017 */
0018 
0019 #ifndef B2_CHAIN_SHAPE_H
0020 #define B2_CHAIN_SHAPE_H
0021 
0022 #include <Box2D/Collision/Shapes/b2Shape.h>
0023 
0024 class b2EdgeShape;
0025 
0026 /// A chain shape is a free form sequence of line segments.
0027 /// The chain has two-sided collision, so you can use inside and outside collision.
0028 /// Therefore, you may use any winding order.
0029 /// Since there may be many vertices, they are allocated using b2Alloc.
0030 /// Connectivity information is used to create smooth collisions.
0031 /// WARNING: The chain will not collide properly if there are self-intersections.
0032 class b2ChainShape : public b2Shape
0033 {
0034 public:
0035     b2ChainShape();
0036 
0037     /// The destructor frees the vertices using b2Free.
0038     ~b2ChainShape();
0039 
0040     /// Clear all data.
0041     void Clear();
0042 
0043     /// Create a loop. This automatically adjusts connectivity.
0044     /// @param vertices an array of vertices, these are copied
0045     /// @param count the vertex count
0046     void CreateLoop(const b2Vec2* vertices, int32 count);
0047 
0048     /// Create a chain with isolated end vertices.
0049     /// @param vertices an array of vertices, these are copied
0050     /// @param count the vertex count
0051     void CreateChain(const b2Vec2* vertices, int32 count);
0052 
0053     /// Establish connectivity to a vertex that precedes the first vertex.
0054     /// Don't call this for loops.
0055     void SetPrevVertex(const b2Vec2& prevVertex);
0056 
0057     /// Establish connectivity to a vertex that follows the last vertex.
0058     /// Don't call this for loops.
0059     void SetNextVertex(const b2Vec2& nextVertex);
0060 
0061     /// Implement b2Shape. Vertices are cloned using b2Alloc.
0062     b2Shape* Clone(b2BlockAllocator* allocator) const;
0063 
0064     /// @see b2Shape::GetChildCount
0065     int32 GetChildCount() const;
0066 
0067     /// Get a child edge.
0068     void GetChildEdge(b2EdgeShape* edge, int32 index) const;
0069 
0070     /// This always return false.
0071     /// @see b2Shape::TestPoint
0072     bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
0073 
0074     /// Implement b2Shape.
0075     bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
0076                     const b2Transform& transform, int32 childIndex) const;
0077 
0078     /// @see b2Shape::ComputeAABB
0079     void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32 childIndex) const;
0080 
0081     /// Chains have zero mass.
0082     /// @see b2Shape::ComputeMass
0083     void ComputeMass(b2MassData* massData, float32 density) const;
0084 
0085     /// The vertices. Owned by this class.
0086     b2Vec2* m_vertices;
0087 
0088     /// The vertex count.
0089     int32 m_count;
0090 
0091     b2Vec2 m_prevVertex, m_nextVertex;
0092     bool m_hasPrevVertex, m_hasNextVertex;
0093 };
0094 
0095 inline b2ChainShape::b2ChainShape()
0096 {
0097     m_type = e_chain;
0098     m_radius = b2_polygonRadius;
0099     m_vertices = NULL;
0100     m_count = 0;
0101     m_hasPrevVertex = false;
0102     m_hasNextVertex = false;
0103 }
0104 
0105 #endif