b2ChainShape.cpp
Go to the documentation of this file.
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 
21 #include <new>
22 #include <string.h>
23 
25 {
26  Clear();
27 }
28 
30 {
32  m_vertices = NULL;
33  m_count = 0;
34 }
35 
36 void 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.
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];
54  m_hasPrevVertex = true;
55  m_hasNextVertex = true;
56 }
57 
58 void 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 
77 }
78 
79 void b2ChainShape::SetPrevVertex(const b2Vec2& prevVertex)
80 {
81  m_prevVertex = prevVertex;
82  m_hasPrevVertex = true;
83 }
84 
85 void b2ChainShape::SetNextVertex(const b2Vec2& nextVertex)
86 {
87  m_nextVertex = nextVertex;
88  m_hasNextVertex = true;
89 }
90 
92 {
93  void* mem = allocator->Allocate(sizeof(b2ChainShape));
94  b2ChainShape* clone = new (mem) b2ChainShape;
96  clone->m_prevVertex = m_prevVertex;
97  clone->m_nextVertex = m_nextVertex;
100  return clone;
101 }
102 
104 {
105  // edge count = vertex count - 1
106  return m_count - 1;
107 }
108 
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;
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;
138  }
139 }
140 
141 bool 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 
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 
168 void 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 
186 void 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 }
b2Vec2 m_nextVertex
Definition: b2ChainShape.h:91
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2Math.h:433
b2Vec2 * m_vertices
The vertices. Owned by this class.
Definition: b2ChainShape.h:86
bool RayCast(b2RayCastOutput *output, const b2RayCastInput &input, const b2Transform &transform, int32 childIndex) const
Implement b2Shape.
Definition: b2EdgeShape.cpp:54
#define b2_linearSlop
Definition: b2Settings.h:68
b2Vec2 lowerBound
the lower vertex
Definition: b2Collision.h:214
b2Shape * Clone(b2BlockAllocator *allocator) const
Implement b2Shape. Vertices are cloned using b2Alloc.
bool RayCast(b2RayCastOutput *output, const b2RayCastInput &input, const b2Transform &transform, int32 childIndex) const
Implement b2Shape.
float32 I
The rotational inertia of the shape about the local origin.
Definition: b2Shape.h:36
Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
Definition: b2Collision.h:147
#define B2_NOT_USED(x)
Definition: b2Settings.h:26
b2Vec2 m_vertex0
Optional adjacent vertices. These are used for smooth collision.
Definition: b2EdgeShape.h:58
~b2ChainShape()
The destructor frees the vertices using b2Free.
void SetPrevVertex(const b2Vec2 &prevVertex)
T b2Max(T a, T b)
Definition: b2Math.h:643
b2Vec2 center
The position of the shape&#39;s centroid relative to the shape&#39;s origin.
Definition: b2Shape.h:33
float32 m_radius
Definition: b2Shape.h:93
void SetZero()
Set this vector to all zeros.
Definition: b2Math.h:62
A 2D column vector.
Definition: b2Math.h:53
void SetNextVertex(const b2Vec2 &nextVertex)
signed int int32
Definition: b2Settings.h:31
bool m_hasNextVertex
Definition: b2ChainShape.h:92
float32 mass
The mass of the shape, usually in kilograms.
Definition: b2Shape.h:30
void * Allocate(int32 size)
Allocate memory. This will use b2Alloc if the size is larger than b2_maxBlockSize.
float32 b2DistanceSquared(const b2Vec2 &a, const b2Vec2 &b)
Definition: b2Math.h:473
bool m_hasVertex0
Definition: b2EdgeShape.h:59
void ComputeMass(b2MassData *massData, float32 density) const
bool m_hasPrevVertex
Definition: b2ChainShape.h:92
void Clear()
Clear all data.
void ComputeAABB(b2AABB *aabb, const b2Transform &transform, int32 childIndex) const
void CreateLoop(const b2Vec2 *vertices, int32 count)
b2Vec2 m_vertex3
Definition: b2EdgeShape.h:58
void GetChildEdge(b2EdgeShape *edge, int32 index) const
Get a child edge.
An axis aligned bounding box.
Definition: b2Collision.h:162
#define b2Assert(A)
Definition: b2Settings.h:27
b2Vec2 m_vertex1
These are the edge vertices.
Definition: b2EdgeShape.h:55
Type m_type
Definition: b2Shape.h:92
b2Vec2 m_vertex2
Definition: b2EdgeShape.h:55
void * b2Alloc(int32 size)
Implement this function to use your own memory allocator.
Definition: b2Settings.cpp:27
T b2Min(T a, T b)
Definition: b2Math.h:632
int32 m_count
The vertex count.
Definition: b2ChainShape.h:89
b2Vec2 m_prevVertex
Definition: b2ChainShape.h:91
void CreateChain(const b2Vec2 *vertices, int32 count)
This holds the mass data computed for a shape.
Definition: b2Shape.h:27
void b2Free(void *mem)
If you implement b2Alloc, you should also implement this function.
Definition: b2Settings.cpp:32
int32 GetChildCount() const
float float32
Definition: b2Settings.h:35
b2Vec2 upperBound
the upper vertex
Definition: b2Collision.h:215
bool TestPoint(const b2Transform &transform, const b2Vec2 &p) const
bool m_hasVertex3
Definition: b2EdgeShape.h:59


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 19:36:39