From 98f0040c87b44528752d7527dccf74d5f54898cf Mon Sep 17 00:00:00 2001 From: George Fotopoulos Date: Thu, 25 Jan 2024 11:44:56 +0200 Subject: [PATCH] Refactor Application --- include/Application.hpp | 1 + src/Application.cpp | 49 ++++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/include/Application.hpp b/include/Application.hpp index 1a93f79..b92e249 100644 --- a/include/Application.hpp +++ b/include/Application.hpp @@ -6,6 +6,7 @@ class Application { public: explicit Application(ApplicationSettings applicationSettings); + void fixedUpdate(float fixedDeltaTime); void Run(); private: diff --git a/src/Application.cpp b/src/Application.cpp index 73d0d66..0aa54e9 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -30,6 +30,31 @@ Application::Application(ApplicationSettings applicationSettings) : mApplication mSoftBodies.emplace_back(SpringPendulum(glm::vec2(400.0f, 0.0f), 100.0f, 2.0f, 4.0f)); } +void Application::fixedUpdate(float fixedDeltaTime) { + for (SoftBody &softBody: mSoftBodies) { + for (Spring &spring: softBody.springs) { + const glm::vec2 dPosition = spring.mParticle2.position - spring.mParticle1.position; + const glm::vec2 dVelocity = spring.mParticle2.velocity - spring.mParticle1.velocity; + if (glm::length(dPosition) != 0) { + const glm::vec2 force = spring.mSpringConstant * (glm::length(dPosition) - spring.mRestLength) * glm::normalize(dPosition) + spring.mDampingConstant * dVelocity; + spring.mParticle1.force += force; + spring.mParticle2.force -= force; + } + } + for (Particle &particle: softBody.particles) { + const glm::vec2 gravitationalForce = particle.mass * mGravitationalAcceleration; + particle.force += gravitationalForce; + if (!particle.pinned) { + particle.HandleBoundaryCollisions(); + const glm::vec2 acceleration = particle.force / particle.mass; + particle.velocity += acceleration * fixedDeltaTime; + particle.position += particle.velocity * fixedDeltaTime; + } + particle.force = glm::vec2(0.0f, 0.0f); + } + } +} + void Application::Run() { sf::VideoMode mode(mApplicationSettings.width, mApplicationSettings.height); @@ -73,29 +98,7 @@ void Application::Run() { const sf::Time deltaTime = deltaClock.restart(); accumulator += timeScale * deltaTime; while (accumulator > fixedDeltaTime) { - const float dt = fixedDeltaTime.asSeconds(); - for (SoftBody &softBody: mSoftBodies) { - for (Spring &spring: softBody.springs) { - const glm::vec2 dPosition = spring.mParticle2.position - spring.mParticle1.position; - const glm::vec2 dVelocity = spring.mParticle2.velocity - spring.mParticle1.velocity; - if (glm::length(dPosition) != 0) { - const glm::vec2 force = spring.mSpringConstant * (glm::length(dPosition) - spring.mRestLength) * glm::normalize(dPosition) + spring.mDampingConstant * dVelocity; - spring.mParticle1.force += force; - spring.mParticle2.force -= force; - } - } - for (Particle &particle: softBody.particles) { - const glm::vec2 gravitationalForce = particle.mass * mGravitationalAcceleration; - particle.force += gravitationalForce; - if (!particle.pinned) { - particle.HandleBoundaryCollisions(); - const glm::vec2 acceleration = particle.force / particle.mass; - particle.velocity += acceleration * dt; - particle.position += particle.velocity * dt; - } - particle.force = glm::vec2(0.0f, 0.0f); - } - } + fixedUpdate(fixedDeltaTime.asSeconds()); accumulator -= fixedDeltaTime; }