diff --git a/C++/Verlet Cloth/main.cpp b/C++/Verlet Cloth/main.cpp
index d5918f8..07fed84 100644
--- a/C++/Verlet Cloth/main.cpp
+++ b/C++/Verlet Cloth/main.cpp
@@ -8,6 +8,7 @@
const float GRAVITY = 5.81f;
const float DRAG = 0.99f;
+struct Link;
struct Vec2f {
float x, y;
};
@@ -15,10 +16,14 @@ struct Vec2f {
struct MassPoint {
Vec2f sPosition;
bool bLocked = false; // If true, the point will not be able to move
+ float fMass;
- MassPoint(Vec2f sPos)
+ std::vector sLinks;
+
+ MassPoint(Vec2f sPos, float fMass)
: sPosition(sPos),
- sLastPosition(sPos) { }
+ sLastPosition(sPos),
+ fMass(fMass) { }
void step() {
if (bLocked) return;
@@ -33,13 +38,47 @@ struct MassPoint {
sLastPosition.x = sPosition.x;
sLastPosition.y = sPosition.y;
}
+
+ void solve() {
+
+ }
private:
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 {
public:
std::vector sPoints;
+ int iConstraintAccuracy;
VerletCloth() {
sAppName = "Verlet Cloth Simulation";
@@ -47,7 +86,9 @@ public:
bool OnUserCreate() override {
- sPoints.push_back({{ 1.0f, 1.0f }});
+ sPoints.push_back({{ 1.0f, 1.0f }, 1.0f});
+
+ iConstraintAccuracy = 5;
return true;
}
@@ -60,6 +101,12 @@ public:
Clear(olc::WHITE);
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){
@@ -80,6 +127,13 @@ public:
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:
float m_fTimeCounter = 0.0f;
};
diff --git a/C++/Verlet Cloth/output b/C++/Verlet Cloth/output
index 77e7d1c..40f823f 100755
Binary files a/C++/Verlet Cloth/output and b/C++/Verlet Cloth/output differ