b2ChainShape.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2006-2010 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 #include <Box2D/Collision/Shapes/b2ChainShape.h>
00020 #include <Box2D/Collision/Shapes/b2EdgeShape.h>
00021 #include <new>
00022 #include <string.h>
00023 
00024 b2ChainShape::~b2ChainShape()
00025 {
00026         Clear();
00027 }
00028 
00029 void b2ChainShape::Clear()
00030 {
00031         b2Free(m_vertices);
00032         m_vertices = NULL;
00033         m_count = 0;
00034 }
00035 
00036 void b2ChainShape::CreateLoop(const b2Vec2* vertices, int32 count)
00037 {
00038         b2Assert(m_vertices == NULL && m_count == 0);
00039         b2Assert(count >= 3);
00040         for (int32 i = 1; i < count; ++i)
00041         {
00042                 b2Vec2 v1 = vertices[i-1];
00043                 b2Vec2 v2 = vertices[i];
00044                 // If the code crashes here, it means your vertices are too close together.
00045                 b2Assert(b2DistanceSquared(v1, v2) > b2_linearSlop * b2_linearSlop);
00046         }
00047 
00048         m_count = count + 1;
00049         m_vertices = (b2Vec2*)b2Alloc(m_count * sizeof(b2Vec2));
00050         memcpy(m_vertices, vertices, count * sizeof(b2Vec2));
00051         m_vertices[count] = m_vertices[0];
00052         m_prevVertex = m_vertices[m_count - 2];
00053         m_nextVertex = m_vertices[1];
00054         m_hasPrevVertex = true;
00055         m_hasNextVertex = true;
00056 }
00057 
00058 void b2ChainShape::CreateChain(const b2Vec2* vertices, int32 count)
00059 {
00060         b2Assert(m_vertices == NULL && m_count == 0);
00061         b2Assert(count >= 2);
00062         for (int32 i = 1; i < count; ++i)
00063         {
00064                 // If the code crashes here, it means your vertices are too close together.
00065                 b2Assert(b2DistanceSquared(vertices[i-1], vertices[i]) > b2_linearSlop * b2_linearSlop);
00066         }
00067 
00068         m_count = count;
00069         m_vertices = (b2Vec2*)b2Alloc(count * sizeof(b2Vec2));
00070         memcpy(m_vertices, vertices, m_count * sizeof(b2Vec2));
00071 
00072         m_hasPrevVertex = false;
00073         m_hasNextVertex = false;
00074 
00075         m_prevVertex.SetZero();
00076         m_nextVertex.SetZero();
00077 }
00078 
00079 void b2ChainShape::SetPrevVertex(const b2Vec2& prevVertex)
00080 {
00081         m_prevVertex = prevVertex;
00082         m_hasPrevVertex = true;
00083 }
00084 
00085 void b2ChainShape::SetNextVertex(const b2Vec2& nextVertex)
00086 {
00087         m_nextVertex = nextVertex;
00088         m_hasNextVertex = true;
00089 }
00090 
00091 b2Shape* b2ChainShape::Clone(b2BlockAllocator* allocator) const
00092 {
00093         void* mem = allocator->Allocate(sizeof(b2ChainShape));
00094         b2ChainShape* clone = new (mem) b2ChainShape;
00095         clone->CreateChain(m_vertices, m_count);
00096         clone->m_prevVertex = m_prevVertex;
00097         clone->m_nextVertex = m_nextVertex;
00098         clone->m_hasPrevVertex = m_hasPrevVertex;
00099         clone->m_hasNextVertex = m_hasNextVertex;
00100         return clone;
00101 }
00102 
00103 int32 b2ChainShape::GetChildCount() const
00104 {
00105         // edge count = vertex count - 1
00106         return m_count - 1;
00107 }
00108 
00109 void b2ChainShape::GetChildEdge(b2EdgeShape* edge, int32 index) const
00110 {
00111         b2Assert(0 <= index && index < m_count - 1);
00112         edge->m_type = b2Shape::e_edge;
00113         edge->m_radius = m_radius;
00114 
00115         edge->m_vertex1 = m_vertices[index + 0];
00116         edge->m_vertex2 = m_vertices[index + 1];
00117 
00118         if (index > 0)
00119         {
00120                 edge->m_vertex0 = m_vertices[index - 1];
00121                 edge->m_hasVertex0 = true;
00122         }
00123         else
00124         {
00125                 edge->m_vertex0 = m_prevVertex;
00126                 edge->m_hasVertex0 = m_hasPrevVertex;
00127         }
00128 
00129         if (index < m_count - 2)
00130         {
00131                 edge->m_vertex3 = m_vertices[index + 2];
00132                 edge->m_hasVertex3 = true;
00133         }
00134         else
00135         {
00136                 edge->m_vertex3 = m_nextVertex;
00137                 edge->m_hasVertex3 = m_hasNextVertex;
00138         }
00139 }
00140 
00141 bool b2ChainShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
00142 {
00143         B2_NOT_USED(xf);
00144         B2_NOT_USED(p);
00145         return false;
00146 }
00147 
00148 bool b2ChainShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
00149                                                         const b2Transform& xf, int32 childIndex) const
00150 {
00151         b2Assert(childIndex < m_count);
00152 
00153         b2EdgeShape edgeShape;
00154 
00155         int32 i1 = childIndex;
00156         int32 i2 = childIndex + 1;
00157         if (i2 == m_count)
00158         {
00159                 i2 = 0;
00160         }
00161 
00162         edgeShape.m_vertex1 = m_vertices[i1];
00163         edgeShape.m_vertex2 = m_vertices[i2];
00164 
00165         return edgeShape.RayCast(output, input, xf, 0);
00166 }
00167 
00168 void b2ChainShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
00169 {
00170         b2Assert(childIndex < m_count);
00171 
00172         int32 i1 = childIndex;
00173         int32 i2 = childIndex + 1;
00174         if (i2 == m_count)
00175         {
00176                 i2 = 0;
00177         }
00178 
00179         b2Vec2 v1 = b2Mul(xf, m_vertices[i1]);
00180         b2Vec2 v2 = b2Mul(xf, m_vertices[i2]);
00181 
00182         aabb->lowerBound = b2Min(v1, v2);
00183         aabb->upperBound = b2Max(v1, v2);
00184 }
00185 
00186 void b2ChainShape::ComputeMass(b2MassData* massData, float32 density) const
00187 {
00188         B2_NOT_USED(density);
00189 
00190         massData->mass = 0.0f;
00191         massData->center.SetZero();
00192         massData->I = 0.0f;
00193 }


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 22:08:34