clean up, add print method for springs, change Euler Function declaration, add demo, demo2 demo3 setup
This commit is contained in:
@@ -1,16 +1,12 @@
|
|||||||
#include "MassSpringSystemSimulator.h"
|
#include "MassSpringSystemSimulator.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
MassSpringSystemSimulator::MassSpringSystemSimulator()
|
MassSpringSystemSimulator::MassSpringSystemSimulator()
|
||||||
{
|
{
|
||||||
m_iTestCase = 0;
|
m_iTestCase = 0;
|
||||||
m_fMass = 10;
|
m_fMass = 10;
|
||||||
m_fStiffness = 40;
|
m_fStiffness = 40;
|
||||||
int m_iIntegrater = 0;
|
m_iIntegrator = EULER;
|
||||||
|
|
||||||
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()
|
const char* MassSpringSystemSimulator::getTestCasesStr()
|
||||||
@@ -23,13 +19,14 @@ void MassSpringSystemSimulator::initUI(DrawingUtilitiesClass* DUC)
|
|||||||
this->DUC = DUC;
|
this->DUC = DUC;
|
||||||
switch (m_iTestCase)
|
switch (m_iTestCase)
|
||||||
{
|
{
|
||||||
case 0:break;
|
case 0:
|
||||||
case 1:
|
break;
|
||||||
|
case 1:
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
break;
|
break;
|
||||||
default:break;
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,10 +35,11 @@ void MassSpringSystemSimulator::reset()
|
|||||||
m_mouse.x = m_mouse.y = 0;
|
m_mouse.x = m_mouse.y = 0;
|
||||||
m_trackmouse.x = m_trackmouse.y = 0;
|
m_trackmouse.x = m_trackmouse.y = 0;
|
||||||
m_oldtrackmouse.x = m_oldtrackmouse.y = 0;
|
m_oldtrackmouse.x = m_oldtrackmouse.y = 0;
|
||||||
|
|
||||||
|
springs.clear();
|
||||||
|
masspoints.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void MassSpringSystemSimulator::drawFrame(ID3D11DeviceContext* pd3dImmediateContext)
|
void MassSpringSystemSimulator::drawFrame(ID3D11DeviceContext* pd3dImmediateContext)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -68,26 +66,91 @@ void MassSpringSystemSimulator::drawFrame(ID3D11DeviceContext* pd3dImmediateCont
|
|||||||
void MassSpringSystemSimulator::notifyCaseChanged(int testCase)
|
void MassSpringSystemSimulator::notifyCaseChanged(int testCase)
|
||||||
{
|
{
|
||||||
m_iTestCase = testCase;
|
m_iTestCase = testCase;
|
||||||
|
system("cls");
|
||||||
|
reset();
|
||||||
|
|
||||||
switch (m_iTestCase)
|
switch (m_iTestCase)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0: {
|
||||||
cout << "Demo1 !\n";
|
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(0.005);
|
||||||
|
printSpring(springs.at(0));
|
||||||
|
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;
|
break;
|
||||||
case 1:
|
}
|
||||||
cout << "Demo2 \n";
|
|
||||||
//m_iNumSpheres = 100;
|
case 2: {
|
||||||
//m_fSphereSize = 0.05f;
|
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;
|
break;
|
||||||
case 2:
|
}
|
||||||
cout << "Demo3 !\n";
|
case 3: {
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
cout << "Demo 4 !\n";
|
cout << "Demo 4 !\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
//cout << "Demo4 !\n";
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,28 +178,32 @@ void MassSpringSystemSimulator::externalForcesCalculations(float timeElapsed)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void MassSpringSystemSimulator::simulateTimestep(float timeStep)
|
void MassSpringSystemSimulator::simulateTimestep(float timeStep)
|
||||||
{
|
{
|
||||||
//update current setup for each frame
|
//update current setup for each frame
|
||||||
switch (m_iTestCase) {
|
for (size_t i = 0; i < springs.size(); i++) {
|
||||||
case 0:
|
auto sp = springs.at(i);
|
||||||
//update the masspoint
|
if (!sp.isValid())
|
||||||
cout << "Euler \n";
|
{
|
||||||
|
springs.erase(springs.begin() + i);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto mp1 = sp.mp1.lock();
|
||||||
|
auto mp2 = sp.mp2.lock();
|
||||||
|
|
||||||
|
|
||||||
|
if (m_iIntegrator == EULER) {
|
||||||
|
|
||||||
|
Euler(*mp1.get(), *mp2.get(), sp, timeStep);
|
||||||
|
}
|
||||||
|
else if (m_iIntegrator == MIDPOINT) {
|
||||||
|
//TODO: Add Midpoint
|
||||||
|
}
|
||||||
|
else if (m_iIntegrator == LEAPFROG) {
|
||||||
|
//TODO: Add Leapfrog
|
||||||
|
}
|
||||||
|
|
||||||
cout << "Midpoint\n";
|
|
||||||
break;
|
|
||||||
case 1:cout << "demo 2 \n";
|
|
||||||
break;
|
|
||||||
case 2:cout << "demo 3\n";
|
|
||||||
break;
|
|
||||||
case 3: cout << "demo 4\n";
|
|
||||||
break;
|
|
||||||
default: break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Euler(0, 1, 0, timeStep);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MassSpringSystemSimulator::onClick(int x, int y)
|
void MassSpringSystemSimulator::onClick(int x, int y)
|
||||||
@@ -234,28 +301,35 @@ Vec3 MassSpringSystemSimulator::calculateAcceleration(Vec3 force, float mass)
|
|||||||
return force / mass;
|
return force / mass;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MassSpringSystemSimulator::Euler(int index1, int index2, int indexSpring, float timestep)
|
void MassSpringSystemSimulator::Euler(MassPoint& masspoint1, MassPoint& masspoint2, Spring& spring, float timestep)
|
||||||
{
|
{
|
||||||
//take old position and send to calculatePositionTimestepEuler
|
//take old position and send to calculatePositionTimestepEuler
|
||||||
auto mp = masspoints.at(index1);
|
auto PosVector = masspoint1.position - masspoint2.position;
|
||||||
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 lengthVector = sqrt(PosVector.x * PosVector.x + PosVector.y * PosVector.y + PosVector.z * PosVector.z);
|
||||||
auto normalized = PosVector / lengthVector;
|
auto normalized = PosVector / lengthVector;
|
||||||
|
|
||||||
// Actual Calculation
|
// Actual Calculation
|
||||||
// Force of spring is -k * (l - L) * normalizedVector [for P2 we can take -F1)
|
// 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 foreP2 = -1 * force;
|
||||||
auto veloc = calcualteVelocityTimestepEuler(mp->velocity, timestep, calculateAcceleration(force, 10.));
|
auto veloc = calcualteVelocityTimestepEuler(masspoint1.velocity, timestep, calculateAcceleration(force, 10.));
|
||||||
auto pos = calcualtePositionTimestepEuler(mp->position, timestep, veloc);
|
auto pos = calcualtePositionTimestepEuler(masspoint1.position, timestep, veloc);
|
||||||
|
|
||||||
auto veloc2 = calcualteVelocityTimestepEuler(mp2->velocity, timestep, calculateAcceleration(foreP2, 10.));
|
auto veloc2 = calcualteVelocityTimestepEuler(masspoint2.velocity, timestep, calculateAcceleration(foreP2, 10.));
|
||||||
auto pos2 = calcualtePositionTimestepEuler(mp2->position, timestep, veloc2);
|
auto pos2 = calcualtePositionTimestepEuler(masspoint2.position, timestep, veloc2);
|
||||||
|
|
||||||
// Update Positions and Velocity
|
// Update Positions and Velocity
|
||||||
mp->position = pos;
|
masspoint1.position = pos;
|
||||||
mp->velocity = veloc;
|
masspoint1.velocity = veloc;
|
||||||
mp2->position = pos2;
|
|
||||||
mp2->velocity = veloc2;
|
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());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,9 @@ public:
|
|||||||
Vec3 calcualtePositionTimestepEuler(Vec3 oldPosition, float timestep, Vec3 veloctiy);
|
Vec3 calcualtePositionTimestepEuler(Vec3 oldPosition, float timestep, Vec3 veloctiy);
|
||||||
Vec3 calcualteVelocityTimestepEuler(Vec3 oldVelocity, float timestep, Vec3 acceleration);
|
Vec3 calcualteVelocityTimestepEuler(Vec3 oldVelocity, float timestep, Vec3 acceleration);
|
||||||
Vec3 calculateAcceleration(Vec3 acceleration, float mass);
|
Vec3 calculateAcceleration(Vec3 acceleration, float mass);
|
||||||
void Euler(int index1, int index2, int indexSpring, float timestep);
|
void Euler(MassPoint& masspoint1, MassPoint& masspoint2, Spring& spring, float timestep);
|
||||||
|
|
||||||
|
void printSpring(const Spring& spring);
|
||||||
|
|
||||||
// Do Not Change
|
// Do Not Change
|
||||||
void setIntegrator(int integrator) {
|
void setIntegrator(int integrator) {
|
||||||
|
|||||||
Reference in New Issue
Block a user