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 #include <Box2D/Collision/Shapes/b2ChainShape.h>
0020 #include <Box2D/Collision/Shapes/b2EdgeShape.h>
0021 #include <new>
0022 #include <string.h>
0023 
0024 b2ChainShape::~b2ChainShape()
0025 {
0026     Clear();
0027 }
0028 
0029 void b2ChainShape::Clear()
0030 {
0031     b2Free(m_vertices);
0032     m_vertices = NULL;
0033     m_count = 0;
0034 }
0035 
0036 void b2ChainShape::CreateLoop(const b2Vec2* vertices, int32 count)
0037 {
0038     b2Assert(m_vertices == NULL && m_count == 0);
0039     b2Assert(count >= 3);
0040     for (int32 i = 1; i < count; ++i)
0041     {
0042         b2Vec2 v1 = vertices[i-1];
0043         b2Vec2 v2 = vertices[i];
0044         // If the code crashes here, it means your vertices are too close together.
0045         b2Assert(b2DistanceSquared(v1, v2) > b2_linearSlop * b2_linearSlop);
0046     }
0047 
0048     m_count = count + 1;
0049     m_vertices = (b2Vec2*)b2Alloc(m_count * sizeof(b2Vec2));
0050     memcpy(m_vertices, vertices, count * sizeof(b2Vec2));
0051     m_vertices[count] = m_vertices[0];
0052     m_prevVertex = m_vertices[m_count - 2];
0053     m_nextVertex = m_vertices[1];
0054     m_hasPrevVertex = true;
0055     m_hasNextVertex = true;
0056 }
0057 
0058 void b2ChainShape::CreateChain(const b2Vec2* vertices, int32 count)
0059 {
0060     b2Assert(m_vertices == NULL && m_count == 0);
0061     b2Assert(count >= 2);
0062     for (int32 i = 1; i < count; ++i)
0063     {
0064         // If the code crashes here, it means your vertices are too close together.
0065         b2Assert(b2DistanceSquared(vertices[i-1], vertices[i]) > b2_linearSlop * b2_linearSlop);
0066     }
0067 
0068     m_count = count;
0069     m_vertices = (b2Vec2*)b2Alloc(count * sizeof(b2Vec2));
0070     memcpy(m_vertices, vertices, m_count * sizeof(b2Vec2));
0071 
0072     m_hasPrevVertex = false;
0073     m_hasNextVertex = false;
0074 
0075     m_prevVertex.SetZero();
0076     m_nextVertex.SetZero();
0077 }
0078 
0079 void b2ChainShape::SetPrevVertex(const b2Vec2& prevVertex)
0080 {
0081     m_prevVertex = prevVertex;
0082     m_hasPrevVertex = true;
0083 }
0084 
0085 void b2ChainShape::SetNextVertex(const b2Vec2& nextVertex)
0086 {
0087     m_nextVertex = nextVertex;
0088     m_hasNextVertex = true;
0089 }
0090 
0091 b2Shape* b2ChainShape::Clone(b2BlockAllocator* allocator) const
0092 {
0093     void* mem = allocator->Allocate(sizeof(b2ChainShape));
0094     b2ChainShape* clone = new (mem) b2ChainShape;
0095     clone->CreateChain(m_vertices, m_count);
0096     clone->m_prevVertex = m_prevVertex;
0097     clone->m_nextVertex = m_nextVertex;
0098     clone->m_hasPrevVertex = m_hasPrevVertex;
0099     clone->m_hasNextVertex = m_hasNextVertex;
0100     return clone;
0101 }
0102 
0103 int32 b2ChainShape::GetChildCount() const
0104 {
0105     // edge count = vertex count - 1
0106     return m_count - 1;
0107 }
0108 
0109 void b2ChainShape::GetChildEdge(b2EdgeShape* edge, int32 index) const
0110 {
0111     b2Assert(0 <= index && index < m_count - 1);
0112     edge->m_type = b2Shape::e_edge;
0113     edge->m_radius = m_radius;
0114 
0115     edge->m_vertex1 = m_vertices[index + 0];
0116     edge->m_vertex2 = m_vertices[index + 1];
0117 
0118     if (index > 0)
0119     {
0120         edge->m_vertex0 = m_vertices[index - 1];
0121         edge->m_hasVertex0 = true;
0122     }
0123     else
0124     {
0125         edge->m_vertex0 = m_prevVertex;
0126         edge->m_hasVertex0 = m_hasPrevVertex;
0127     }
0128 
0129     if (index < m_count - 2)
0130     {
0131         edge->m_vertex3 = m_vertices[index + 2];
0132         edge->m_hasVertex3 = true;
0133     }
0134     else
0135     {
0136         edge->m_vertex3 = m_nextVertex;
0137         edge->m_hasVertex3 = m_hasNextVertex;
0138     }
0139 }
0140 
0141 bool b2ChainShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
0142 {
0143     B2_NOT_USED(xf);
0144     B2_NOT_USED(p);
0145     return false;
0146 }
0147 
0148 bool b2ChainShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
0149                             const b2Transform& xf, int32 childIndex) const
0150 {
0151     b2Assert(childIndex < m_count);
0152 
0153     b2EdgeShape edgeShape;
0154 
0155     int32 i1 = childIndex;
0156     int32 i2 = childIndex + 1;
0157     if (i2 == m_count)
0158     {
0159         i2 = 0;
0160     }
0161 
0162     edgeShape.m_vertex1 = m_vertices[i1];
0163     edgeShape.m_vertex2 = m_vertices[i2];
0164 
0165     return edgeShape.RayCast(output, input, xf, 0);
0166 }
0167 
0168 void b2ChainShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
0169 {
0170     b2Assert(childIndex < m_count);
0171 
0172     int32 i1 = childIndex;
0173     int32 i2 = childIndex + 1;
0174     if (i2 == m_count)
0175     {
0176         i2 = 0;
0177     }
0178 
0179     b2Vec2 v1 = b2Mul(xf, m_vertices[i1]);
0180     b2Vec2 v2 = b2Mul(xf, m_vertices[i2]);
0181 
0182     aabb->lowerBound = b2Min(v1, v2);
0183     aabb->upperBound = b2Max(v1, v2);
0184 }
0185 
0186 void b2ChainShape::ComputeMass(b2MassData* massData, float32 density) const
0187 {
0188     B2_NOT_USED(density);
0189 
0190     massData->mass = 0.0f;
0191     massData->center.SetZero();
0192     massData->I = 0.0f;
0193 }