From 82f103758dc806faef11f3216db508524172625f Mon Sep 17 00:00:00 2001 From: Okay Uenal Date: Wed, 8 Nov 2023 15:19:29 +0100 Subject: [PATCH] merge stuff demo 2 --- Simulations/MassSpringSystemSimulator.cpp | 55 ++++++++++++++++++++--- Simulations/MassSpringSystemSimulator.h | 5 +++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/Simulations/MassSpringSystemSimulator.cpp b/Simulations/MassSpringSystemSimulator.cpp index 4a4b155..0ea12ac 100644 --- a/Simulations/MassSpringSystemSimulator.cpp +++ b/Simulations/MassSpringSystemSimulator.cpp @@ -7,12 +7,10 @@ MassSpringSystemSimulator::MassSpringSystemSimulator() m_fStiffness = 40; int m_iIntegrater = 0; - - //Test only - auto first = addMassPoint(Vec3(0, 0, 1), Vec3(0, 0, 0), true); - auto second = addMassPoint(Vec3(0, 0, 0), Vec3(0, 0, 0), true); - addSpring(first, second, 2.0); - + auto first = addMassPoint(Vec3(0, 0, 0), Vec3(-1, 0, 0), true); + auto second = addMassPoint(Vec3(0, 2, 0), Vec3(1, 0, 0), true); + addSpring(first, second, 1.0); + m_fStiffness = 40; } const char* MassSpringSystemSimulator::getTestCasesStr() @@ -94,7 +92,7 @@ void MassSpringSystemSimulator::notifyCaseChanged(int testCase) } } -void MassSpringSystemSimulator::externalForcesCalculations(float timeElapsed) +void MassSpringSystemSimulator::externalForcesCalculations(float timeElapsed) { Point2D mouseDiff; mouseDiff.x = m_trackmouse.x - m_oldtrackmouse.x; @@ -137,6 +135,8 @@ void MassSpringSystemSimulator::simulateTimestep(float timeStep) break; default: break; } + + Euler(0, 1, 0, timeStep); } void MassSpringSystemSimulator::onClick(int x, int y) @@ -218,3 +218,44 @@ void MassSpringSystemSimulator::applyExternalForce(Vec3 force) { } + +Vec3 MassSpringSystemSimulator::calcualtePositionTimestepEuler(Vec3 oldPosition, float timestep, Vec3 velocity) +{ + return oldPosition + timestep * velocity; +} + +Vec3 MassSpringSystemSimulator::calcualteVelocityTimestepEuler(Vec3 oldVelocity, float timestep, Vec3 acceleration) +{ + return oldVelocity + acceleration * timestep; +} + +Vec3 MassSpringSystemSimulator::calculateAcceleration(Vec3 force, float mass) +{ + return force / mass; +} + +void MassSpringSystemSimulator::Euler(int index1, int index2, int indexSpring, float timestep) +{ + //take old position and send to calculatePositionTimestepEuler + auto mp = masspoints.at(index1); + auto mp2 = masspoints.at(index2); + Vec3 PosVector = mp->position - mp2->position; + auto lengthVector = sqrt(PosVector.x * PosVector.x + PosVector.y * PosVector.y + PosVector.z * PosVector.z); + auto normalized = PosVector / lengthVector; + + // Actual Calculation + // Force of spring is -k * (l - L) * normalizedVector [for P2 we can take -F1) + auto force = -m_fStiffness * (lengthVector - springs.at(0).initialLength) * normalized; + auto foreP2 = -1 * force; + auto veloc = calcualteVelocityTimestepEuler(mp->velocity, timestep, calculateAcceleration(force, 10.)); + auto pos = calcualtePositionTimestepEuler(mp->position, timestep, veloc); + + auto veloc2 = calcualteVelocityTimestepEuler(mp2->velocity, timestep, calculateAcceleration(foreP2, 10.)); + auto pos2 = calcualtePositionTimestepEuler(mp2->position, timestep, veloc2); + + // Update Positions and Velocity + mp->position = pos; + mp->velocity = veloc; + mp2->position = pos2; + mp2->velocity = veloc2; +} diff --git a/Simulations/MassSpringSystemSimulator.h b/Simulations/MassSpringSystemSimulator.h index d6f968e..d40aee9 100644 --- a/Simulations/MassSpringSystemSimulator.h +++ b/Simulations/MassSpringSystemSimulator.h @@ -38,6 +38,11 @@ public: Vec3 getPositionOfMassPoint(int index); Vec3 getVelocityOfMassPoint(int index); void applyExternalForce(Vec3 force); + + Vec3 calcualtePositionTimestepEuler(Vec3 oldPosition, float timestep, Vec3 veloctiy); + Vec3 calcualteVelocityTimestepEuler(Vec3 oldVelocity, float timestep, Vec3 acceleration); + Vec3 calculateAcceleration(Vec3 acceleration, float mass); + void Euler(int index1, int index2, int indexSpring, float timestep); // Do Not Change void setIntegrator(int integrator) {