Dura2D  v0.1.0
An educational 2D game physics library
Loading...
Searching...
No Matches
d2Body.h
Go to the documentation of this file.
1#ifndef BODY_H
2#define BODY_H
3
4#include "d2api.h"
5
6#include "d2Shape.h"
7#include "d2Math.h"
8#include "d2Types.h"
9
10// Forward declarations
11class d2World;
12struct d2AABB;
13
14// enums
16{
17 d2_staticBody = 0, //< A static body does not move or rotate.
18 d2_dynamicBody //< A dynamic body is fully simulated.
19};
20
28{
29public:
38 d2Body(const d2Shape &shape, real x, real y, real mass, d2World* world);
39
44
47
53 void AddForce(const d2Vec2 &force);
54
60 void AddTorque(real torque);
61
64
67
75
83
89 void ApplyImpulseLinear(const d2Vec2 &j);
90
97
104 void ApplyImpulseAtPoint(const d2Vec2 &j, const d2Vec2 &r);
105
112
119
125 void IntegrateForces(const real dt);
126
132 void IntegrateVelocities(const real dt);
133
139 inline const d2Vec2& GetPosition() const;
140
146 inline void SetPosition(const d2Vec2& position);
147
153 inline const d2Vec2& GetVelocity() const;
154
156 inline void SetAcceleration(const d2Vec2& acceleration);
157
159 inline void SetAngularVelocity(real angularVelocity);
160
166 inline real GetAngularVelocity() const;
167
173 inline real GetRotation() const;
174
180 inline real GetMass() const;
181
187 inline real GetInvMass() const;
188
194 inline real GetI() const;
195
201 inline real GetInvI() const;
202
204 inline void SetFriction(real friction);
205
211 inline real GetFriction() const;
212
214 inline void SetRestitution(real restitution);
215
221 inline real GetRestitution() const;
222
228 inline d2Shape* GetShape() const;
229
235 inline d2AABB* GetAABB() const;
236
242 inline d2Body* GetNext();
243 inline const d2Body* GetNext() const;
244
250 inline d2Body* GetPrev();
251 inline const d2Body* GetPrev() const;
252
258 inline real GetSleepTime() const;
259
265 inline bool IsAwake() const;
266
272 inline void SetAwake(bool awake);
273
275 inline real GetGravityScale() const;
276
278 inline void SetGravityScale(real gravityScale);
279
280private:
281 friend class d2World;
282
283 enum
284 {
285 e_awakeFlag = 0x0001
286 };
287
288 uint16 m_flags{};
289 d2BodyType m_type{};
290
291 d2Transform m_transform;
292
293 d2Vec2 velocity {};
294 d2Vec2 acceleration {};
295
296 real m_gravityScale{ 1.0f };
297
298 real angularVelocity {};
299 real angularAcceleration {};
300
301 d2Vec2 sumForces {};
302 real sumTorque {};
303
304 real mass {};
305 real invMass {};
306 real I {};
307 real invI {};
308
309 real restitution {};
310
311 real friction {};
312
313 d2AABB *aabb { nullptr };
314
315 d2Shape *shape { nullptr };
316
317 d2World* world { nullptr };
318 d2Body* prev { nullptr };
319 d2Body* next { nullptr };
320
321 real m_sleepTime{};
322};
323
324inline const d2Vec2& d2Body::GetPosition() const
325{
326 return m_transform.p;
327}
328
329inline void d2Body::SetPosition(const d2Vec2& position)
330{
331 m_transform.p = position;
332}
333
334inline const d2Vec2& d2Body::GetVelocity() const
335{
336 return velocity;
337}
338
339inline void d2Body::SetAcceleration(const d2Vec2& acceleration)
340{
341 this->acceleration = acceleration;
342}
343
344inline void d2Body::SetAngularVelocity(real angularVelocity)
345{
346 this->angularVelocity = angularVelocity;
347}
348
350{
351 return angularVelocity;
352}
353
355{
356 return m_transform.q.GetAngle();
357}
358
359inline real d2Body::GetMass() const
360{
361 return mass;
362}
363
365{
366 return invMass;
367}
368
369inline real d2Body::GetI() const
370{
371 return I;
372}
373
374inline real d2Body::GetInvI() const
375{
376 return invI;
377}
378
379inline void d2Body::SetFriction(real friction)
380{
381 this->friction = friction;
382}
383
385{
386 return friction;
387}
388
389inline void d2Body::SetRestitution(real restitution)
390{
391 this->restitution = restitution;
392}
393
395{
396 return restitution;
397}
398
400{
401 return shape;
402}
403
404inline d2AABB* d2Body::GetAABB() const
405{
406 return aabb;
407}
408
410{
411 return next;
412}
413
414inline const d2Body* d2Body::GetNext() const
415{
416 return next;
417}
418
420{
421 return prev;
422}
423
424inline const d2Body* d2Body::GetPrev() const
425{
426 return prev;
427}
428
430{
431 return m_sleepTime;
432}
433
435{
436 return m_gravityScale;
437}
438
439inline void d2Body::SetGravityScale(real gravityScale)
440{
441 m_gravityScale = gravityScale;
442}
443
444inline bool d2Body::IsAwake() const
445{
446 return (m_flags & e_awakeFlag) == e_awakeFlag;
447}
448
449inline void d2Body::SetAwake(bool awake)
450{
451 if (awake)
452 {
453 m_flags |= e_awakeFlag;
454 m_sleepTime = 0.0f;
455 }
456 else
457 {
458 m_flags &= ~e_awakeFlag;
459 m_sleepTime = 0.0f;
460 velocity = d2Vec2(0, 0);
461 angularVelocity = 0.0f;
462 sumForces = d2Vec2(0, 0);
463 sumTorque = 0.0f;
464 }
465}
466
467#endif
A class representing a 2D rigid body.
Definition d2Body.h:28
const d2Vec2 & GetPosition() const
Gets the position of the body.
Definition d2Body.h:324
void IntegrateLinear(real dt)
Integrates the linear motion of the body over a time step.
const d2Vec2 & GetVelocity() const
Gets the velocity of the body.
Definition d2Body.h:334
void SetGravityScale(real gravityScale)
Sets the gravity scale of the body.
Definition d2Body.h:439
real GetMass() const
Gets the mass of the body.
Definition d2Body.h:359
void ClearForces()
Clears all forces applied to the body.
d2Body * GetNext()
Gets the next body in a linked list of bodies.
Definition d2Body.h:409
void ApplyImpulseLinear(const d2Vec2 &j)
Applies a linear impulse to the body.
real GetI() const
Gets the moment of inertia of the body.
Definition d2Body.h:369
void AddTorque(real torque)
Adds a torque to the body.
d2Vec2 LocalSpaceToWorldSpace(const d2Vec2 &point) const
Converts a point from local space to world space.
void SetAwake(bool awake)
Sets the wake state of the body.
Definition d2Body.h:449
real GetAngularVelocity() const
Gets the angular velocity of the body.
Definition d2Body.h:349
real GetRestitution() const
Gets the coefficient of restitution of the body.
Definition d2Body.h:394
real GetRotation() const
Gets the rotation angle of the body.
Definition d2Body.h:354
void SetRestitution(real restitution)
Sets the coefficient of restitution of the body.
Definition d2Body.h:389
void ApplyImpulseAngular(const real j)
Applies an angular impulse to the body.
void IntegrateForces(const real dt)
Integrates the forces applied to the body over a time step.
void IntegrateAngular(real dt)
Integrates the angular motion of the body over a time step.
bool IsAwake() const
Gets the wake state of the body.
Definition d2Body.h:444
real GetFriction() const
Gets the coefficient of friction of the body.
Definition d2Body.h:384
void SetFriction(real friction)
Sets the coefficient of friction of the body.
Definition d2Body.h:379
void ClearTorque()
Clears all torques applied to the body.
void AddForce(const d2Vec2 &force)
Adds a force to the body.
real GetInvI() const
Gets the inverse moment of inertia of the body.
Definition d2Body.h:374
void SetPosition(const d2Vec2 &position)
Sets the position of the body.
Definition d2Body.h:329
void SetAcceleration(const d2Vec2 &acceleration)
Set acceleration of the body.
Definition d2Body.h:339
void IntegrateVelocities(const real dt)
Integrates the velocities of the body over a time step.
d2Body * GetPrev()
Gets the previous body in a linked list of bodies.
Definition d2Body.h:419
d2AABB * GetAABB() const
Gets the Axis-Aligned Bounding Box of the body.
Definition d2Body.h:404
real GetSleepTime() const
Gets wake-up time of the body.
Definition d2Body.h:429
~d2Body()
Destructor for the d2Body class.
d2Vec2 WorldSpaceToLocalSpace(const d2Vec2 &point) const
Converts a point from world space to local space.
real GetGravityScale() const
Gets the gravity scale of the body.
Definition d2Body.h:434
real GetInvMass() const
Gets the inverse mass of the body.
Definition d2Body.h:364
d2Body(const d2Shape &shape, real x, real y, real mass, d2World *world)
Constructor for the d2Body class.
d2Shape * GetShape() const
Gets the shape/geometry of the body.
Definition d2Body.h:399
void SetAngularVelocity(real angularVelocity)
Sets the angular velocity of the body.
Definition d2Body.h:344
void ApplyImpulseAtPoint(const d2Vec2 &j, const d2Vec2 &r)
Applies an impulse at a specific point on the body.
void ComputeAABB()
Computes the Axis-Aligned Bounding Box of the body.
Represents a 2D physics world.
Definition d2World.h:22
d2BodyType
Definition d2Body.h:16
@ d2_staticBody
Definition d2Body.h:17
@ d2_dynamicBody
Definition d2Body.h:18
float real
Definition d2Types.h:10
unsigned short uint16
Definition d2Types.h:8
#define D2_API
Definition d2api.h:27
Definition d2AABB.h:10
real GetAngle() const
Get the rotation angle in radians.
Definition d2Math.h:254
Definition d2Shape.h:18
Represents a transformation in 2D space.
Definition d2Math.h:309
d2Vec2 p
The translation of the body.
Definition d2Math.h:311
d2Rot q
The rotation of the body.
Definition d2Math.h:314
Represents a 2D vector.
Definition d2Math.h:22