Dura2D  v0.1.0
An educational 2D game physics library
Loading...
Searching...
No Matches
d2World.h
Go to the documentation of this file.
1#ifndef D2WORLD_H
2#define D2WORLD_H
3
4#include <vector>
5
6#include "d2api.h"
7#include "d2Math.h"
9
10// Forward declarations
11struct d2Color;
12class d2Body;
13struct d2Shape;
14class d2Broadphase;
15class d2Constraint;
16class d2Draw;
17
22{
23public:
28 explicit d2World(const d2Vec2& gravity);
29
34
42 d2Body* CreateBody(const d2Shape& shape, d2Vec2 position, real mass);
43
49 void DestroyBody(d2Body* body);
50
58 d2Constraint* CreateJoint(d2Body* bodyA, d2Body* bodyB, d2Vec2 anchorPoint);
59
66
71 d2Constraint*& GetConstraints();
72
77 int32 GetConstraintCount() const;
78
83 void AddForce(const d2Vec2& force);
84
89 void AddTorque(real torque);
90
96 void Step(real dt, int32 posIterations = 3);
97
102 void Solve(real dt);
103
108
113 d2Body* GetBodies() const;
114
119 int32 GetBodyCount() const;
120
125 void SetDebugDraw(d2Draw* debugDraw);
126
128 void DrawShape(const d2Body* body, const bool &mesh, const d2Color& color);
129
131 void DebugDraw();
132
133public:
134 d2World(const d2World& other) = delete;
135 d2World& operator=(const d2World& other) = delete;
136
137 d2Vec2 m_gravity {0.0f, -9.8f };
141 d2Body* m_bodiesList { nullptr };
142 int32 m_bodyCount { 0 };
144 d2Constraint *m_constraints { nullptr };
145 int32 m_constraintCount { 0 };
147 d2Draw* m_debugDraw { nullptr };
148};
149
151{
152 return m_bodiesList;
153}
154
156{
157 return m_bodyCount;
158}
159
161{
162 return m_constraints;
163}
164
166{
167 return m_constraintCount;
168}
169
170#endif // D2WORLD_H
The d2BlockAllocator class is responsible for managing memory allocation and deallocation....
Definition d2BlockAllocator.h:41
A class representing a 2D rigid body.
Definition d2Body.h:28
Definition d2Broadphase.h:16
Definition d2Constraint.h:7
Definition d2Draw.h:67
Represents a 2D physics world.
Definition d2World.h:22
void SetDebugDraw(d2Draw *debugDraw)
Set the debug draw object.
void AddTorque(real torque)
Add an external torque to be applied to m_bodiesList.
d2Constraint * m_constraints
Definition d2World.h:144
void DestroyBody(d2Body *body)
Destroy a body.
void DebugDraw()
Debug draw the world.
int32 m_bodyCount
Definition d2World.h:142
d2Body * m_bodiesList
Definition d2World.h:141
int32 GetConstraintCount() const
Get the number of constraints in the world.
Definition d2World.h:165
d2World(const d2World &other)=delete
d2BlockAllocator m_blockAllocator
Definition d2World.h:139
void AddForce(const d2Vec2 &force)
Add an external force to be applied to m_bodiesList.
d2Body * GetBodies() const
Get pointer to the array of m_bodiesList.
Definition d2World.h:150
int32 m_constraintCount
Definition d2World.h:145
d2World(const d2Vec2 &gravity)
Constructor that initializes the world with specified gravity.
~d2World()
Destructor to clean up resources.
void Step(real dt, int32 posIterations=3)
Update the world simulation by a specified time step.
void DrawShape(const d2Body *body, const bool &mesh, const d2Color &color)
Draw a shape.
void CheckCollisions()
Check for collisions between m_bodiesList.
d2Constraint *& GetConstraints()
Get a reference to the list of constraints.
Definition d2World.h:160
d2World & operator=(const d2World &other)=delete
d2Broadphase * broadphase
Definition d2World.h:138
int32 GetBodyCount() const
Get the number of m_bodiesList in the world.
Definition d2World.h:155
void Solve(real dt)
Solve the world simulation by a specified time step.
d2Constraint * CreateJoint(d2Body *bodyA, d2Body *bodyB, d2Vec2 anchorPoint)
Create a joint between two bodies.
d2Body * CreateBody(const d2Shape &shape, d2Vec2 position, real mass)
Create a new rigid body with given shape, position, and mass.
void DestroyJoint(d2Constraint *joint)
Destroy a joint.
float real
Definition d2Types.h:10
signed int int32
Definition d2Types.h:6
#define D2_API
Definition d2api.h:27
Definition d2Draw.h:12
Definition d2Shape.h:18
Represents a 2D vector.
Definition d2Math.h:22