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
19 #include <Box2D/Collision/Shapes/b2ChainShape.h>
20 #include <Box2D/Collision/Shapes/b2EdgeShape.h>
21 #include <new>
22 #include <cstring>
23 using namespace std;
24
~b2ChainShape()25 b2ChainShape::~b2ChainShape()
26 {
27 Clear();
28 }
29
Clear()30 void b2ChainShape::Clear()
31 {
32 b2Free(m_vertices);
33 m_vertices = NULL;
34 m_count = 0;
35 }
36
CreateLoop(const b2Vec2 * vertices,int32 count)37 void b2ChainShape::CreateLoop(const b2Vec2* vertices, int32 count)
38 {
39 b2Assert(m_vertices == NULL && m_count == 0);
40 b2Assert(count >= 3);
41 for (int32 i = 1; i < count; ++i)
42 {
43 b2Vec2 v1 = vertices[i-1];
44 b2Vec2 v2 = vertices[i];
45 // If the code crashes here, it means your vertices are too close together.
46 b2Assert(b2DistanceSquared(v1, v2) > b2_linearSlop * b2_linearSlop);
47 }
48
49 m_count = count + 1;
50 m_vertices = (b2Vec2*)b2Alloc(m_count * sizeof(b2Vec2));
51 memcpy(m_vertices, vertices, count * sizeof(b2Vec2));
52 m_vertices[count] = m_vertices[0];
53 m_prevVertex = m_vertices[m_count - 2];
54 m_nextVertex = m_vertices[1];
55 m_hasPrevVertex = true;
56 m_hasNextVertex = true;
57 }
58
CreateChain(const b2Vec2 * vertices,int32 count)59 void b2ChainShape::CreateChain(const b2Vec2* vertices, int32 count)
60 {
61 b2Assert(m_vertices == NULL && m_count == 0);
62 b2Assert(count >= 2);
63 for (int32 i = 1; i < count; ++i)
64 {
65 // If the code crashes here, it means your vertices are too close together.
66 b2Assert(b2DistanceSquared(vertices[i-1], vertices[i]) > b2_linearSlop * b2_linearSlop);
67 }
68
69 m_count = count;
70 m_vertices = (b2Vec2*)b2Alloc(count * sizeof(b2Vec2));
71 memcpy(m_vertices, vertices, m_count * sizeof(b2Vec2));
72
73 m_hasPrevVertex = false;
74 m_hasNextVertex = false;
75
76 m_prevVertex.SetZero();
77 m_nextVertex.SetZero();
78 }
79
SetPrevVertex(const b2Vec2 & prevVertex)80 void b2ChainShape::SetPrevVertex(const b2Vec2& prevVertex)
81 {
82 m_prevVertex = prevVertex;
83 m_hasPrevVertex = true;
84 }
85
SetNextVertex(const b2Vec2 & nextVertex)86 void b2ChainShape::SetNextVertex(const b2Vec2& nextVertex)
87 {
88 m_nextVertex = nextVertex;
89 m_hasNextVertex = true;
90 }
91
Clone(b2BlockAllocator * allocator) const92 b2Shape* b2ChainShape::Clone(b2BlockAllocator* allocator) const
93 {
94 void* mem = allocator->Allocate(sizeof(b2ChainShape));
95 b2ChainShape* clone = new (mem) b2ChainShape;
96 clone->CreateChain(m_vertices, m_count);
97 clone->m_prevVertex = m_prevVertex;
98 clone->m_nextVertex = m_nextVertex;
99 clone->m_hasPrevVertex = m_hasPrevVertex;
100 clone->m_hasNextVertex = m_hasNextVertex;
101 return clone;
102 }
103
GetChildCount() const104 int32 b2ChainShape::GetChildCount() const
105 {
106 // edge count = vertex count - 1
107 return m_count - 1;
108 }
109
GetChildEdge(b2EdgeShape * edge,int32 index) const110 void b2ChainShape::GetChildEdge(b2EdgeShape* edge, int32 index) const
111 {
112 b2Assert(0 <= index && index < m_count - 1);
113 edge->m_type = b2Shape::e_edge;
114 edge->m_radius = m_radius;
115
116 edge->m_vertex1 = m_vertices[index + 0];
117 edge->m_vertex2 = m_vertices[index + 1];
118
119 if (index > 0)
120 {
121 edge->m_vertex0 = m_vertices[index - 1];
122 edge->m_hasVertex0 = true;
123 }
124 else
125 {
126 edge->m_vertex0 = m_prevVertex;
127 edge->m_hasVertex0 = m_hasPrevVertex;
128 }
129
130 if (index < m_count - 2)
131 {
132 edge->m_vertex3 = m_vertices[index + 2];
133 edge->m_hasVertex3 = true;
134 }
135 else
136 {
137 edge->m_vertex3 = m_nextVertex;
138 edge->m_hasVertex3 = m_hasNextVertex;
139 }
140 }
141
TestPoint(const b2Transform & xf,const b2Vec2 & p) const142 bool b2ChainShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
143 {
144 B2_NOT_USED(xf);
145 B2_NOT_USED(p);
146 return false;
147 }
148
RayCast(b2RayCastOutput * output,const b2RayCastInput & input,const b2Transform & xf,int32 childIndex) const149 bool b2ChainShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
150 const b2Transform& xf, int32 childIndex) const
151 {
152 b2Assert(childIndex < m_count);
153
154 b2EdgeShape edgeShape;
155
156 int32 i1 = childIndex;
157 int32 i2 = childIndex + 1;
158 if (i2 == m_count)
159 {
160 i2 = 0;
161 }
162
163 edgeShape.m_vertex1 = m_vertices[i1];
164 edgeShape.m_vertex2 = m_vertices[i2];
165
166 return edgeShape.RayCast(output, input, xf, 0);
167 }
168
ComputeAABB(b2AABB * aabb,const b2Transform & xf,int32 childIndex) const169 void b2ChainShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
170 {
171 b2Assert(childIndex < m_count);
172
173 int32 i1 = childIndex;
174 int32 i2 = childIndex + 1;
175 if (i2 == m_count)
176 {
177 i2 = 0;
178 }
179
180 b2Vec2 v1 = b2Mul(xf, m_vertices[i1]);
181 b2Vec2 v2 = b2Mul(xf, m_vertices[i2]);
182
183 aabb->lowerBound = b2Min(v1, v2);
184 aabb->upperBound = b2Max(v1, v2);
185 }
186
ComputeMass(b2MassData * massData,float32 density) const187 void b2ChainShape::ComputeMass(b2MassData* massData, float32 density) const
188 {
189 B2_NOT_USED(density);
190
191 massData->mass = 0.0f;
192 massData->center.SetZero();
193 massData->I = 0.0f;
194 }
195