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