diff --git a/Simulations/MassSpringSystemSimulator.cpp b/Simulations/MassSpringSystemSimulator.cpp index 54c2df7..38a2a8d 100644 --- a/Simulations/MassSpringSystemSimulator.cpp +++ b/Simulations/MassSpringSystemSimulator.cpp @@ -3,10 +3,6 @@ MassSpringSystemSimulator::MassSpringSystemSimulator() { - m_iTestCase = 0; - m_fMass = 10; - m_fStiffness = 40; - m_iIntegrator = EULER; } const char* MassSpringSystemSimulator::getTestCasesStr() @@ -310,44 +306,42 @@ void MassSpringSystemSimulator::Midpoint(Spring& spring, float timestep) { auto massPoint1 = spring.mp1.lock(); auto massPoint2 = spring.mp2.lock(); - //old Velocity - auto mOld_v = massPoint1->velocity; - auto m2Old_v = massPoint2->velocity; - Vec3 PosVector = massPoint1->position - massPoint2->position; //Abstand ausrechnen - float length = LengthCalculator(PosVector); + float length = sqrtf(PosVector.x * PosVector.x + PosVector.y * PosVector.y + PosVector.z * PosVector.z); //normalize - Vec3 normal1 = PosVector / length; - Vec3 normal2 = -1 * normal1; + Vec3 normal = PosVector / length; //Midpoint calculator - auto midpointStep1 = MidPointStep(timestep / 2, spring, massPoint1->position, massPoint1->velocity, normal1, length); - auto midpointStep2 = MidPointStep(timestep / 2, spring, massPoint2->position, massPoint2->velocity, normal2, length); + std::tuple midpointStep1 = MidPointHalfStep(timestep * .5, spring, massPoint1->position, massPoint1->velocity, normal, length); + std::tuple midpointStep2 = MidPointHalfStep(timestep * .5, spring, massPoint2->position, massPoint2->velocity, -normal, length); - auto newPosVector = std::get<0>(midpointStep1) - std::get<0>(midpointStep2); - auto newLength = LengthCalculator(newPosVector); - auto newNormal1 = newPosVector / newLength; - auto newNormal2 = -1 * newNormal1; + Vec3 newPosVector = std::get<0>(midpointStep1) - std::get<0>(midpointStep2); + float newLength = sqrtf(newPosVector.x * newPosVector.x + newPosVector.y * newPosVector.y + newPosVector.z * newPosVector.z); + Vec3 newNormal = newPosVector / newLength; - auto midpoint1 = MidPointStep(timestep, spring, massPoint1->position, std::get<1>(midpointStep1), newNormal1, newLength); - auto midpoint2 = MidPointStep(timestep, spring, massPoint2->position, std::get<1>(midpointStep2), newNormal2, newLength); + std::tuple midpoint1 = MidPointStep(timestep, spring, massPoint1->position, massPoint1->velocity, std::get<1>(midpointStep1), newNormal, newLength); + std::tuple midpoint2 = MidPointStep(timestep, spring, massPoint2->position, massPoint2->velocity, std::get<1>(midpointStep2), -newNormal, newLength); - //cout << NewPos; - //cout << NewVel; massPoint1->position = std::get<0>(midpoint1); massPoint1->velocity = std::get<1>(midpoint1); massPoint2->position = std::get<0>(midpoint2); massPoint2->velocity = std::get<1>(midpoint2); } -std::tuple MassSpringSystemSimulator::MidPointStep(float timestep, const Spring& spring, Vec3 position, Vec3 velocity, Vec3 normal, float length) +std::tuple MassSpringSystemSimulator::MidPointHalfStep(double timestep, const Spring& spring, Vec3 position, Vec3 velocity, Vec3 normal, double length) { - Vec3 force = -m_fStiffness * (length - spring.initialLength) * normal; - Vec3 acc = calculateAcceleration(force, m_fMass); - Vec3 pos = position + timestep * velocity; - Vec3 vel = velocity + timestep * acc; - return std::tuple(pos, vel); + return MidPointStep(timestep, spring, position, velocity, velocity, normal, length); +} + + +std::tuple MassSpringSystemSimulator::MidPointStep(double timestep, const Spring& spring, Vec3 position, Vec3 oldVelo,Vec3 velocity, Vec3 normal, double length) +{ + auto force = -m_fStiffness * (length - spring.initialLength) * normal; + auto acc = calculateAcceleration(force, m_fMass); + auto pos = position + timestep * velocity; + auto vel = oldVelo + timestep * acc; + return std::tuple(pos, vel); } void MassSpringSystemSimulator::Euler(Spring& spring, float timestep) diff --git a/Simulations/MassSpringSystemSimulator.h b/Simulations/MassSpringSystemSimulator.h index cb32802..f0c6dcc 100644 --- a/Simulations/MassSpringSystemSimulator.h +++ b/Simulations/MassSpringSystemSimulator.h @@ -40,7 +40,8 @@ public: void applyExternalForce(Vec3 force); void Midpoint(Spring& spring, float timestep); - std::tuple MidPointStep(float timestep, const Spring& spring, Vec3 position, Vec3 velocity, Vec3 normal, float length); + std::tuple MidPointHalfStep(double timestep, const Spring& spring, Vec3 position, Vec3 velocity, Vec3 normal, double length); + std::tuple MassSpringSystemSimulator::MidPointStep(double timestep, const Spring& spring, Vec3 position, Vec3 oldVelo, Vec3 velocity, Vec3 normal, double length); float LengthCalculator(Vec3 vector); Vec3 calculatePositionTimestepEuler(Vec3 oldPosition, float timestep, Vec3 veloctiy);