From 9316f35eeedd0ec5c121684757a93044e0e1a449 Mon Sep 17 00:00:00 2001 From: Lukas Moungos Date: Thu, 16 Nov 2023 21:25:14 +0100 Subject: [PATCH] revert b1c4939b16a9ae408160e0d0d3dcd2a4872a1cb4 revert test --- Simulations/MassSpringSystemSimulator.cpp | 208 +++++++++++++++------- 1 file changed, 141 insertions(+), 67 deletions(-) diff --git a/Simulations/MassSpringSystemSimulator.cpp b/Simulations/MassSpringSystemSimulator.cpp index 7844f82..44bea13 100644 --- a/Simulations/MassSpringSystemSimulator.cpp +++ b/Simulations/MassSpringSystemSimulator.cpp @@ -1,22 +1,17 @@ #include "MassSpringSystemSimulator.h" +#include MassSpringSystemSimulator::MassSpringSystemSimulator() { m_iTestCase = 0; m_fMass = 10; m_fStiffness = 40; - int m_iIntegrater = 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_iIntegrator = EULER; } const char* MassSpringSystemSimulator::getTestCasesStr() { - //hier to change the choices - return "Euler,LeapFrog,Midpoint"; + return "Demo1,Demo2,Demo3,Demo4"; } void MassSpringSystemSimulator::initUI(DrawingUtilitiesClass* DUC) @@ -24,13 +19,14 @@ void MassSpringSystemSimulator::initUI(DrawingUtilitiesClass* DUC) this->DUC = DUC; switch (m_iTestCase) { - case 0:break; - case 1: - - break; - case 2: - break; - default:break; + case 0: + break; + case 1: + break; + case 2: + break; + default: + break; } } @@ -39,10 +35,11 @@ void MassSpringSystemSimulator::reset() m_mouse.x = m_mouse.y = 0; m_trackmouse.x = m_trackmouse.y = 0; m_oldtrackmouse.x = m_oldtrackmouse.y = 0; + + springs.clear(); + masspoints.clear(); } - - void MassSpringSystemSimulator::drawFrame(ID3D11DeviceContext* pd3dImmediateContext) { @@ -69,24 +66,91 @@ void MassSpringSystemSimulator::drawFrame(ID3D11DeviceContext* pd3dImmediateCont void MassSpringSystemSimulator::notifyCaseChanged(int testCase) { m_iTestCase = testCase; + system("cls"); + reset(); + switch (m_iTestCase) { - case 0: - cout << "Euler !\n"; + case 0: { + cout << "Demo 1 !\n"; + setMass(10); + setStiffness(40); -//simulateTimestep(1); + int first = addMassPoint(Vec3(0, 0, 0), Vec3(-1, 0, 0), true); + int second = addMassPoint(Vec3(0, 2, 0), Vec3(1, 0, 0), true); + addSpring(first, second, 1); + cout << "\t -- INITIAL --\n"; + printSpring(springs.at(0)); + cout << "--------------------------------------------------" << std::endl; + + //calculate Euler for one step and print results + setIntegrator(EULER); + cout << "\n\n\t -- EULER RESULT --\n"; + simulateTimestep(1); + printSpring(springs.at(0)); + + cout << "--------------------------------------------------" << std::endl; + + reset(); + first = addMassPoint(Vec3(0, 0, 0), Vec3(-1, 0, 0), true); + second = addMassPoint(Vec3(0, 2, 0), Vec3(1, 0, 0), true); + + addSpring(first, second, 1); + + //calculate Midpoint for one step and print results + setIntegrator(MIDPOINT); + cout << "\n\n\t -- MIDPOINT RESULT --\n"; + simulateTimestep(1); + printSpring(springs.at(0)); break; - case 1: - break; - case 2: - cout << "Midpoint \n"; - //m_iNumSpheres = 100; - //m_fSphereSize = 0.05f; - break; + } + case 1: { + cout << "Demo 2 !\n"; + reset(); + int first = addMassPoint(Vec3(0, 0, 0), Vec3(-1, 0, 0), true); + int second = addMassPoint(Vec3(0, 2, 0), Vec3(1, 0, 0), true); + addSpring(first, second, 1.0); + + cout << "\t -- INITIAL --\n"; + printSpring(springs.at(0)); + + cout << "--------------------------------------------------" << std::endl; + + //calculate Euler for a timestep of 0.005 and print results + setIntegrator(EULER); + cout << "\n\n\t -- EULER RESULT--\n"; + simulateTimestep(0.005); + printSpring(springs.at(0)); + + break; + } + + case 2: { + cout << "Demo 3 !\n"; + reset(); + int first = addMassPoint(Vec3(0, 0, 0), Vec3(-1, 0, 0), true); + int second = addMassPoint(Vec3(0, 2, 0), Vec3(1, 0, 0), true); + addSpring(first, second, 1.0); + + cout << "\t -- INITIAL --\n"; + printSpring(springs.at(0)); + + cout << "--------------------------------------------------" << std::endl; + + //calculate Midpoint for a timestep of 0.005 and print results + setIntegrator(MIDPOINT); + cout << "\n\n\t -- MIDPOINT RESULT --\n"; + simulateTimestep(0.005); + printSpring(springs.at(0)); + break; + } + case 3: { + cout << "Demo 4 !\n"; + break; + } default: - //cout << "Demo4 !\n"; break; } } @@ -114,29 +178,29 @@ void MassSpringSystemSimulator::externalForcesCalculations(float timeElapsed) } - - void MassSpringSystemSimulator::simulateTimestep(float timeStep) { //update current setup for each frame - switch (m_iTestCase) { - case 0: - //update the masspoint - cout << "Euler \n"; - Euler(0, 1, 0, timeStep); + for (size_t i = 0; i < springs.size(); i++) { + auto sp = springs.at(i); + if (!sp.isValid()) + { + springs.erase(springs.begin() + i); + continue; + } - break; - case 1: - break; + if (m_iIntegrator == EULER) { + + Euler(sp, timeStep); + } + else if (m_iIntegrator == MIDPOINT) { + Midpoint(sp, timeStep); + } + else if (m_iIntegrator == LEAPFROG) { + //TODO: Add Leapfrog + } - case 2:cout << "midpoint \n"; - Midpoint(0, 1, timeStep); - break; - - default: break; } - - //Euler(0, 1, 0, timeStep); } void MassSpringSystemSimulator::onClick(int x, int y) @@ -245,16 +309,17 @@ Vec3 MassSpringSystemSimulator::calculateAcceleration(Vec3 force, float mass) return force / mass; } -void MassSpringSystemSimulator::Midpoint(int index1, int index2, float timestep) { +void MassSpringSystemSimulator::Midpoint(Spring& spring, float timestep) { //here some implementation about Midpoint - auto massPoint1 = masspoints.at(index1); - auto massPoint2 = masspoints.at(index2); + auto massPoint1 = spring.mp1.lock(); + auto massPoint2 = spring.mp2.lock(); + //old position auto mp = massPoint1->position; - auto mp2 = massPoint2 ->position; + auto mp2 = massPoint2->position; //old Velocity auto mOld_v = massPoint1->velocity; - auto m2Old_v = massPoint1->velocity; + auto m2Old_v = massPoint2->velocity; Vec3 PosVector = mp - mp2; //Abstand ausrechnen @@ -263,7 +328,7 @@ void MassSpringSystemSimulator::Midpoint(int index1, int index2, float timestep) Vec3 PosNorm1 = PosVector / d; Vec3 PosNorm2 = -1 * PosNorm1; - Vec3 Force = -m_fStiffness * (d - springs.at(0).initialLength) * PosNorm1; + Vec3 Force = -m_fStiffness * (d - spring.initialLength) * PosNorm1; Vec3 Force2 = -1 * Force; Vec3 oldAcc = calculateAcceleration(Force,m_fMass); @@ -282,37 +347,46 @@ void MassSpringSystemSimulator::Midpoint(int index1, int index2, float timestep) Vec3 NewVel = mOld_v + timestep * oldAcc; Vec3 NewVel2 = m2Old_v + timestep * oldAcc2; - - cout << NewPos; - cout << NewVel; + //cout << NewPos; + //cout << NewVel; massPoint1->position = NewPos; massPoint1->velocity = NewVel; massPoint2->position = NewPos2; massPoint2->velocity = NewVel2; } -void MassSpringSystemSimulator::Euler(int index1, int index2, int indexSpring, float timestep) +void MassSpringSystemSimulator::Euler(Spring& spring, float timestep) { + auto massPoint1 = spring.mp1.lock(); + auto massPoint2 = spring.mp2.lock(); + //take old position and send to calculatePositionTimestepEuler - auto mp = masspoints.at(index1); - auto mp2 = masspoints.at(index2); - Vec3 PosVector = mp->position - mp2->position; + auto PosVector = massPoint1->position - massPoint2->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 force = -m_fStiffness * (lengthVector - spring.initialLength) * normalized; auto foreP2 = -1 * force; - auto veloc = calculateVelocityTimestepEuler(mp->velocity, timestep, calculateAcceleration(force, 10.)); - auto pos = calculatePositionTimestepEuler(mp->position, timestep, veloc); + auto veloc = calculatePositionTimestepEuler(massPoint1->velocity, timestep, calculateAcceleration(force, 10.)); + auto pos = calculatePositionTimestepEuler(massPoint1->position, timestep, veloc); - auto veloc2 = calculateVelocityTimestepEuler(mp2->velocity, timestep, calculateAcceleration(foreP2, 10.)); - auto pos2 = calculatePositionTimestepEuler(mp2->position, timestep, veloc2); + auto veloc2 = calculateVelocityTimestepEuler(massPoint2->velocity, timestep, calculateAcceleration(foreP2, 10.)); + auto pos2 = calculatePositionTimestepEuler(massPoint2->position, timestep, veloc2); // Update Positions and Velocity - mp->position = pos; - mp->velocity = veloc; - mp2->position = pos2; - mp2->velocity = veloc2; + massPoint1->position = pos; + massPoint1->velocity = veloc; + + massPoint2->position = pos2; + massPoint2->velocity = veloc2; +} + +void MassSpringSystemSimulator::printSpring(const Spring& spring) +{ + auto mp1 = spring.mp1.lock(); + auto mp2 = spring.mp2.lock(); + printf("Masspoint 1:\nPosition: %s \nVelocity: %s\n\n", mp1->position.toString().c_str(), mp1->velocity.toString().c_str()); + printf("Masspoint 2:\nPosition: %s \nVelocity: %s\n", mp2->position.toString().c_str(), mp2->velocity.toString().c_str()); }