b2Fixture.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2006-2009 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 <Box2D/Dynamics/b2World.h>
29 
31 {
32  m_userData = NULL;
33  m_body = NULL;
34  m_next = NULL;
35  m_proxies = NULL;
36  m_proxyCount = 0;
37  m_shape = NULL;
38  m_density = 0.0f;
39 }
40 
41 void b2Fixture::Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def)
42 {
43  m_userData = def->userData;
44  m_friction = def->friction;
46 
47  m_body = body;
48  m_next = NULL;
49 
50  m_filter = def->filter;
51 
52  m_isSensor = def->isSensor;
53 
54  m_shape = def->shape->Clone(allocator);
55 
56  // Reserve proxy space
57  int32 childCount = m_shape->GetChildCount();
58  m_proxies = (b2FixtureProxy*)allocator->Allocate(childCount * sizeof(b2FixtureProxy));
59  for (int32 i = 0; i < childCount; ++i)
60  {
61  m_proxies[i].fixture = NULL;
63  }
64  m_proxyCount = 0;
65 
66  m_density = def->density;
67 }
68 
70 {
71  // The proxies must be destroyed before calling this.
72  b2Assert(m_proxyCount == 0);
73 
74  // Free the proxy array.
75  int32 childCount = m_shape->GetChildCount();
76  allocator->Free(m_proxies, childCount * sizeof(b2FixtureProxy));
77  m_proxies = NULL;
78 
79  // Free the child shape.
80  switch (m_shape->m_type)
81  {
82  case b2Shape::e_circle:
83  {
85  s->~b2CircleShape();
86  allocator->Free(s, sizeof(b2CircleShape));
87  }
88  break;
89 
90  case b2Shape::e_edge:
91  {
93  s->~b2EdgeShape();
94  allocator->Free(s, sizeof(b2EdgeShape));
95  }
96  break;
97 
98  case b2Shape::e_polygon:
99  {
101  s->~b2PolygonShape();
102  allocator->Free(s, sizeof(b2PolygonShape));
103  }
104  break;
105 
106  case b2Shape::e_chain:
107  {
109  s->~b2ChainShape();
110  allocator->Free(s, sizeof(b2ChainShape));
111  }
112  break;
113 
114  default:
115  b2Assert(false);
116  break;
117  }
118 
119  m_shape = NULL;
120 }
121 
123 {
124  b2Assert(m_proxyCount == 0);
125 
126  // Create proxies in the broad-phase.
128 
129  for (int32 i = 0; i < m_proxyCount; ++i)
130  {
131  b2FixtureProxy* proxy = m_proxies + i;
132  m_shape->ComputeAABB(&proxy->aabb, xf, i);
133  proxy->proxyId = broadPhase->CreateProxy(proxy->aabb, proxy);
134  proxy->fixture = this;
135  proxy->childIndex = i;
136  }
137 }
138 
140 {
141  // Destroy proxies in the broad-phase.
142  for (int32 i = 0; i < m_proxyCount; ++i)
143  {
144  b2FixtureProxy* proxy = m_proxies + i;
145  broadPhase->DestroyProxy(proxy->proxyId);
147  }
148 
149  m_proxyCount = 0;
150 }
151 
152 void b2Fixture::Synchronize(b2BroadPhase* broadPhase, const b2Transform& transform1, const b2Transform& transform2)
153 {
154  if (m_proxyCount == 0)
155  {
156  return;
157  }
158 
159  for (int32 i = 0; i < m_proxyCount; ++i)
160  {
161  b2FixtureProxy* proxy = m_proxies + i;
162 
163  // Compute an AABB that covers the swept shape (may miss some rotation effect).
164  b2AABB aabb1, aabb2;
165  m_shape->ComputeAABB(&aabb1, transform1, proxy->childIndex);
166  m_shape->ComputeAABB(&aabb2, transform2, proxy->childIndex);
167 
168  proxy->aabb.Combine(aabb1, aabb2);
169 
170  b2Vec2 displacement = transform2.p - transform1.p;
171 
172  broadPhase->MoveProxy(proxy->proxyId, proxy->aabb, displacement);
173  }
174 }
175 
177 {
178  m_filter = filter;
179 
180  Refilter();
181 }
182 
184 {
185  if (m_body == NULL)
186  {
187  return;
188  }
189 
190  // Flag associated contacts for filtering.
192  while (edge)
193  {
194  b2Contact* contact = edge->contact;
195  b2Fixture* fixtureA = contact->GetFixtureA();
196  b2Fixture* fixtureB = contact->GetFixtureB();
197  if (fixtureA == this || fixtureB == this)
198  {
199  contact->FlagForFiltering();
200  }
201 
202  edge = edge->next;
203  }
204 
205  b2World* world = m_body->GetWorld();
206 
207  if (world == NULL)
208  {
209  return;
210  }
211 
212  // Touch each proxy so that new pairs may be created
213  b2BroadPhase* broadPhase = &world->m_contactManager.m_broadPhase;
214  for (int32 i = 0; i < m_proxyCount; ++i)
215  {
216  broadPhase->TouchProxy(m_proxies[i].proxyId);
217  }
218 }
219 
220 void b2Fixture::SetSensor(bool sensor)
221 {
222  if (sensor != m_isSensor)
223  {
224  m_body->SetAwake(true);
225  m_isSensor = sensor;
226  }
227 }
228 
229 void b2Fixture::Dump(int32 bodyIndex)
230 {
231  b2Log(" b2FixtureDef fd;\n");
232  b2Log(" fd.friction = %.15lef;\n", m_friction);
233  b2Log(" fd.restitution = %.15lef;\n", m_restitution);
234  b2Log(" fd.density = %.15lef;\n", m_density);
235  b2Log(" fd.isSensor = bool(%d);\n", m_isSensor);
236  b2Log(" fd.filter.categoryBits = uint16(%d);\n", m_filter.categoryBits);
237  b2Log(" fd.filter.maskBits = uint16(%d);\n", m_filter.maskBits);
238  b2Log(" fd.filter.groupIndex = int16(%d);\n", m_filter.groupIndex);
239 
240  switch (m_shape->m_type)
241  {
242  case b2Shape::e_circle:
243  {
245  b2Log(" b2CircleShape shape;\n");
246  b2Log(" shape.m_radius = %.15lef;\n", s->m_radius);
247  b2Log(" shape.m_p.Set(%.15lef, %.15lef);\n", s->m_p.x, s->m_p.y);
248  }
249  break;
250 
251  case b2Shape::e_edge:
252  {
254  b2Log(" b2EdgeShape shape;\n");
255  b2Log(" shape.m_radius = %.15lef;\n", s->m_radius);
256  b2Log(" shape.m_vertex0.Set(%.15lef, %.15lef);\n", s->m_vertex0.x, s->m_vertex0.y);
257  b2Log(" shape.m_vertex1.Set(%.15lef, %.15lef);\n", s->m_vertex1.x, s->m_vertex1.y);
258  b2Log(" shape.m_vertex2.Set(%.15lef, %.15lef);\n", s->m_vertex2.x, s->m_vertex2.y);
259  b2Log(" shape.m_vertex3.Set(%.15lef, %.15lef);\n", s->m_vertex3.x, s->m_vertex3.y);
260  b2Log(" shape.m_hasVertex0 = bool(%d);\n", s->m_hasVertex0);
261  b2Log(" shape.m_hasVertex3 = bool(%d);\n", s->m_hasVertex3);
262  }
263  break;
264 
265  case b2Shape::e_polygon:
266  {
268  b2Log(" b2PolygonShape shape;\n");
269  b2Log(" b2Vec2 vs[%d];\n", b2_maxPolygonVertices);
270  for (int32 i = 0; i < s->m_count; ++i)
271  {
272  b2Log(" vs[%d].Set(%.15lef, %.15lef);\n", i, s->m_vertices[i].x, s->m_vertices[i].y);
273  }
274  b2Log(" shape.Set(vs, %d);\n", s->m_count);
275  }
276  break;
277 
278  case b2Shape::e_chain:
279  {
281  b2Log(" b2ChainShape shape;\n");
282  b2Log(" b2Vec2 vs[%d];\n", s->m_count);
283  for (int32 i = 0; i < s->m_count; ++i)
284  {
285  b2Log(" vs[%d].Set(%.15lef, %.15lef);\n", i, s->m_vertices[i].x, s->m_vertices[i].y);
286  }
287  b2Log(" shape.CreateChain(vs, %d);\n", s->m_count);
288  b2Log(" shape.m_prevVertex.Set(%.15lef, %.15lef);\n", s->m_prevVertex.x, s->m_prevVertex.y);
289  b2Log(" shape.m_nextVertex.Set(%.15lef, %.15lef);\n", s->m_nextVertex.x, s->m_nextVertex.y);
290  b2Log(" shape.m_hasPrevVertex = bool(%d);\n", s->m_hasPrevVertex);
291  b2Log(" shape.m_hasNextVertex = bool(%d);\n", s->m_hasNextVertex);
292  }
293  break;
294 
295  default:
296  return;
297  }
298 
299  b2Log("\n");
300  b2Log(" fd.shape = &shape;\n");
301  b2Log("\n");
302  b2Log(" bodies[%d]->CreateFixture(&fd);\n", bodyIndex);
303 }
const b2Shape * shape
Definition: b2Fixture.h:71
float32 m_friction
Definition: b2Fixture.h:225
b2Vec2 m_nextVertex
Definition: b2ChainShape.h:91
void Combine(const b2AABB &aabb)
Combine an AABB into this one.
Definition: b2Collision.h:188
b2Vec2 * m_vertices
The vertices. Owned by this class.
Definition: b2ChainShape.h:86
void b2Log(const char *string,...)
Logging function.
Definition: b2Settings.cpp:38
void Create(b2BlockAllocator *allocator, b2Body *body, const b2FixtureDef *def)
Definition: b2Fixture.cpp:41
b2Vec2 p
Definition: b2Math.h:372
b2ContactManager m_contactManager
Definition: b2World.h:238
virtual b2Shape * Clone(b2BlockAllocator *allocator) const =0
Clone the concrete shape using the provided allocator.
void Dump(int32 bodyIndex)
Dump this fixture to the log file.
Definition: b2Fixture.cpp:229
b2Fixture * fixture
Definition: b2Fixture.h:97
b2Filter m_filter
Definition: b2Fixture.h:231
b2Fixture * GetFixtureB()
Get fixture B in this contact.
Definition: b2Contact.h:284
void Refilter()
Call this if you want to establish collision that was previously disabled by b2ContactFilter::ShouldC...
Definition: b2Fixture.cpp:183
XmlRpcServer s
b2Fixture * m_next
Definition: b2Fixture.h:220
b2ContactEdge * GetContactList()
Definition: b2Body.h:707
void Synchronize(b2BroadPhase *broadPhase, const b2Transform &xf1, const b2Transform &xf2)
Definition: b2Fixture.cpp:152
b2Vec2 m_vertex0
Optional adjacent vertices. These are used for smooth collision.
Definition: b2EdgeShape.h:58
void Free(void *p, int32 size)
Free memory. This will use b2Free if the size is larger than b2_maxBlockSize.
int32 m_proxyCount
Definition: b2Fixture.h:229
void SetFilterData(const b2Filter &filter)
Definition: b2Fixture.cpp:176
~b2ChainShape()
The destructor frees the vertices using b2Free.
float32 m_restitution
Definition: b2Fixture.h:226
This proxy is used internally to connect fixtures to the broad-phase.
Definition: b2Fixture.h:94
float32 m_radius
Definition: b2Shape.h:93
b2ContactEdge * next
the next contact edge in the body&#39;s contact list
Definition: b2Contact.h:71
void FlagForFiltering()
Flag this contact for filtering. Filtering will occur the next time step.
Definition: b2Contact.h:304
A circle shape.
Definition: b2CircleShape.h:25
A 2D column vector.
Definition: b2Math.h:53
b2Contact * contact
the contact
Definition: b2Contact.h:69
b2Vec2 m_p
Position.
Definition: b2CircleShape.h:62
signed int int32
Definition: b2Settings.h:31
bool m_hasNextVertex
Definition: b2ChainShape.h:92
virtual int32 GetChildCount() const =0
Get the number of child primitives.
void * m_userData
Definition: b2Fixture.h:235
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
void * Allocate(int32 size)
Allocate memory. This will use b2Alloc if the size is larger than b2_maxBlockSize.
bool m_hasVertex0
Definition: b2EdgeShape.h:59
int32 CreateProxy(const b2AABB &aabb, void *userData)
float32 m_density
Definition: b2Fixture.h:218
b2Filter filter
Contact filtering data.
Definition: b2Fixture.h:90
This holds contact filtering data.
Definition: b2Fixture.h:32
bool m_hasPrevVertex
Definition: b2ChainShape.h:92
b2Vec2 m_vertices[b2_maxPolygonVertices]
void DestroyProxy(int32 proxyId)
Destroy a proxy. It is up to the client to remove any pairs.
void Destroy(b2BlockAllocator *allocator)
Definition: b2Fixture.cpp:69
#define b2_maxPolygonVertices
Definition: b2Settings.h:54
b2Vec2 m_vertex3
Definition: b2EdgeShape.h:58
void SetSensor(bool sensor)
Set if this fixture is a sensor.
Definition: b2Fixture.cpp:220
bool isSensor
Definition: b2Fixture.h:87
float32 density
The density, usually in kg/m^2.
Definition: b2Fixture.h:83
b2Fixture * GetFixtureA()
Get fixture A in this contact.
Definition: b2Contact.h:274
float32 y
Definition: b2Math.h:140
void * userData
Use this to store application specific fixture data.
Definition: b2Fixture.h:74
b2BroadPhase m_broadPhase
An axis aligned bounding box.
Definition: b2Collision.h:162
#define b2Assert(A)
Definition: b2Settings.h:27
int32 childIndex
Definition: b2Fixture.h:98
b2Body * m_body
Definition: b2Fixture.h:221
void TouchProxy(int32 proxyId)
Call to trigger a re-processing of it&#39;s pairs on the next call to UpdatePairs.
b2Vec2 m_vertex1
These are the edge vertices.
Definition: b2EdgeShape.h:55
Type m_type
Definition: b2Shape.h:92
void DestroyProxies(b2BroadPhase *broadPhase)
Definition: b2Fixture.cpp:139
float32 restitution
The restitution (elasticity) usually in the range [0,1].
Definition: b2Fixture.h:80
b2Vec2 m_vertex2
Definition: b2EdgeShape.h:55
void CreateProxies(b2BroadPhase *broadPhase, const b2Transform &xf)
Definition: b2Fixture.cpp:122
b2FixtureProxy * m_proxies
Definition: b2Fixture.h:228
b2Shape * m_shape
Definition: b2Fixture.h:223
int16 groupIndex
Definition: b2Fixture.h:51
int32 m_count
The vertex count.
Definition: b2ChainShape.h:89
bool m_isSensor
Definition: b2Fixture.h:233
void MoveProxy(int32 proxyId, const b2AABB &aabb, const b2Vec2 &displacement)
virtual void ComputeAABB(b2AABB *aabb, const b2Transform &xf, int32 childIndex) const =0
float32 x
Definition: b2Math.h:140
uint16 categoryBits
The collision category bits. Normally you would just set one bit.
Definition: b2Fixture.h:42
b2Vec2 m_prevVertex
Definition: b2ChainShape.h:91
uint16 maskBits
Definition: b2Fixture.h:46
b2AABB aabb
Definition: b2Fixture.h:96
float32 friction
The friction coefficient, usually in the range [0,1].
Definition: b2Fixture.h:77
void SetAwake(bool flag)
Definition: b2Body.h:633
int32 proxyId
Definition: b2Fixture.h:99
b2World * GetWorld()
Get the parent world of this body.
Definition: b2Body.h:850
bool m_hasVertex3
Definition: b2EdgeShape.h:59


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