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_fixture.h"
24 #include "box2d/b2_block_allocator.h"
25 #include "box2d/b2_broad_phase.h"
26 #include "box2d/b2_chain_shape.h"
27 #include "box2d/b2_circle_shape.h"
28 #include "box2d/b2_collision.h"
29 #include "box2d/b2_contact.h"
30 #include "box2d/b2_edge_shape.h"
31 #include "box2d/b2_polygon_shape.h"
32 #include "box2d/b2_world.h"
33
b2Fixture()34 b2Fixture::b2Fixture()
35 {
36 m_body = nullptr;
37 m_next = nullptr;
38 m_proxies = nullptr;
39 m_proxyCount = 0;
40 m_shape = nullptr;
41 m_density = 0.0f;
42 }
43
Create(b2BlockAllocator * allocator,b2Body * body,const b2FixtureDef * def)44 void b2Fixture::Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def)
45 {
46 m_userData = def->userData;
47 m_friction = def->friction;
48 m_restitution = def->restitution;
49 m_restitutionThreshold = def->restitutionThreshold;
50
51 m_body = body;
52 m_next = nullptr;
53
54 m_filter = def->filter;
55
56 m_isSensor = def->isSensor;
57
58 m_shape = def->shape->Clone(allocator);
59
60 // Reserve proxy space
61 int32 childCount = m_shape->GetChildCount();
62 m_proxies = (b2FixtureProxy*)allocator->Allocate(childCount * sizeof(b2FixtureProxy));
63 for (int32 i = 0; i < childCount; ++i)
64 {
65 m_proxies[i].fixture = nullptr;
66 m_proxies[i].proxyId = b2BroadPhase::e_nullProxy;
67 }
68 m_proxyCount = 0;
69
70 m_density = def->density;
71 }
72
Destroy(b2BlockAllocator * allocator)73 void b2Fixture::Destroy(b2BlockAllocator* allocator)
74 {
75 // The proxies must be destroyed before calling this.
76 b2Assert(m_proxyCount == 0);
77
78 // Free the proxy array.
79 int32 childCount = m_shape->GetChildCount();
80 allocator->Free(m_proxies, childCount * sizeof(b2FixtureProxy));
81 m_proxies = nullptr;
82
83 // Free the child shape.
84 switch (m_shape->m_type)
85 {
86 case b2Shape::e_circle:
87 {
88 b2CircleShape* s = (b2CircleShape*)m_shape;
89 s->~b2CircleShape();
90 allocator->Free(s, sizeof(b2CircleShape));
91 }
92 break;
93
94 case b2Shape::e_edge:
95 {
96 b2EdgeShape* s = (b2EdgeShape*)m_shape;
97 s->~b2EdgeShape();
98 allocator->Free(s, sizeof(b2EdgeShape));
99 }
100 break;
101
102 case b2Shape::e_polygon:
103 {
104 b2PolygonShape* s = (b2PolygonShape*)m_shape;
105 s->~b2PolygonShape();
106 allocator->Free(s, sizeof(b2PolygonShape));
107 }
108 break;
109
110 case b2Shape::e_chain:
111 {
112 b2ChainShape* s = (b2ChainShape*)m_shape;
113 s->~b2ChainShape();
114 allocator->Free(s, sizeof(b2ChainShape));
115 }
116 break;
117
118 default:
119 b2Assert(false);
120 break;
121 }
122
123 m_shape = nullptr;
124 }
125
CreateProxies(b2BroadPhase * broadPhase,const b2Transform & xf)126 void b2Fixture::CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf)
127 {
128 b2Assert(m_proxyCount == 0);
129
130 // Create proxies in the broad-phase.
131 m_proxyCount = m_shape->GetChildCount();
132
133 for (int32 i = 0; i < m_proxyCount; ++i)
134 {
135 b2FixtureProxy* proxy = m_proxies + i;
136 m_shape->ComputeAABB(&proxy->aabb, xf, i);
137 proxy->proxyId = broadPhase->CreateProxy(proxy->aabb, proxy);
138 proxy->fixture = this;
139 proxy->childIndex = i;
140 }
141 }
142
DestroyProxies(b2BroadPhase * broadPhase)143 void b2Fixture::DestroyProxies(b2BroadPhase* broadPhase)
144 {
145 // Destroy proxies in the broad-phase.
146 for (int32 i = 0; i < m_proxyCount; ++i)
147 {
148 b2FixtureProxy* proxy = m_proxies + i;
149 broadPhase->DestroyProxy(proxy->proxyId);
150 proxy->proxyId = b2BroadPhase::e_nullProxy;
151 }
152
153 m_proxyCount = 0;
154 }
155
Synchronize(b2BroadPhase * broadPhase,const b2Transform & transform1,const b2Transform & transform2)156 void b2Fixture::Synchronize(b2BroadPhase* broadPhase, const b2Transform& transform1, const b2Transform& transform2)
157 {
158 if (m_proxyCount == 0)
159 {
160 return;
161 }
162
163 for (int32 i = 0; i < m_proxyCount; ++i)
164 {
165 b2FixtureProxy* proxy = m_proxies + i;
166
167 // Compute an AABB that covers the swept shape (may miss some rotation effect).
168 b2AABB aabb1, aabb2;
169 m_shape->ComputeAABB(&aabb1, transform1, proxy->childIndex);
170 m_shape->ComputeAABB(&aabb2, transform2, proxy->childIndex);
171
172 proxy->aabb.Combine(aabb1, aabb2);
173
174 b2Vec2 displacement = aabb2.GetCenter() - aabb1.GetCenter();
175
176 broadPhase->MoveProxy(proxy->proxyId, proxy->aabb, displacement);
177 }
178 }
179
SetFilterData(const b2Filter & filter)180 void b2Fixture::SetFilterData(const b2Filter& filter)
181 {
182 m_filter = filter;
183
184 Refilter();
185 }
186
Refilter()187 void b2Fixture::Refilter()
188 {
189 if (m_body == nullptr)
190 {
191 return;
192 }
193
194 // Flag associated contacts for filtering.
195 b2ContactEdge* edge = m_body->GetContactList();
196 while (edge)
197 {
198 b2Contact* contact = edge->contact;
199 b2Fixture* fixtureA = contact->GetFixtureA();
200 b2Fixture* fixtureB = contact->GetFixtureB();
201 if (fixtureA == this || fixtureB == this)
202 {
203 contact->FlagForFiltering();
204 }
205
206 edge = edge->next;
207 }
208
209 b2World* world = m_body->GetWorld();
210
211 if (world == nullptr)
212 {
213 return;
214 }
215
216 // Touch each proxy so that new pairs may be created
217 b2BroadPhase* broadPhase = &world->m_contactManager.m_broadPhase;
218 for (int32 i = 0; i < m_proxyCount; ++i)
219 {
220 broadPhase->TouchProxy(m_proxies[i].proxyId);
221 }
222 }
223
SetSensor(bool sensor)224 void b2Fixture::SetSensor(bool sensor)
225 {
226 if (sensor != m_isSensor)
227 {
228 m_body->SetAwake(true);
229 m_isSensor = sensor;
230 }
231 }
232
Dump(int32 bodyIndex)233 void b2Fixture::Dump(int32 bodyIndex)
234 {
235 b2Dump(" b2FixtureDef fd;\n");
236 b2Dump(" fd.friction = %.9g;\n", m_friction);
237 b2Dump(" fd.restitution = %.9g;\n", m_restitution);
238 b2Dump(" fd.restitutionThreshold = %.9g;\n", m_restitutionThreshold);
239 b2Dump(" fd.density = %.9g;\n", m_density);
240 b2Dump(" fd.isSensor = bool(%d);\n", m_isSensor);
241 b2Dump(" fd.filter.categoryBits = uint16(%d);\n", m_filter.categoryBits);
242 b2Dump(" fd.filter.maskBits = uint16(%d);\n", m_filter.maskBits);
243 b2Dump(" fd.filter.groupIndex = int16(%d);\n", m_filter.groupIndex);
244
245 switch (m_shape->m_type)
246 {
247 case b2Shape::e_circle:
248 {
249 b2CircleShape* s = (b2CircleShape*)m_shape;
250 b2Dump(" b2CircleShape shape;\n");
251 b2Dump(" shape.m_radius = %.9g;\n", s->m_radius);
252 b2Dump(" shape.m_p.Set(%.9g, %.9g);\n", s->m_p.x, s->m_p.y);
253 }
254 break;
255
256 case b2Shape::e_edge:
257 {
258 b2EdgeShape* s = (b2EdgeShape*)m_shape;
259 b2Dump(" b2EdgeShape shape;\n");
260 b2Dump(" shape.m_radius = %.9g;\n", s->m_radius);
261 b2Dump(" shape.m_vertex0.Set(%.9g, %.9g);\n", s->m_vertex0.x, s->m_vertex0.y);
262 b2Dump(" shape.m_vertex1.Set(%.9g, %.9g);\n", s->m_vertex1.x, s->m_vertex1.y);
263 b2Dump(" shape.m_vertex2.Set(%.9g, %.9g);\n", s->m_vertex2.x, s->m_vertex2.y);
264 b2Dump(" shape.m_vertex3.Set(%.9g, %.9g);\n", s->m_vertex3.x, s->m_vertex3.y);
265 b2Dump(" shape.m_oneSided = bool(%d);\n", s->m_oneSided);
266 }
267 break;
268
269 case b2Shape::e_polygon:
270 {
271 b2PolygonShape* s = (b2PolygonShape*)m_shape;
272 b2Dump(" b2PolygonShape shape;\n");
273 b2Dump(" b2Vec2 vs[%d];\n", b2_maxPolygonVertices);
274 for (int32 i = 0; i < s->m_count; ++i)
275 {
276 b2Dump(" vs[%d].Set(%.9g, %.9g);\n", i, s->m_vertices[i].x, s->m_vertices[i].y);
277 }
278 b2Dump(" shape.Set(vs, %d);\n", s->m_count);
279 }
280 break;
281
282 case b2Shape::e_chain:
283 {
284 b2ChainShape* s = (b2ChainShape*)m_shape;
285 b2Dump(" b2ChainShape shape;\n");
286 b2Dump(" b2Vec2 vs[%d];\n", s->m_count);
287 for (int32 i = 0; i < s->m_count; ++i)
288 {
289 b2Dump(" vs[%d].Set(%.9g, %.9g);\n", i, s->m_vertices[i].x, s->m_vertices[i].y);
290 }
291 b2Dump(" shape.CreateChain(vs, %d);\n", s->m_count);
292 b2Dump(" shape.m_prevVertex.Set(%.9g, %.9g);\n", s->m_prevVertex.x, s->m_prevVertex.y);
293 b2Dump(" shape.m_nextVertex.Set(%.9g, %.9g);\n", s->m_nextVertex.x, s->m_nextVertex.y);
294 }
295 break;
296
297 default:
298 return;
299 }
300
301 b2Dump("\n");
302 b2Dump(" fd.shape = &shape;\n");
303 b2Dump("\n");
304 b2Dump(" bodies[%d]->CreateFixture(&fd);\n", bodyIndex);
305 }
306