This commit is contained in:
Ben
2019-03-29 23:05:55 +00:00
parent 0e6ad651cc
commit adae1dafa9
2 changed files with 57 additions and 3 deletions

View File

@@ -8,6 +8,7 @@
const float GRAVITY = 5.81f; const float GRAVITY = 5.81f;
const float DRAG = 0.99f; const float DRAG = 0.99f;
struct Link;
struct Vec2f { struct Vec2f {
float x, y; float x, y;
}; };
@@ -15,10 +16,14 @@ struct Vec2f {
struct MassPoint { struct MassPoint {
Vec2f sPosition; Vec2f sPosition;
bool bLocked = false; // If true, the point will not be able to move bool bLocked = false; // If true, the point will not be able to move
float fMass;
MassPoint(Vec2f sPos) std::vector<Link*> sLinks;
MassPoint(Vec2f sPos, float fMass)
: sPosition(sPos), : sPosition(sPos),
sLastPosition(sPos) { } sLastPosition(sPos),
fMass(fMass) { }
void step() { void step() {
if (bLocked) return; if (bLocked) return;
@@ -33,13 +38,47 @@ struct MassPoint {
sLastPosition.x = sPosition.x; sLastPosition.x = sPosition.x;
sLastPosition.y = sPosition.y; sLastPosition.y = sPosition.y;
} }
void solve() {
}
private: private:
Vec2f sLastPosition; Vec2f sLastPosition;
}; };
struct Link {
float fRestingDistance;
float fStiffness;
float fTear;
MassPoint* p0;
MassPoint* p1;
void solve() {
float diffX = p0->sPosition.x - p1->sPosition.x;
float diffY = p0->sPosition.y - p1->sPosition.y;
float d = sqrt(diffX * diffX + diffY * diffY);
float difference = (fRestingDistance - d) / d;
float im1 = 1 / p0->fMass;
float im2 = 1 / p1->fMass;
float scalarP1 = (im1 / (im1 + im2)) * fStiffness;
float scalarP2 = fStiffness - scalarP1;
p0->sPosition.x += diffX * scalarP1 * difference;
p0->sPosition.y += diffY * scalarP1 * difference;
p1->sPosition.x -= diffX * scalarP2 * difference;
p1->sPosition.y -= diffY * scalarP2 * difference;
}
};
class VerletCloth : public olc::PixelGameEngine { class VerletCloth : public olc::PixelGameEngine {
public: public:
std::vector<MassPoint> sPoints; std::vector<MassPoint> sPoints;
int iConstraintAccuracy;
VerletCloth() { VerletCloth() {
sAppName = "Verlet Cloth Simulation"; sAppName = "Verlet Cloth Simulation";
@@ -47,7 +86,9 @@ public:
bool OnUserCreate() override { bool OnUserCreate() override {
sPoints.push_back({{ 1.0f, 1.0f }}); sPoints.push_back({{ 1.0f, 1.0f }, 1.0f});
iConstraintAccuracy = 5;
return true; return true;
} }
@@ -60,6 +101,12 @@ public:
Clear(olc::WHITE); Clear(olc::WHITE);
m_fTimeCounter = 0.0f; m_fTimeCounter = 0.0f;
for (int x = 0; x < iConstraintAccuracy; x++) {
for (int i = 0; i < sPoints.size(); i++) {
MassPoint pointmass = sPoints[i];
pointmass.solve();
}
}
for (auto& sPoint : sPoints){ for (auto& sPoint : sPoints){
@@ -80,6 +127,13 @@ public:
return true; return true;
} }
void DrawLink(Link& link) {
DrawLine(link.p0->sPosition.x, link.p0->sPosition.y,
link.p1->sPosition.x, link.p1->sPosition.y,
olc::RED);
}
private: private:
float m_fTimeCounter = 0.0f; float m_fTimeCounter = 0.0f;
}; };

Binary file not shown.