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 | |
parent | c90423d5c9d38ae426c168e52994a4fd63e6f266 (diff) |
added fixed update
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/AISystem.cpp | 6 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 22 |
2 files changed, 21 insertions, 7 deletions
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index 1d8ffb9..1bbac69 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -13,12 +13,10 @@ using namespace std::chrono; void AISystem::update() { const Mediator & mediator = this->mediator; ComponentManager & mgr = mediator.component_manager; - LoopTimerManager & timer = mediator.loop_timer; - RefVector<AI> ai_components = mgr.get_components_by_type<AI>(); LoopTimerManager & loop_timer = mediator.loop_timer; + RefVector<AI> ai_components = mgr.get_components_by_type<AI>(); - //TODO: Use fixed loop dt (this is not available at master at the moment) - duration_t dt = loop_timer.get_delta_time(); + duration_t dt = loop_timer.get_scaled_fixed_delta_time(); // Loop through all AI components for (AI & ai : ai_components) { 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 |