Compare commits

1 Commits

Author SHA1 Message Date
1296ef0b39 fix midpoint 2023-11-17 14:22:14 +01:00
2 changed files with 23 additions and 28 deletions

View File

@@ -3,10 +3,6 @@
MassSpringSystemSimulator::MassSpringSystemSimulator()
{
m_iTestCase = 0;
m_fMass = 10;
m_fStiffness = 40;
m_iIntegrator = EULER;
}
const char* MassSpringSystemSimulator::getTestCasesStr()
@@ -310,43 +306,41 @@ 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<Vec3, Vec3> midpointStep1 = MidPointHalfStep(timestep * .5, spring, massPoint1->position, massPoint1->velocity, normal, length);
std::tuple<Vec3, Vec3> 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<Vec3, Vec3> midpoint1 = MidPointStep(timestep, spring, massPoint1->position, massPoint1->velocity, std::get<1>(midpointStep1), newNormal, newLength);
std::tuple<Vec3, Vec3> 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<Vec3, Vec3> MassSpringSystemSimulator::MidPointStep(float timestep, const Spring& spring, Vec3 position, Vec3 velocity, Vec3 normal, float length)
std::tuple<Vec3, Vec3> 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 MidPointStep(timestep, spring, position, velocity, velocity, normal, length);
}
std::tuple<Vec3, Vec3> 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<Vec3, Vec3>(pos, vel);
}

View File

@@ -40,7 +40,8 @@ public:
void applyExternalForce(Vec3 force);
void Midpoint(Spring& spring, float timestep);
std::tuple<Vec3, Vec3> MidPointStep(float timestep, const Spring& spring, Vec3 position, Vec3 velocity, Vec3 normal, float length);
std::tuple<Vec3, Vec3> MidPointHalfStep(double timestep, const Spring& spring, Vec3 position, Vec3 velocity, Vec3 normal, double length);
std::tuple<Vec3, Vec3> 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);