LiquidFun
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
b2Fixture.h
1 /*
2 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3 * Copyright (c) 2013 Google, Inc.
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 * 2. Altered source versions must be plainly marked as such, and must not be
16 * misrepresented as being the original software.
17 * 3. This notice may not be removed or altered from any source distribution.
18 */
19 
20 #ifndef B2_FIXTURE_H
21 #define B2_FIXTURE_H
22 
23 #include <Box2D/Dynamics/b2Body.h>
25 #include <Box2D/Collision/Shapes/b2Shape.h>
26 
27 class b2BlockAllocator;
28 class b2Body;
29 class b2BroadPhase;
30 class b2Fixture;
31 
33 struct b2Filter
34 {
35  b2Filter()
36  {
37  categoryBits = 0x0001;
38  maskBits = 0xFFFF;
39  groupIndex = 0;
40  }
41 
43  uint16 categoryBits;
44 
47  uint16 maskBits;
48 
52  int16 groupIndex;
53 };
54 
58 {
61  {
62  shape = NULL;
63  userData = NULL;
64  friction = 0.2f;
65  restitution = 0.0f;
66  density = 0.0f;
67  isSensor = false;
68  }
69 
72  const b2Shape* shape;
73 
75  void* userData;
76 
78  float32 friction;
79 
81  float32 restitution;
82 
84  float32 density;
85 
88  bool isSensor;
89 
92 };
93 
96 {
97  b2AABB aabb;
98  b2Fixture* fixture;
99  int32 childIndex;
100  int32 proxyId;
101 };
102 
109 {
110 public:
113  b2Shape::Type GetType() const;
114 
118  b2Shape* GetShape();
119  const b2Shape* GetShape() const;
120 
122  void SetSensor(bool sensor);
123 
126  bool IsSensor() const;
127 
131  void SetFilterData(const b2Filter& filter);
132 
134  const b2Filter& GetFilterData() const;
135 
137  void Refilter();
138 
141  b2Body* GetBody();
142  const b2Body* GetBody() const;
143 
146  b2Fixture* GetNext();
147  const b2Fixture* GetNext() const;
148 
151  void* GetUserData() const;
152 
154  void SetUserData(void* data);
155 
158  bool TestPoint(const b2Vec2& p) const;
159 
162  void ComputeDistance(const b2Vec2& p, float32* distance, b2Vec2* normal, int32 childIndex) const;
163 
167  bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const;
168 
172  void GetMassData(b2MassData* massData) const;
173 
176  void SetDensity(float32 density);
177 
179  float32 GetDensity() const;
180 
182  float32 GetFriction() const;
183 
186  void SetFriction(float32 friction);
187 
189  float32 GetRestitution() const;
190 
193  void SetRestitution(float32 restitution);
194 
198  const b2AABB& GetAABB(int32 childIndex) const;
199 
201  void Dump(int32 bodyIndex);
202 
203 protected:
204 
205  friend class b2Body;
206  friend class b2World;
207  friend class b2Contact;
208  friend class b2ContactManager;
209 
210  b2Fixture();
211 
212  // We need separation create/destroy functions from the constructor/destructor because
213  // the destructor cannot access the allocator (no destructor arguments allowed by C++).
214  void Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def);
215  void Destroy(b2BlockAllocator* allocator);
216 
217  // These support body activation/deactivation.
218  void CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf);
219  void DestroyProxies(b2BroadPhase* broadPhase);
220 
221  void Synchronize(b2BroadPhase* broadPhase, const b2Transform& xf1, const b2Transform& xf2);
222 
223  float32 m_density;
224 
225  b2Fixture* m_next;
226  b2Body* m_body;
227 
228  b2Shape* m_shape;
229 
230  float32 m_friction;
231  float32 m_restitution;
232 
233  b2FixtureProxy* m_proxies;
234  int32 m_proxyCount;
235 
236  b2Filter m_filter;
237 
238  bool m_isSensor;
239 
240  void* m_userData;
241 };
242 
243 inline b2Shape::Type b2Fixture::GetType() const
244 {
245  return m_shape->GetType();
246 }
247 
249 {
250  return m_shape;
251 }
252 
253 inline const b2Shape* b2Fixture::GetShape() const
254 {
255  return m_shape;
256 }
257 
258 inline bool b2Fixture::IsSensor() const
259 {
260  return m_isSensor;
261 }
262 
263 inline const b2Filter& b2Fixture::GetFilterData() const
264 {
265  return m_filter;
266 }
267 
268 inline void* b2Fixture::GetUserData() const
269 {
270  return m_userData;
271 }
272 
273 inline void b2Fixture::SetUserData(void* data)
274 {
275  m_userData = data;
276 }
277 
279 {
280  return m_body;
281 }
282 
283 inline const b2Body* b2Fixture::GetBody() const
284 {
285  return m_body;
286 }
287 
289 {
290  return m_next;
291 }
292 
293 inline const b2Fixture* b2Fixture::GetNext() const
294 {
295  return m_next;
296 }
297 
298 inline void b2Fixture::SetDensity(float32 density)
299 {
300  b2Assert(b2IsValid(density) && density >= 0.0f);
301  m_density = density;
302 }
303 
304 inline float32 b2Fixture::GetDensity() const
305 {
306  return m_density;
307 }
308 
309 inline float32 b2Fixture::GetFriction() const
310 {
311  return m_friction;
312 }
313 
314 inline void b2Fixture::SetFriction(float32 friction)
315 {
316  m_friction = friction;
317 }
318 
319 inline float32 b2Fixture::GetRestitution() const
320 {
321  return m_restitution;
322 }
323 
324 inline void b2Fixture::SetRestitution(float32 restitution)
325 {
326  m_restitution = restitution;
327 }
328 
329 inline bool b2Fixture::TestPoint(const b2Vec2& p) const
330 {
331  return m_shape->TestPoint(m_body->GetTransform(), p);
332 }
333 
334 inline void b2Fixture::ComputeDistance(const b2Vec2& p, float32* d, b2Vec2* n, int32 childIndex) const
335 {
336  m_shape->ComputeDistance(m_body->GetTransform(), p, d, n, childIndex);
337 }
338 
339 inline bool b2Fixture::RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const
340 {
341  return m_shape->RayCast(output, input, m_body->GetTransform(), childIndex);
342 }
343 
344 inline void b2Fixture::GetMassData(b2MassData* massData) const
345 {
346  m_shape->ComputeMass(massData, m_density);
347 }
348 
349 inline const b2AABB& b2Fixture::GetAABB(int32 childIndex) const
350 {
351  b2Assert(0 <= childIndex && childIndex < m_proxyCount);
352  return m_proxies[childIndex].aabb;
353 }
354 
355 #endif
Definition: b2Math.h:411
const b2AABB & GetAABB(int32 childIndex) const
Definition: b2Fixture.h:349
Type GetType() const
Definition: b2Shape.h:104
b2Filter filter
Contact filtering data.
Definition: b2Fixture.h:91
b2Fixture * GetNext()
Definition: b2Fixture.h:288
void SetRestitution(float32 restitution)
Definition: b2Fixture.h:324
Definition: b2BroadPhase.h:36
bool RayCast(b2RayCastOutput *output, const b2RayCastInput &input, int32 childIndex) const
Definition: b2Fixture.h:339
void * userData
Use this to store application specific fixture data.
Definition: b2Fixture.h:75
bool isSensor
Definition: b2Fixture.h:88
void GetMassData(b2MassData *massData) const
Definition: b2Fixture.h:344
Definition: b2World.h:44
This proxy is used internally to connect fixtures to the broad-phase.
Definition: b2Fixture.h:95
void SetFriction(float32 friction)
Definition: b2Fixture.h:314
uint16 categoryBits
The collision category bits. Normally you would just set one bit.
Definition: b2Fixture.h:43
float32 restitution
The restitution (elasticity) usually in the range [0,1].
Definition: b2Fixture.h:81
float32 GetFriction() const
Get the coefficient of friction.
Definition: b2Fixture.h:309
Definition: b2BlockAllocator.h:36
This holds the mass data computed for a shape.
Definition: b2Shape.h:28
Definition: b2Shape.h:43
void ComputeDistance(const b2Vec2 &p, float32 *distance, b2Vec2 *normal, int32 childIndex) const
Definition: b2Fixture.h:334
float32 GetDensity() const
Get the density of this fixture.
Definition: b2Fixture.h:304
b2Shape::Type GetType() const
Definition: b2Fixture.h:243
Definition: b2Collision.h:155
bool IsSensor() const
Definition: b2Fixture.h:258
b2Body * GetBody()
Definition: b2Fixture.h:278
b2Shape * GetShape()
Definition: b2Fixture.h:248
Definition: b2Fixture.h:57
int16 groupIndex
Definition: b2Fixture.h:52
An axis aligned bounding box.
Definition: b2Collision.h:162
const b2Filter & GetFilterData() const
Get the contact filtering data.
Definition: b2Fixture.h:263
void SetFilterData(const b2Filter &filter)
Definition: b2Fixture.cpp:176
Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
Definition: b2Collision.h:147
void SetSensor(bool sensor)
Set if this fixture is a sensor.
Definition: b2Fixture.cpp:220
void Refilter()
Call this if you want to establish collision that was previously disabled by b2ContactFilter::ShouldC...
Definition: b2Fixture.cpp:183
uint16 maskBits
Definition: b2Fixture.h:47
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:132
float32 GetRestitution() const
Get the coefficient of restitution.
Definition: b2Fixture.h:319
b2FixtureDef()
The constructor sets the default fixture definition values.
Definition: b2Fixture.h:60
void SetDensity(float32 density)
Definition: b2Fixture.h:298
const b2Shape * shape
Definition: b2Fixture.h:72
const b2Transform & GetTransform() const
Definition: b2Body.h:496
virtual bool RayCast(b2RayCastOutput *output, const b2RayCastInput &input, const b2Transform &transform, int32 childIndex) const =0
virtual void ComputeMass(b2MassData *massData, float32 density) const =0
A 2D column vector.
Definition: b2Math.h:56
Definition: b2Contact.h:77
void * GetUserData() const
Definition: b2Fixture.h:268
void SetUserData(void *data)
Set the user data. Use this to store your application specific data.
Definition: b2Fixture.h:273
This holds contact filtering data.
Definition: b2Fixture.h:33
Definition: b2ContactManager.h:31
bool TestPoint(const b2Vec2 &p) const
Definition: b2Fixture.h:329
virtual void ComputeDistance(const b2Transform &xf, const b2Vec2 &p, float32 *distance, b2Vec2 *normal, int32 childIndex) const =0
float32 density
The density, usually in kg/m^2.
Definition: b2Fixture.h:84
float32 friction
The friction coefficient, usually in the range [0,1].
Definition: b2Fixture.h:78
void Dump(int32 bodyIndex)
Dump this fixture to the log file.
Definition: b2Fixture.cpp:229
virtual bool TestPoint(const b2Transform &xf, const b2Vec2 &p) const =0
Definition: b2Fixture.h:108