fix midpoint

This commit is contained in:
2023-11-17 14:22:14 +01:00
parent b857996445
commit 1296ef0b39
2 changed files with 23 additions and 28 deletions

View File

@@ -3,10 +3,6 @@
MassSpringSystemSimulator::MassSpringSystemSimulator() MassSpringSystemSimulator::MassSpringSystemSimulator()
{ {
m_iTestCase = 0;
m_fMass = 10;
m_fStiffness = 40;
m_iIntegrator = EULER;
} }
const char* MassSpringSystemSimulator::getTestCasesStr() const char* MassSpringSystemSimulator::getTestCasesStr()
@@ -310,43 +306,41 @@ void MassSpringSystemSimulator::Midpoint(Spring& spring, float timestep) {
auto massPoint1 = spring.mp1.lock(); auto massPoint1 = spring.mp1.lock();
auto massPoint2 = spring.mp2.lock(); auto massPoint2 = spring.mp2.lock();
//old Velocity
auto mOld_v = massPoint1->velocity;
auto m2Old_v = massPoint2->velocity;
Vec3 PosVector = massPoint1->position - massPoint2->position; Vec3 PosVector = massPoint1->position - massPoint2->position;
//Abstand ausrechnen //Abstand ausrechnen
float length = LengthCalculator(PosVector); float length = sqrtf(PosVector.x * PosVector.x + PosVector.y * PosVector.y + PosVector.z * PosVector.z);
//normalize //normalize
Vec3 normal1 = PosVector / length; Vec3 normal = PosVector / length;
Vec3 normal2 = -1 * normal1;
//Midpoint calculator //Midpoint calculator
auto midpointStep1 = MidPointStep(timestep / 2, spring, massPoint1->position, massPoint1->velocity, normal1, length); std::tuple<Vec3, Vec3> midpointStep1 = MidPointHalfStep(timestep * .5, spring, massPoint1->position, massPoint1->velocity, normal, length);
auto midpointStep2 = MidPointStep(timestep / 2, spring, massPoint2->position, massPoint2->velocity, normal2, 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); Vec3 newPosVector = std::get<0>(midpointStep1) - std::get<0>(midpointStep2);
auto newLength = LengthCalculator(newPosVector); float newLength = sqrtf(newPosVector.x * newPosVector.x + newPosVector.y * newPosVector.y + newPosVector.z * newPosVector.z);
auto newNormal1 = newPosVector / newLength; Vec3 newNormal = newPosVector / newLength;
auto newNormal2 = -1 * newNormal1;
auto midpoint1 = MidPointStep(timestep, spring, massPoint1->position, std::get<1>(midpointStep1), newNormal1, newLength); std::tuple<Vec3, Vec3> midpoint1 = MidPointStep(timestep, spring, massPoint1->position, massPoint1->velocity, std::get<1>(midpointStep1), newNormal, newLength);
auto midpoint2 = MidPointStep(timestep, spring, massPoint2->position, std::get<1>(midpointStep2), newNormal2, 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->position = std::get<0>(midpoint1);
massPoint1->velocity = std::get<1>(midpoint1); massPoint1->velocity = std::get<1>(midpoint1);
massPoint2->position = std::get<0>(midpoint2); massPoint2->position = std::get<0>(midpoint2);
massPoint2->velocity = std::get<1>(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; return MidPointStep(timestep, spring, position, velocity, velocity, normal, length);
Vec3 acc = calculateAcceleration(force, m_fMass); }
Vec3 pos = position + timestep * velocity;
Vec3 vel = velocity + timestep * acc;
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); return std::tuple<Vec3, Vec3>(pos, vel);
} }

View File

@@ -40,7 +40,8 @@ public:
void applyExternalForce(Vec3 force); void applyExternalForce(Vec3 force);
void Midpoint(Spring& spring, float timestep); 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); float LengthCalculator(Vec3 vector);
Vec3 calculatePositionTimestepEuler(Vec3 oldPosition, float timestep, Vec3 veloctiy); Vec3 calculatePositionTimestepEuler(Vec3 oldPosition, float timestep, Vec3 veloctiy);