Browse Source

Particle Physics: Bouncing particle

Godzil 1 year ago
parent
commit
101dba840b
3 changed files with 20 additions and 2 deletions
  1. 15 1
      source/app.cpp
  2. 2 0
      source/include/physics/constants.h
  3. 3 1
      source/include/physics/particle.h

+ 15 - 1
source/app.cpp

@@ -68,11 +68,25 @@ void application::update()
     }
     this->millisecsPreviousFrame = SDL_GetTicks();
 
-    this->part->acceleration = vec2(0.0, 9.8 * PIXELS_PER_METER);
+    this->part->acceleration = vec2(2.0 * PIXELS_PER_METER, 9.8 * PIXELS_PER_METER);
 
     // Integrate the change
     this->part->velocity += (this->part->acceleration * deltaTime);
     this->part->position += (this->part->velocity * 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()))
+    {
+        this->part->velocity.y = -this->part->velocity.y;
+    }
+
+    if ( ((this->part->position.x - this->part->radius) <= 0) ||
+         ((this->part->position.x + this->part->radius) >= graphics::width()))
+    {
+        this->part->velocity.x = -this->part->velocity.x;
+    }
+
 }
 
 void application::render()

+ 2 - 0
source/include/physics/constants.h

@@ -13,4 +13,6 @@
 const uint32_t FPS = 60;
 const int32_t MILLISECS_PER_FRAME = 1000 / FPS;
 
+const int32_t PIXELS_PER_METER = 50;
+
 #endif /* PHYSICENGINE_CONSTANTS_H */

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

@@ -15,6 +15,8 @@
 class particle
 {
 public:
+    uint16_t radius;
+
     vec2 position;
     vec2 acceleration;
     vec2 velocity;
@@ -22,7 +24,7 @@ public:
     double mass;
 
 public:
-    particle(double x, double y, double mass): position(x, y), mass(mass) {};
+    particle(double x, double y, double mass): radius(4), position(x, y), mass(mass) {};
     ~particle() = default;
 };