CG-Project2
Loading...
Searching...
No Matches
PhysicWorld.hpp
1#pragma once
2
3#include "../../Opengl-Core/include/Core.hpp"
4
5#include <glm/ext/vector_float3.hpp>
6#include <utility>
7#include <vector>
8
9inline glm::vec3 GRAVITY = {0, -9.82f, 0};
10
11class PhysicWorld;
12
22class Solver {
23public:
29 virtual void solve() { std::cout << "solve\n"; };
30
38
39 virtual ~Solver() = default;
40
43};
44
48class CollisionSolver : public Solver {
49public:
59 virtual void solve() override;
60
69
70 virtual ~CollisionSolver() override = default;
71};
72
76class PositionSolver : public Solver {
77public:
78 virtual void solve() override;
79
88
89 virtual ~PositionSolver() override = default;
90};
91
95class GravitySolver : public Solver {
96public:
97 virtual void solve() override;
98
107
108 virtual ~GravitySolver() override = default;
109};
110
123class PhysicWorld : public ogl::Layer {
124public:
128 virtual void onAttach() override;
129
133 virtual void onUpdate() override;
134
144 template <typename T, typename... Args>
145 inline void addSolver(Args &&...args) {
146 this->m_solvers.push_back(CreateShared<T>(*this, std::forward<Args>(args)...));
147 }
148
154 void addEntity(const unsigned int &id);
155
162 bool removeEntity(const unsigned int &id);
163
169 inline float getWorldDeltaTime() const { return this->m_deltaTime; }
170
176 inline std::vector<unsigned int> getEntities() const { return this->m_entities; }
177
185 PhysicWorld(const std::string &name = "Physic World") :
186 ogl::Layer(std::move(name)) {}
187
188 virtual ~PhysicWorld() override = default;
189
190private:
192 std::vector<unsigned int> m_entities{};
194 std::vector<Shared<Solver>> m_solvers{};
195
197 float m_lastFrame = 0;
199 float m_currentFrame = 0;
201 float m_deltaTime = 0;
202};
203
204namespace systems {
210 namespace physic {
220 void resetGravitySolver(const unsigned int &id);
221
228 glm::vec3 getVelocity(const unsigned int &id);
235 void updateVelocity(const unsigned int &id, const glm::vec3 velocity);
242 void addVelocity(const unsigned int &id, const glm::vec3 offset);
243
250 void updateRestitutionFactor(const unsigned int &id, const float &factor);
251
258 glm::vec3 getAcceleration(const unsigned int &id);
265 void updateAcceleration(const unsigned int &id, const glm::vec3 acceleration);
272 void addAcceleration(const unsigned int &id, const glm::vec3 offset);
273
280 glm::vec3 getForce(const unsigned int &id);
287 void updateForce(const unsigned int &id, const glm::vec3 force);
294 void addForce(const unsigned int &id, const glm::vec3 offset);
295
302 float getMass(const unsigned int &id);
309 void updateMass(const unsigned int &id, const float &mass);
310 } // namespace physic
311} // namespace systems
Solves all collision between entities in the given PhysicWorld.
Definition PhysicWorld.hpp:48
CollisionSolver(PhysicWorld &world)
Instance basic CollisionSolver object.
Definition PhysicWorld.hpp:67
virtual void solve() override
It gets all collisions at the current step, and calls a different method based on the collider type o...
Definition PhysicWorld.cpp:103
Simulate gravity force towards the negative y world axis.
Definition PhysicWorld.hpp:95
virtual void solve() override
This method is executed at every physic simulation step.
Definition PhysicWorld.cpp:179
GravitySolver(PhysicWorld &world)
Instance basic GravitySolver object.
Definition PhysicWorld.hpp:105
The PhysicWorld is a Layer that simulates physic objects in the scene.
Definition PhysicWorld.hpp:123
bool removeEntity(const unsigned int &id)
Removes the given entity from the simulation.
Definition PhysicWorld.cpp:224
PhysicWorld(const std::string &name="Physic World")
Construct a simulated world.
Definition PhysicWorld.hpp:185
float getWorldDeltaTime() const
Removes the given entity from the simulation.
Definition PhysicWorld.hpp:169
std::vector< unsigned int > getEntities() const
Definition PhysicWorld.hpp:176
virtual void onUpdate() override
Definition PhysicWorld.cpp:205
virtual void onAttach() override
Definition PhysicWorld.cpp:200
void addSolver(Args &&...args)
This method add a Solver to physics simulation.
Definition PhysicWorld.hpp:145
void addEntity(const unsigned int &id)
Adds the given entity to the simulation.
Definition PhysicWorld.cpp:217
It calculates all the entities position from the current velocity.
Definition PhysicWorld.hpp:76
virtual void solve() override
This method is executed at every physic simulation step.
Definition PhysicWorld.cpp:171
PositionSolver(PhysicWorld &world)
Instance basic PositionSolver object.
Definition PhysicWorld.hpp:86
A Solver defines a generic behaviour in a Physic World.
Definition PhysicWorld.hpp:22
PhysicWorld & world
the reference to the PhysicWorld
Definition PhysicWorld.hpp:42
Solver(PhysicWorld &world)
Instances basic Solver.
Definition PhysicWorld.hpp:36
virtual void solve()
This method is executed at every physic simulation step.
Definition PhysicWorld.hpp:29
Definition Layer.hpp:6
This namespaces contains all utilities for entities affected by physic.
void updateVelocity(const unsigned int &id, const glm::vec3 velocity)
Updates the velocity of the given entity.
Definition PhysicWorld.cpp:253
glm::vec3 getVelocity(const unsigned int &id)
Retrieves the velocity of the given entity.
Definition PhysicWorld.cpp:246
void resetGravitySolver(const unsigned int &id)
Resets velocity on the on y-axis, acceleration and force of the given entity.
Definition PhysicWorld.cpp:236
void updateForce(const unsigned int &id, const glm::vec3 force)
Updates the force of the given entity.
Definition PhysicWorld.cpp:302
void addVelocity(const unsigned int &id, const glm::vec3 offset)
Adds the given offset to the velocity of the given entity.
Definition PhysicWorld.cpp:260
void addForce(const unsigned int &id, const glm::vec3 offset)
Adds the given offset to the force of the given entity.
Definition PhysicWorld.cpp:309
void updateAcceleration(const unsigned int &id, const glm::vec3 acceleration)
Updates the acceleration of the given entity.
Definition PhysicWorld.cpp:281
void updateRestitutionFactor(const unsigned int &id, const float &factor)
Updates the restitution factor of the given entity.
Definition PhysicWorld.cpp:267
float getMass(const unsigned int &id)
Retrieves the mass of the given entity.
Definition PhysicWorld.cpp:316
glm::vec3 getForce(const unsigned int &id)
Retrieves the force of the given entity.
Definition PhysicWorld.cpp:295
void addAcceleration(const unsigned int &id, const glm::vec3 offset)
Adds the given offset to the acceleration of the given entity.
Definition PhysicWorld.cpp:288
glm::vec3 getAcceleration(const unsigned int &id)
Retrieves the acceleration of the given entity.
Definition PhysicWorld.cpp:274
void updateMass(const unsigned int &id, const float &mass)
Updates the mass of the given entity.
Definition PhysicWorld.cpp:323
It contains all the ECS systems.