aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-12-12 18:24:26 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-12-12 18:24:26 +0100
commit962c987adff029dc2cbc60b0f1bb9b21b1d416f7 (patch)
tree2738ca92360631efadd99259200feb6577cdfe9c
parent1e42d6669b462a3d1490788d3b20141d15c881db (diff)
parent15bb400deafa1ea4fea1ff11e835f103fa82090f (diff)
Merge branch 'jaro/physics-system-improvement' of github.com:lonkaars/crepe into jaro/particle-system-improvement
-rw-r--r--src/crepe/manager/LoopTimerManager.cpp4
-rw-r--r--src/crepe/manager/LoopTimerManager.h2
-rw-r--r--src/crepe/system/AISystem.cpp4
-rw-r--r--src/crepe/system/PhysicsSystem.cpp13
-rw-r--r--src/test/PhysicsTest.cpp8
5 files changed, 16 insertions, 15 deletions
diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp
index 9819632..4a3df0a 100644
--- a/src/crepe/manager/LoopTimerManager.cpp
+++ b/src/crepe/manager/LoopTimerManager.cpp
@@ -80,8 +80,8 @@ duration_t LoopTimerManager::get_lag() const {
return this->elapsed_time - this->elapsed_fixed_time;
}
-duration_t LoopTimerManager::get_scaled_fixed_delta_time() const {
- return this->fixed_delta_time * this->time_scale;
+float LoopTimerManager::get_scaled_fixed_delta_time() const {
+ return (this->fixed_delta_time * this->time_scale).count();
}
void LoopTimerManager::set_fixed_delta_time(float seconds) {
diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h
index 91403e4..dcbe21c 100644
--- a/src/crepe/manager/LoopTimerManager.h
+++ b/src/crepe/manager/LoopTimerManager.h
@@ -103,7 +103,7 @@ public:
*
* \return The fixed delta time, scaled by the current time scale, in seconds.
*/
- duration_t get_scaled_fixed_delta_time() const;
+ float get_scaled_fixed_delta_time() const;
private:
//! Friend relation to use start,enforce_frame_rate,get_lag,update,advance_fixed_update.
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp
index 6578ecb..e9d8fa7 100644
--- a/src/crepe/system/AISystem.cpp
+++ b/src/crepe/system/AISystem.cpp
@@ -16,7 +16,7 @@ void AISystem::update() {
LoopTimerManager & loop_timer = mediator.loop_timer;
RefVector<AI> ai_components = mgr.get_components_by_type<AI>();
- duration_t dt = loop_timer.get_scaled_fixed_delta_time();
+ float dt = loop_timer.get_scaled_fixed_delta_time();
// Loop through all AI components
for (AI & ai : ai_components) {
@@ -43,7 +43,7 @@ void AISystem::update() {
// Calculate the acceleration (using the above calculated force)
vec2 acceleration = force / rigidbody.data.mass;
// Finally, update Rigidbody's velocity
- rigidbody.data.linear_velocity += acceleration * duration<float>(dt).count();
+ rigidbody.data.linear_velocity += acceleration * dt;
}
}
diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp
index a1d35bb..ca10b25 100644
--- a/src/crepe/system/PhysicsSystem.cpp
+++ b/src/crepe/system/PhysicsSystem.cpp
@@ -11,7 +11,6 @@
#include "PhysicsSystem.h"
using namespace crepe;
-using namespace std::chrono;
void PhysicsSystem::update() {
@@ -19,9 +18,7 @@ void PhysicsSystem::update() {
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<float>(delta_time).count();
+ float dt = loop_timer.get_scaled_fixed_delta_time();
float gravity = Config::get_instance().physics.gravity;
for (Rigidbody & rigidbody : rigidbodies) {
@@ -42,7 +39,7 @@ void PhysicsSystem::update() {
throw std::runtime_error("Config Gravity must be greater than 0");
}
- if (rigidbody.data.gravity_scale > 0) {
+ if (rigidbody.data.gravity_scale > 0 && !rigidbody.data.constraints.y) {
rigidbody.data.linear_velocity.y
+= (rigidbody.data.mass * rigidbody.data.gravity_scale * gravity
* dt);
@@ -54,13 +51,15 @@ void PhysicsSystem::update() {
}
// Add coefficient movement horizontal
- if (rigidbody.data.linear_velocity_coefficient.x > 0) {
+ if (rigidbody.data.linear_velocity_coefficient.x > 0
+ && !rigidbody.data.constraints.x) {
rigidbody.data.linear_velocity.x
*= std::pow(rigidbody.data.linear_velocity_coefficient.x, dt);
}
// Add coefficient movement horizontal
- if (rigidbody.data.linear_velocity_coefficient.y > 0) {
+ if (rigidbody.data.linear_velocity_coefficient.y > 0
+ && !rigidbody.data.constraints.y) {
rigidbody.data.linear_velocity.y
*= std::pow(rigidbody.data.linear_velocity_coefficient.y, dt);
}
diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp
index c04e3ff..3afb3c7 100644
--- a/src/test/PhysicsTest.cpp
+++ b/src/test/PhysicsTest.cpp
@@ -108,7 +108,7 @@ TEST_F(PhysicsTest, movement) {
EXPECT_NEAR(transform.position.x, 0.02, 0.001);
EXPECT_NEAR(transform.position.y, 0.02, 0.001);
EXPECT_NEAR(transform.rotation, 0.02, 0.001);
-
+ rigidbody.data.constraints = {0, 0, 0};
rigidbody.data.linear_velocity_coefficient.x = 0.5;
rigidbody.data.linear_velocity_coefficient.y = 0.5;
rigidbody.data.angular_velocity_coefficient = 0.5;
@@ -122,9 +122,11 @@ TEST_F(PhysicsTest, movement) {
rigidbody.data.max_angular_velocity = 1000;
rigidbody.data.angular_velocity = 360;
system.update();
- EXPECT_NEAR(transform.rotation, 7.22, 0.0001);
+ EXPECT_NEAR(transform.rotation, 7.24, 0.01);
rigidbody.data.angular_velocity = -360;
system.update();
- EXPECT_NEAR(transform.rotation, 0.02, 0.001);
+ EXPECT_NEAR(transform.rotation, 0.04, 0.001);
+ system.update();
+ EXPECT_NEAR(transform.rotation, 352.84, 0.01);
}