Browse Source

Simulating movement: Playing with mass.

Godzil 1 year ago
parent
commit
1fa52ef7c2
3 changed files with 34 additions and 11 deletions
  1. 30 10
      source/app.cpp
  2. 3 1
      source/include/app.h
  3. 1 0
      source/include/physics/particle.h

+ 30 - 10
source/app.cpp

@@ -23,7 +23,9 @@ void application::setup()
 {
     this->running = graphics::openWindow();
     this->millisecsPreviousFrame = SDL_GetTicks();
-    this->part = new particle(50, 100, 1.0);
+
+    this->particles.push_back(new particle(50, 100, 1.0, 4));
+    this->particles.push_back(new particle(50, 200, 3.0, 12));
 }
 
 void application::input()
@@ -80,14 +82,24 @@ void application::update()
 
     this->part->addForce(wind);
 
-    // Integrate the change
-    this->part->integrate(deltaTime);
-
-    // check the particles position and keep the particule in the window.
-    if ( ((this->part->position.y - this->part->radius) <= 0) ||
-         ((this->part->position.y + this->part->radius) >= graphics::height()))
+    for (auto part:this->particles)
     {
-        this->part->velocity.y = -this->part->velocity.y;
+        part->addForce(wind);
+        part->integrate(deltaTime);
+
+        // check the particles position and keep the particule in the window.
+        if ( ((part->position.y - part->radius) <= 0) ||
+             ((part->position.y + part->radius) >= graphics::height()))
+        {
+            part->velocity.y = -part->velocity.y;
+        }
+
+        if ( ((part->position.x - part->radius) <= 0) ||
+             ((part->position.x + part->radius) >= graphics::width()))
+        {
+            part->velocity.x = -part->velocity.x;
+        }
+
     }
 
     if ( ((this->part->position.x - this->part->radius) <= 0) ||
@@ -101,6 +113,11 @@ void application::update()
 void application::render()
 {
     graphics::clearScreen(0xFF056263);
+    for (auto part:this->particles)
+    {
+        graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFFFFFFF);
+    }
+
     graphics::draw::fillCircle(this->part->position.x, this->part->position.y, 4, 0xFFFFFFFF);
 
     graphics::renderFrame();
@@ -109,8 +126,11 @@ void application::render()
 void application::destroy()
 {
     // Nothing for now.
+    for (auto part:this->particles)
+    {
+        delete part;
+    }
 
-    graphics::closeWindow();
 
-    delete this->part;
+    graphics::closeWindow();
 }

+ 3 - 1
source/include/app.h

@@ -10,6 +10,8 @@
 #ifndef PHYSICENGINE_APP_H
 #define PHYSICENGINE_APP_H
 
+#include <vector>
+
 #include <physics/particle.h>
 
 class application
@@ -17,7 +19,7 @@ class application
 private:
     uint32_t millisecsPreviousFrame;
     bool running;
-    particle *part;
+    std::vector<particle *>particles;
 
 public:
     application() : running(false) {};

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

@@ -27,6 +27,7 @@ public:
 
 public:
     particle(double x, double y, double mass): radius(4), position(x, y), mass(mass) {};
+    particle(double x, double y, double mass, double radius): radius(radius), position(x, y), mass(mass) {};
     ~particle() = default;
 
     void clearForces()