Browse Source

Add the softbody sprint demo

Godzil 1 year ago
parent
commit
a2aacf3033
2 changed files with 32 additions and 14 deletions
  1. 29 11
      source/app.cpp
  2. 3 3
      source/include/app.h

+ 29 - 11
source/app.cpp

@@ -29,9 +29,12 @@ void application::setup()
     this->running = graphics::openWindow();
     this->millisecsPreviousFrame = SDL_GetTicks();
 
-    this->anchor = vec2(graphics::width() / 2.0,30);
-    this->particles.push_back(new particle(graphics::width() / 2.0, graphics::height() / 2.0,
-                                       2.0, 10));
+    for(int i = 0; i < this->numberOfCorners; i++)
+    {
+        this->particles.push_back(new particle(graphics::width() / 2.0 + i * 5,
+                                               graphics::height() / 2.0 + i * 5,
+                                               2, 10));
+    }
 }
 
 void application::input()
@@ -147,16 +150,24 @@ void application::update()
         vec2 friction = force::generateFrictionForce(*part, 20);
         part->addForce(friction);
 
-        vec2 drag = force::generateDragForce(*part, 0.001);
+        vec2 drag = force::generateDragForce(*part, 0.002);
         part->addForce(drag);
 
         vec2 weight = vec2(0, part->mass * 9.81 * PIXELS_PER_METER);
         part->addForce(weight);
     }
 
-    vec2 spring = force::generateSpringForce(*this->particles[0],
-                                             this->anchor, this->restLenght, this->k);
-    this->particles[0]->addForce(spring);
+    for(int i = 0; i < this->numberOfCorners; i++)
+    {
+        particle *a = this->particles[i];
+        for(int j = i + 1; j < this->numberOfCorners; j++)
+        {
+            particle *b = this->particles[j];
+            vec2 spring = force::generateSpringForce(*a, *b, this->restLenght, this->k);
+            a->addForce(spring);
+            b->addForce( -spring);
+        }
+    }
 
     for (auto part:this->particles)
     {
@@ -211,10 +222,17 @@ void application::render()
                              this->mouseCursor.x, this->mouseCursor.y, 0xFFFFFFFF);
     }
 
-    graphics::draw::line(this->anchor.x, this->anchor.y,
-                         this->particles[0]->position.x, this->particles[0]->position.y,
-                         0xFF313131);
-    graphics::draw::fillCircle(this->anchor.x, this->anchor.y, 5, 0xFF001155);
+    for(int i = 0; i < this->numberOfCorners; i++)
+    {
+        particle *a = this->particles[i];
+        for(int j = i + 1; j < this->numberOfCorners; j++)
+        {
+            particle *b = this->particles[j];
+            graphics::draw::line(a->position.x, a->position.y,
+                                 b->position.x, b->position.y,
+                                 0xFF313131);
+        }
+    }
 
     for(auto part: this->particles)
     {

+ 3 - 3
source/include/app.h

@@ -29,9 +29,9 @@ private:
     vec2 pushForce = vec2(0, 0);
     vec2 mouseCursor;
 
-    vec2 anchor;
-    double k = 100;
-    double restLenght = 400;
+    uint32_t numberOfCorners = 9;
+    double k = 1500;
+    double restLenght = 200;
 
     const double G = 0.00000000006674;
     bool leftButtonPressed;