b2_chain_shape.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "box2d/b2_chain_shape.h"
24 #include "box2d/b2_edge_shape.h"
25 
27 
28 #include <new>
29 #include <string.h>
30 
32 {
33  Clear();
34 }
35 
37 {
39  m_vertices = nullptr;
40  m_count = 0;
41 }
42 
43 void b2ChainShape::CreateLoop(const b2Vec2* vertices, int32 count)
44 {
45  b2Assert(m_vertices == nullptr && m_count == 0);
46  b2Assert(count >= 3);
47  if (count < 3)
48  {
49  return;
50  }
51 
52  for (int32 i = 1; i < count; ++i)
53  {
54  b2Vec2 v1 = vertices[i-1];
55  b2Vec2 v2 = vertices[i];
56  // If the code crashes here, it means your vertices are too close together.
58  }
59 
60  m_count = count + 1;
61  m_vertices = (b2Vec2*)b2Alloc(m_count * sizeof(b2Vec2));
62  memcpy(m_vertices, vertices, count * sizeof(b2Vec2));
63  m_vertices[count] = m_vertices[0];
66 }
67 
68 void b2ChainShape::CreateChain(const b2Vec2* vertices, int32 count, const b2Vec2& prevVertex, const b2Vec2& nextVertex)
69 {
70  b2Assert(m_vertices == nullptr && m_count == 0);
71  b2Assert(count >= 2);
72  for (int32 i = 1; i < count; ++i)
73  {
74  // If the code crashes here, it means your vertices are too close together.
75  b2Assert(b2DistanceSquared(vertices[i-1], vertices[i]) > b2_linearSlop * b2_linearSlop);
76  }
77 
78  m_count = count;
79  m_vertices = (b2Vec2*)b2Alloc(count * sizeof(b2Vec2));
80  memcpy(m_vertices, vertices, m_count * sizeof(b2Vec2));
81 
82  m_prevVertex = prevVertex;
83  m_nextVertex = nextVertex;
84 }
85 
87 {
88  void* mem = allocator->Allocate(sizeof(b2ChainShape));
89  b2ChainShape* clone = new (mem) b2ChainShape;
91  return clone;
92 }
93 
95 {
96  // edge count = vertex count - 1
97  return m_count - 1;
98 }
99 
101 {
102  b2Assert(0 <= index && index < m_count - 1);
103  edge->m_type = b2Shape::e_edge;
104  edge->m_radius = m_radius;
105 
106  edge->m_vertex1 = m_vertices[index + 0];
107  edge->m_vertex2 = m_vertices[index + 1];
108  edge->m_oneSided = true;
109 
110  if (index > 0)
111  {
112  edge->m_vertex0 = m_vertices[index - 1];
113  }
114  else
115  {
116  edge->m_vertex0 = m_prevVertex;
117  }
118 
119  if (index < m_count - 2)
120  {
121  edge->m_vertex3 = m_vertices[index + 2];
122  }
123  else
124  {
125  edge->m_vertex3 = m_nextVertex;
126  }
127 }
128 
129 bool b2ChainShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const
130 {
131  B2_NOT_USED(xf);
132  B2_NOT_USED(p);
133  return false;
134 }
135 
137  const b2Transform& xf, int32 childIndex) const
138 {
139  b2Assert(childIndex < m_count);
140 
141  b2EdgeShape edgeShape;
142 
143  int32 i1 = childIndex;
144  int32 i2 = childIndex + 1;
145  if (i2 == m_count)
146  {
147  i2 = 0;
148  }
149 
150  edgeShape.m_vertex1 = m_vertices[i1];
151  edgeShape.m_vertex2 = m_vertices[i2];
152 
153  return edgeShape.RayCast(output, input, xf, 0);
154 }
155 
156 void b2ChainShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 childIndex) const
157 {
158  b2Assert(childIndex < m_count);
159 
160  int32 i1 = childIndex;
161  int32 i2 = childIndex + 1;
162  if (i2 == m_count)
163  {
164  i2 = 0;
165  }
166 
167  b2Vec2 v1 = b2Mul(xf, m_vertices[i1]);
168  b2Vec2 v2 = b2Mul(xf, m_vertices[i2]);
169 
170  b2Vec2 lower = b2Min(v1, v2);
171  b2Vec2 upper = b2Max(v1, v2);
172 
174  aabb->lowerBound = lower - r;
175  aabb->upperBound = upper + r;
176 }
177 
178 void b2ChainShape::ComputeMass(b2MassData* massData, float density) const
179 {
180  B2_NOT_USED(density);
181 
182  massData->mass = 0.0f;
183  massData->center.SetZero();
184  massData->I = 0.0f;
185 }
float mass
The mass of the shape, usually in kilograms.
Definition: b2_shape.h:36
void * b2Alloc(int32 size)
Implement this function to use your own memory allocator.
Definition: b2_settings.h:100
b2Shape * Clone(b2BlockAllocator *allocator) const override
Implement b2Shape. Vertices are cloned using b2Alloc.
b2Vec2 m_nextVertex
T b2Min(T a, T b)
Definition: b2_math.h:626
b2Vec2 * m_vertices
The vertices. Owned by this class.
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2_math.h:422
b2Vec2 lowerBound
the lower vertex
Definition: b2_collision.h:220
void ComputeAABB(b2AABB *aabb, const b2Transform &transform, int32 childIndex) const override
bool TestPoint(const b2Transform &transform, const b2Vec2 &p) const override
Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
Definition: b2_collision.h:153
#define b2_linearSlop
Definition: b2_common.h:65
b2Vec2 m_vertex0
Optional adjacent vertices. These are used for smooth collision.
Definition: b2_edge_shape.h:69
~b2ChainShape()
The destructor frees the vertices using b2Free.
b2Vec2 center
The position of the shape&#39;s centroid relative to the shape&#39;s origin.
Definition: b2_shape.h:39
void SetZero()
Set this vector to all zeros.
Definition: b2_math.h:50
A 2D column vector.
Definition: b2_math.h:41
int32 GetChildCount() const override
signed int int32
Definition: b2_types.h:28
float b2DistanceSquared(const b2Vec2 &a, const b2Vec2 &b)
Definition: b2_math.h:467
bool RayCast(b2RayCastOutput *output, const b2RayCastInput &input, const b2Transform &transform, int32 childIndex) const override
Implement b2Shape.
float m_radius
Definition: b2_shape.h:102
bool RayCast(b2RayCastOutput *output, const b2RayCastInput &input, const b2Transform &transform, int32 childIndex) const override
Implement b2Shape.
void * Allocate(int32 size)
Allocate memory. This will use b2Alloc if the size is larger than b2_maxBlockSize.
#define B2_NOT_USED(x)
Definition: b2_common.h:36
void CreateChain(const b2Vec2 *vertices, int32 count, const b2Vec2 &prevVertex, const b2Vec2 &nextVertex)
void Clear()
Clear all data.
T b2Max(T a, T b)
Definition: b2_math.h:637
void CreateLoop(const b2Vec2 *vertices, int32 count)
b2Vec2 m_vertex3
Definition: b2_edge_shape.h:69
void GetChildEdge(b2EdgeShape *edge, int32 index) const
Get a child edge.
float I
The rotational inertia of the shape about the local origin.
Definition: b2_shape.h:42
An axis aligned bounding box.
Definition: b2_collision.h:168
void b2Free(void *mem)
If you implement b2Alloc, you should also implement this function.
Definition: b2_settings.h:106
b2Vec2 m_vertex1
These are the edge vertices.
Definition: b2_edge_shape.h:66
Type m_type
Definition: b2_shape.h:98
void ComputeMass(b2MassData *massData, float density) const override
b2Vec2 m_vertex2
Definition: b2_edge_shape.h:66
bool m_oneSided
Uses m_vertex0 and m_vertex3 to create smooth collision.
Definition: b2_edge_shape.h:72
int32 m_count
The vertex count.
b2Vec2 m_prevVertex
This holds the mass data computed for a shape.
Definition: b2_shape.h:33
#define b2Assert(A)
Definition: b2_common.h:37
b2Vec2 upperBound
the upper vertex
Definition: b2_collision.h:221


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19