diff options
author | JAROWMR <jarorutjes07@gmail.com> | 2024-12-11 20:21:55 +0100 |
---|---|---|
committer | JAROWMR <jarorutjes07@gmail.com> | 2024-12-11 20:21:55 +0100 |
commit | 436b0db58c7533b286ecd3ec3d3c71311e71cf9c (patch) | |
tree | 1a344b7b0a369013f8ba275be948dc0c3c721345 /src/crepe/system/PhysicsSystem.cpp | |
parent | c90423d5c9d38ae426c168e52994a4fd63e6f266 (diff) |
added fixed update
Diffstat (limited to 'src/crepe/system/PhysicsSystem.cpp')
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index be768f9..77c3be7 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -5,17 +5,24 @@ #include "../api/Transform.h" #include "../api/Vector2.h" #include "../manager/ComponentManager.h" +#include "../manager/LoopTimerManager.h" +#include "../manager/Mediator.h" #include "PhysicsSystem.h" using namespace crepe; +using namespace std::chrono; void PhysicsSystem::update() { - double dt = LoopTimer::get_instance().get_fixed_delta_time(); - ComponentManager & mgr = this->mediator.component_manager; + + const Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + LoopTimerManager & loop_timer = mediator.loop_timer; RefVector<Rigidbody> rigidbodies = mgr.get_components_by_type<Rigidbody>(); - + duration_t delta_time = loop_timer.get_scaled_fixed_delta_time(); + float dt = duration_cast<seconds>(delta_time).count(); + float gravity = Config::get_instance().physics.gravity; for (Rigidbody & rigidbody : rigidbodies) { if (!rigidbody.active) continue; @@ -25,6 +32,15 @@ void PhysicsSystem::update() { case Rigidbody::BodyType::DYNAMIC: if (transform.game_object_id == rigidbody.game_object_id) { // Add gravity + + if (rigidbody.data.mass <= 0) { + throw std::runtime_error("Mass must be greater than 0"); + } + + if (gravity <= 0) { + throw std::runtime_error("Config Gravity must be greater than 0"); + } + if (rigidbody.data.gravity_scale > 0) { rigidbody.data.linear_velocity.y += (rigidbody.data.mass * rigidbody.data.gravity_scale |