aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/PhysicsSystem.cpp
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-12-12 15:06:17 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-12-12 15:06:17 +0100
commit3de0a0f12d9d91e120f92975bda6f9915e5b8fcb (patch)
treed3da29d03914f4a7a644d6fbd47ed513b1270eaf /src/crepe/system/PhysicsSystem.cpp
parent641e202607b0f694df6662532f0165022c0f8621 (diff)
parenta6350aa70a80e0fe1a6eade3b5eefd240b4942f2 (diff)
merge physics
Diffstat (limited to 'src/crepe/system/PhysicsSystem.cpp')
-rw-r--r--src/crepe/system/PhysicsSystem.cpp53
1 files changed, 33 insertions, 20 deletions
diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp
index be768f9..a1d35bb 100644
--- a/src/crepe/system/PhysicsSystem.cpp
+++ b/src/crepe/system/PhysicsSystem.cpp
@@ -5,65 +5,78 @@
#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<float>(delta_time).count();
float gravity = Config::get_instance().physics.gravity;
for (Rigidbody & rigidbody : rigidbodies) {
if (!rigidbody.active) continue;
- Transform & transform = mgr.get_components_by_id<Transform>(rigidbody.game_object_id).front().get();
+ Transform & transform
+ = mgr.get_components_by_id<Transform>(rigidbody.game_object_id).front().get();
switch (rigidbody.data.body_type) {
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
- * gravity * dt);
+ += (rigidbody.data.mass * rigidbody.data.gravity_scale * gravity
+ * dt);
}
// Add coefficient rotation
if (rigidbody.data.angular_velocity_coefficient > 0) {
rigidbody.data.angular_velocity
*= std::pow(rigidbody.data.angular_velocity_coefficient, dt);
-
}
// Add coefficient movement horizontal
- if (rigidbody.data.linear_velocity_coefficient.x > 0)
- {
+ if (rigidbody.data.linear_velocity_coefficient.x > 0) {
rigidbody.data.linear_velocity.x
- *= std::pow(rigidbody.data.linear_velocity_coefficient.x, dt);
+ *= 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.linear_velocity.y
- *= std::pow(rigidbody.data.linear_velocity_coefficient.y, dt);
+ *= std::pow(rigidbody.data.linear_velocity_coefficient.y, dt);
}
// Max velocity check
if (rigidbody.data.angular_velocity
> rigidbody.data.max_angular_velocity) {
- rigidbody.data.angular_velocity
- = rigidbody.data.max_angular_velocity;
+ rigidbody.data.angular_velocity = rigidbody.data.max_angular_velocity;
} else if (rigidbody.data.angular_velocity
- < -rigidbody.data.max_angular_velocity) {
- rigidbody.data.angular_velocity
- = -rigidbody.data.max_angular_velocity;
+ < -rigidbody.data.max_angular_velocity) {
+ rigidbody.data.angular_velocity = -rigidbody.data.max_angular_velocity;
}
-
+
// Set max velocity to maximum length
- if(rigidbody.data.linear_velocity.length() > rigidbody.data.max_linear_velocity) {
+ if (rigidbody.data.linear_velocity.length()
+ > rigidbody.data.max_linear_velocity) {
rigidbody.data.linear_velocity.normalize();
rigidbody.data.linear_velocity *= rigidbody.data.max_linear_velocity;
}