aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-12-11 20:21:55 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-12-11 20:21:55 +0100
commit436b0db58c7533b286ecd3ec3d3c71311e71cf9c (patch)
tree1a344b7b0a369013f8ba275be948dc0c3c721345 /src
parentc90423d5c9d38ae426c168e52994a4fd63e6f266 (diff)
added fixed update
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Rigidbody.h2
-rw-r--r--src/crepe/system/AISystem.cpp6
-rw-r--r--src/crepe/system/PhysicsSystem.cpp22
3 files changed, 22 insertions, 8 deletions
diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h
index f641fff..b08c8db 100644
--- a/src/crepe/api/Rigidbody.h
+++ b/src/crepe/api/Rigidbody.h
@@ -53,7 +53,7 @@ public:
*/
struct Data {
//! objects mass
- float mass = 0.0;
+ float mass = 1;
/**
* \brief Gravity scale factor.
*
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