Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
physics_world.h
Go to the documentation of this file.
1#pragma once
2
3#include <box2d/box2d.h>
10
11#include <memory>
12#include <set>
13
14class Component;
15class Collider2D;
16
17constexpr int32_t default_velocity_iterations = 6;
18constexpr float default_gravity_x = 0.0f;
19constexpr float default_gravity_y = 30.0f;
20
28 public:
31
32 PhysicsWorld(const PhysicsWorld&) = delete;
36
38
47 void step(float dt);
48
57 const std::vector<std::reference_wrapper<GameObject>>& objects);
58
61 std::optional<std::reference_wrapper<Collider2D>> find_collider_in_objects(
62 const std::vector<std::reference_wrapper<GameObject>>& objects,
63 Component* comp, b2ShapeId shape_id);
64
67 const std::vector<std::reference_wrapper<GameObject>>& objects);
70 const std::vector<std::reference_wrapper<GameObject>>& objects);
73 const std::vector<std::reference_wrapper<GameObject>>& objects);
76 const std::vector<std::reference_wrapper<GameObject>>& objects);
77
80 const std::vector<std::reference_wrapper<GameObject>>& objects,
81 b2ShapeId a, b2ShapeId b,
82 const std::function<void(Collider2D&, Collider2D&)>& callback);
83
92 const Body2DTransform& b);
93
107 const Body2DTransform& b);
108
109 [[nodiscard]] b2WorldId world_id() const noexcept;
110
111 [[nodiscard]] int32_t velocity_iterations() const noexcept;
112 int32_t velocity_iterations(int32_t iterations) noexcept;
113
114 [[nodiscard]] float gravity_x() const noexcept;
115 float gravity_x(float gx) noexcept;
116
117 [[nodiscard]] float gravity_y() const noexcept;
118 float gravity_y(float gy) noexcept;
119
120 [[nodiscard]] bool world_exists() const noexcept;
121
122 static constexpr float pixels_per_meters = 16.0f;
123 static constexpr float meters_per_pixels = 1.0f / pixels_per_meters;
124
125 private:
133 b2WorldId world_id_{};
134
136 int32_t velocity_iterations_{default_velocity_iterations};
137
139 float gravity_x_{default_gravity_x};
140
142 float gravity_y_{default_gravity_y};
143
145 bool debug_draw_enabled_{false};
146};
A 2D collider base that defines the shape and physical properties of a GameObject for collision detec...
Definition collider_2d.h:31
Base class for all components that can be attached to GameObjects.
Definition component.h:24
Represents the physics world using Box2D.
Definition physics_world.h:27
int32_t velocity_iterations() const noexcept
static constexpr float meters_per_pixels
Definition physics_world.h:123
static constexpr float pixels_per_meters
Definition physics_world.h:122
static BodyDistance2D fixture_distance(const Body2DTransform &a, const Body2DTransform &b)
Calculate the distance between two bodies using their fixtures.
float gravity_x() const noexcept
void check_contact_touch_begin_events(const std::vector< std::reference_wrapper< GameObject > > &objects)
Checks for the beginning of contact touch events.
bool world_exists() const noexcept
std::optional< std::reference_wrapper< Collider2D > > find_collider_in_objects(const std::vector< std::reference_wrapper< GameObject > > &objects, Component *comp, b2ShapeId shape_id)
Finds a collider in the given objects based on the component and shape ID.
void check_sensor_touch_begin_events(const std::vector< std::reference_wrapper< GameObject > > &objects)
Checks for the beginning of sensor touch events.
void step(float dt)
Steps the physics simulation forward by the fixed time step.
PhysicsWorld & operator=(PhysicsWorld &&)=delete
PhysicsWorld & operator=(const PhysicsWorld &)=delete
void execute_valid_collider(const std::vector< std::reference_wrapper< GameObject > > &objects, b2ShapeId a, b2ShapeId b, const std::function< void(Collider2D &, Collider2D &)> &callback)
Executes a callback if the given shape IDs are valid.
PhysicsWorld(PhysicsWorld &&)=delete
void check_sensor_touch_end_events(const std::vector< std::reference_wrapper< GameObject > > &objects)
Checks for the ending of sensor touch events.
float gravity_y() const noexcept
void check_contact_touch_end_events(const std::vector< std::reference_wrapper< GameObject > > &objects)
Checks for the ending of contact touch events.
PhysicsWorld(const PhysicsWorld &)=delete
void check_collision(const std::vector< std::reference_wrapper< GameObject > > &objects)
Checks for collisions in the physics world.
b2WorldId world_id() const noexcept
PhysicsWorld()
Constructs a new physics world.
static BodyDistance2D distance(const Body2DTransform &a, const Body2DTransform &b)
Calculate the distance between two bodies.
constexpr int32_t default_velocity_iterations
Definition physics_world.h:17
constexpr float default_gravity_x
Definition physics_world.h:18
constexpr float default_gravity_y
Definition physics_world.h:19
Structure representing the transform of a 2D physics body.
Definition body_2d.h:147
Structure representing the distance information between two colliders.
Definition body_distance_2d.h:11