From 436b0db58c7533b286ecd3ec3d3c71311e71cf9c Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Wed, 11 Dec 2024 20:21:55 +0100 Subject: added fixed update --- src/crepe/system/PhysicsSystem.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/crepe/system/PhysicsSystem.cpp') 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 rigidbodies = mgr.get_components_by_type(); - + duration_t delta_time = loop_timer.get_scaled_fixed_delta_time(); + float dt = duration_cast(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 -- cgit v1.2.3