diff options
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/api/Config.h | 2 | ||||
-rw-r--r-- | src/crepe/api/Rigidbody.h | 2 | ||||
-rw-r--r-- | src/crepe/system/AISystem.cpp | 8 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.cpp | 4 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 53 |
5 files changed, 38 insertions, 31 deletions
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index cd27343..ca2d3f1 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -53,7 +53,7 @@ struct Config final { * * Gravity value of game. */ - float gravity = 1; + float gravity = 10; } physics; //! default window settings 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..6578ecb 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) { @@ -45,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_cast<seconds>(dt).count(); + rigidbody.data.linear_velocity += acceleration * duration<float>(dt).count(); } } diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index dc5270e..0483693 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -290,10 +290,6 @@ vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider & circle_co // Compute penetration depth float penetration_depth = circle_collider.radius - distance; -<<<<<<< HEAD -======= - ->>>>>>> 05a33d4793520fa84a93bc79882ef29d39cd08e5 // Compute the resolution vector vec2 resolution = collision_normal * penetration_depth; 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; } |