merge stuff demo 2

This commit is contained in:
2023-11-08 15:19:29 +01:00
parent 03ebc33ec6
commit 82f103758d
2 changed files with 53 additions and 7 deletions

View File

@@ -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;
}

View File

@@ -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) {