Browse Source

Simulating movement: Add Forces

Godzil 1 year ago
parent
commit
2ab39ed270
4 changed files with 30 additions and 2 deletions
  1. 12 2
      source/app.cpp
  2. 1 0
      source/include/app.h
  3. 8 0
      source/include/physics/particle.h
  4. 9 0
      source/physics/particle.cpp

+ 12 - 2
source/app.cpp

@@ -50,7 +50,7 @@ void application::input()
     }
 }
 
-void application::update()
+double application::syncAndDeltatime()
 {
     /* Let's make sure we are running at the expected framerate */
     uint32_t now = SDL_GetTicks();
@@ -68,7 +68,17 @@ void application::update()
     }
     this->millisecsPreviousFrame = SDL_GetTicks();
 
-    this->part->acceleration = vec2(2.0 * PIXELS_PER_METER, 9.8 * PIXELS_PER_METER);
+    return deltaTime;
+}
+
+
+void application::update()
+{
+    vec2 wind = vec2(0.2 * PIXELS_PER_METER, 0);
+
+    double deltaTime = this->syncAndDeltatime();
+
+    this->part->addForce(wind);
 
     // Integrate the change
     this->part->integrate(deltaTime);

+ 1 - 0
source/include/app.h

@@ -25,6 +25,7 @@ public:
 
     void parseParameters(int argc, char *argv[]);
     bool isRunning() { return this->running; }
+    double syncAndDeltatime();
 
     void setup();
     void input();

+ 8 - 0
source/include/physics/particle.h

@@ -21,12 +21,20 @@ public:
     vec2 acceleration;
     vec2 velocity;
 
+    vec2 sum_of_forces;
+
     double mass;
 
 public:
     particle(double x, double y, double mass): radius(4), position(x, y), mass(mass) {};
     ~particle() = default;
 
+    void clearForces()
+    {
+        this->sum_of_forces = vec2(0, 0);
+    }
+
+    void addForce(const vec2 &force);
     void integrate(double dt);
 };
 

+ 9 - 0
source/physics/particle.cpp

@@ -11,7 +11,16 @@
 
 void particle::integrate(double dt)
 {
+    this->acceleration = this->sum_of_forces / this->mass;
+
     this->velocity += this->acceleration * dt;
     this->position += this->velocity * dt;
+
+    this->clearForces();
+}
+
+void particle::addForce(const vec2 &force)
+{
+    this->sum_of_forces += force;
 }