Box2D  2.4.0
A 2D physics engine for games
b2_fixture.h
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 #ifndef B2_FIXTURE_H
24 #define B2_FIXTURE_H
25 
26 #include "b2_api.h"
27 #include "b2_body.h"
28 #include "b2_collision.h"
29 #include "b2_shape.h"
30 
31 class b2BlockAllocator;
32 class b2Body;
33 class b2BroadPhase;
34 class b2Fixture;
35 
37 struct B2_API b2Filter
38 {
39  b2Filter()
40  {
41  categoryBits = 0x0001;
42  maskBits = 0xFFFF;
43  groupIndex = 0;
44  }
45 
47  uint16 categoryBits;
48 
51  uint16 maskBits;
52 
56  int16 groupIndex;
57 };
58 
61 struct B2_API b2FixtureDef
62 {
65  {
66  shape = nullptr;
67  userData = nullptr;
68  friction = 0.2f;
69  restitution = 0.0f;
70  density = 0.0f;
71  isSensor = false;
72  }
73 
76  const b2Shape* shape;
77 
79  void* userData;
80 
82  float friction;
83 
85  float restitution;
86 
88  float density;
89 
92  bool isSensor;
93 
96 };
97 
99 struct B2_API b2FixtureProxy
100 {
101  b2AABB aabb;
102  b2Fixture* fixture;
103  int32 childIndex;
104  int32 proxyId;
105 };
106 
112 class B2_API b2Fixture
113 {
114 public:
117  b2Shape::Type GetType() const;
118 
122  b2Shape* GetShape();
123  const b2Shape* GetShape() const;
124 
126  void SetSensor(bool sensor);
127 
130  bool IsSensor() const;
131 
135  void SetFilterData(const b2Filter& filter);
136 
138  const b2Filter& GetFilterData() const;
139 
141  void Refilter();
142 
145  b2Body* GetBody();
146  const b2Body* GetBody() const;
147 
150  b2Fixture* GetNext();
151  const b2Fixture* GetNext() const;
152 
155  void* GetUserData() const;
156 
158  void SetUserData(void* data);
159 
162  bool TestPoint(const b2Vec2& p) const;
163 
168  bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const;
169 
173  void GetMassData(b2MassData* massData) const;
174 
177  void SetDensity(float density);
178 
180  float GetDensity() const;
181 
183  float GetFriction() const;
184 
187  void SetFriction(float friction);
188 
190  float GetRestitution() const;
191 
194  void SetRestitution(float restitution);
195 
199  const b2AABB& GetAABB(int32 childIndex) const;
200 
202  void Dump(int32 bodyIndex);
203 
204 protected:
205 
206  friend class b2Body;
207  friend class b2World;
208  friend class b2Contact;
209  friend class b2ContactManager;
210 
211  b2Fixture();
212 
213  // We need separation create/destroy functions from the constructor/destructor because
214  // the destructor cannot access the allocator (no destructor arguments allowed by C++).
215  void Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def);
216  void Destroy(b2BlockAllocator* allocator);
217 
218  // These support body activation/deactivation.
219  void CreateProxies(b2BroadPhase* broadPhase, const b2Transform& xf);
220  void DestroyProxies(b2BroadPhase* broadPhase);
221 
222  void Synchronize(b2BroadPhase* broadPhase, const b2Transform& xf1, const b2Transform& xf2);
223 
224  float m_density;
225 
226  b2Fixture* m_next;
227  b2Body* m_body;
228 
229  b2Shape* m_shape;
230 
231  float m_friction;
232  float m_restitution;
233 
234  b2FixtureProxy* m_proxies;
235  int32 m_proxyCount;
236 
237  b2Filter m_filter;
238 
239  bool m_isSensor;
240 
241  void* m_userData;
242 };
243 
244 inline b2Shape::Type b2Fixture::GetType() const
245 {
246  return m_shape->GetType();
247 }
248 
250 {
251  return m_shape;
252 }
253 
254 inline const b2Shape* b2Fixture::GetShape() const
255 {
256  return m_shape;
257 }
258 
259 inline bool b2Fixture::IsSensor() const
260 {
261  return m_isSensor;
262 }
263 
264 inline const b2Filter& b2Fixture::GetFilterData() const
265 {
266  return m_filter;
267 }
268 
269 inline void* b2Fixture::GetUserData() const
270 {
271  return m_userData;
272 }
273 
274 inline void b2Fixture::SetUserData(void* data)
275 {
276  m_userData = data;
277 }
278 
280 {
281  return m_body;
282 }
283 
284 inline const b2Body* b2Fixture::GetBody() const
285 {
286  return m_body;
287 }
288 
290 {
291  return m_next;
292 }
293 
294 inline const b2Fixture* b2Fixture::GetNext() const
295 {
296  return m_next;
297 }
298 
299 inline void b2Fixture::SetDensity(float density)
300 {
301  b2Assert(b2IsValid(density) && density >= 0.0f);
302  m_density = density;
303 }
304 
305 inline float b2Fixture::GetDensity() const
306 {
307  return m_density;
308 }
309 
310 inline float b2Fixture::GetFriction() const
311 {
312  return m_friction;
313 }
314 
315 inline void b2Fixture::SetFriction(float friction)
316 {
317  m_friction = friction;
318 }
319 
320 inline float b2Fixture::GetRestitution() const
321 {
322  return m_restitution;
323 }
324 
325 inline void b2Fixture::SetRestitution(float restitution)
326 {
327  m_restitution = restitution;
328 }
329 
330 inline bool b2Fixture::TestPoint(const b2Vec2& p) const
331 {
332  return m_shape->TestPoint(m_body->GetTransform(), p);
333 }
334 
335 inline bool b2Fixture::RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const
336 {
337  return m_shape->RayCast(output, input, m_body->GetTransform(), childIndex);
338 }
339 
340 inline void b2Fixture::GetMassData(b2MassData* massData) const
341 {
342  m_shape->ComputeMass(massData, m_density);
343 }
344 
345 inline const b2AABB& b2Fixture::GetAABB(int32 childIndex) const
346 {
347  b2Assert(0 <= childIndex && childIndex < m_proxyCount);
348  return m_proxies[childIndex].aabb;
349 }
350 
351 #endif
b2Fixture::GetRestitution
float GetRestitution() const
Get the coefficient of restitution.
Definition: b2_fixture.h:320
b2Vec2
A 2D column vector.
Definition: b2_math.h:42
b2FixtureDef::restitution
float restitution
The restitution (elasticity) usually in the range [0,1].
Definition: b2_fixture.h:85
b2Fixture::GetDensity
float GetDensity() const
Get the density of this fixture.
Definition: b2_fixture.h:305
b2Shape::RayCast
virtual bool RayCast(b2RayCastOutput *output, const b2RayCastInput &input, const b2Transform &transform, int32 childIndex) const =0
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:133
b2ContactManager
Definition: b2_contact_manager.h:36
b2Fixture::GetType
b2Shape::Type GetType() const
Definition: b2_fixture.h:244
b2Fixture::SetUserData
void SetUserData(void *data)
Set the user data. Use this to store your application specific data.
Definition: b2_fixture.h:274
b2Fixture::GetBody
b2Body * GetBody()
Definition: b2_fixture.h:279
b2Body::GetTransform
const b2Transform & GetTransform() const
Definition: b2_body.h:483
b2RayCastInput
Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
Definition: b2_collision.h:154
b2Fixture::Refilter
void Refilter()
Call this if you want to establish collision that was previously disabled by b2ContactFilter::ShouldC...
b2Fixture::GetFilterData
const b2Filter & GetFilterData() const
Get the contact filtering data.
Definition: b2_fixture.h:264
b2Transform
Definition: b2_math.h:339
b2FixtureDef::userData
void * userData
Use this to store application specific fixture data.
Definition: b2_fixture.h:79
b2Fixture::SetSensor
void SetSensor(bool sensor)
Set if this fixture is a sensor.
b2FixtureDef::filter
b2Filter filter
Contact filtering data.
Definition: b2_fixture.h:95
b2FixtureDef::friction
float friction
The friction coefficient, usually in the range [0,1].
Definition: b2_fixture.h:82
b2Fixture::RayCast
bool RayCast(b2RayCastOutput *output, const b2RayCastInput &input, int32 childIndex) const
Definition: b2_fixture.h:335
b2Filter::categoryBits
uint16 categoryBits
The collision category bits. Normally you would just set one bit.
Definition: b2_fixture.h:47
b2FixtureDef::shape
const b2Shape * shape
Definition: b2_fixture.h:76
b2Shape::ComputeMass
virtual void ComputeMass(b2MassData *massData, float density) const =0
b2Fixture::GetAABB
const b2AABB & GetAABB(int32 childIndex) const
Definition: b2_fixture.h:345
b2Fixture::SetFilterData
void SetFilterData(const b2Filter &filter)
b2FixtureDef
Definition: b2_fixture.h:62
b2Fixture::Dump
void Dump(int32 bodyIndex)
Dump this fixture to the log file.
b2Fixture::SetDensity
void SetDensity(float density)
Definition: b2_fixture.h:299
b2Fixture
Definition: b2_fixture.h:113
b2AABB
An axis aligned bounding box.
Definition: b2_collision.h:169
b2FixtureProxy
This proxy is used internally to connect fixtures to the broad-phase.
Definition: b2_fixture.h:100
b2Fixture::IsSensor
bool IsSensor() const
Definition: b2_fixture.h:259
b2Filter::groupIndex
int16 groupIndex
Definition: b2_fixture.h:56
b2Fixture::TestPoint
bool TestPoint(const b2Vec2 &p) const
Definition: b2_fixture.h:330
b2BlockAllocator
Definition: b2_block_allocator.h:38
b2Fixture::GetUserData
void * GetUserData() const
Definition: b2_fixture.h:269
b2_collision.h
b2Fixture::SetFriction
void SetFriction(float friction)
Definition: b2_fixture.h:315
b2BroadPhase
Definition: b2_broad_phase.h:41
b2Contact
Definition: b2_contact.h:83
b2FixtureDef::density
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:88
b2World
Definition: b2_world.h:47
b2Fixture::GetFriction
float GetFriction() const
Get the coefficient of friction.
Definition: b2_fixture.h:310
b2Filter::maskBits
uint16 maskBits
Definition: b2_fixture.h:51
b2Shape
Definition: b2_shape.h:49
b2FixtureDef::isSensor
bool isSensor
Definition: b2_fixture.h:92
b2MassData
This holds the mass data computed for a shape.
Definition: b2_shape.h:34
b2Fixture::GetMassData
void GetMassData(b2MassData *massData) const
Definition: b2_fixture.h:340
b2Fixture::GetShape
b2Shape * GetShape()
Definition: b2_fixture.h:249
b2Fixture::GetNext
b2Fixture * GetNext()
Definition: b2_fixture.h:289
b2Filter
This holds contact filtering data.
Definition: b2_fixture.h:38
b2RayCastOutput
Definition: b2_collision.h:162
b2Shape::TestPoint
virtual bool TestPoint(const b2Transform &xf, const b2Vec2 &p) const =0
b2FixtureDef::b2FixtureDef
b2FixtureDef()
The constructor sets the default fixture definition values.
Definition: b2_fixture.h:64
b2Fixture::SetRestitution
void SetRestitution(float restitution)
Definition: b2_fixture.h:325
b2Shape::GetType
Type GetType() const
Definition: b2_shape.h:105