Browse Source

Applying forces: Pushing the buttons.

Godzil 1 year ago
parent
commit
e48d9125b6
2 changed files with 18 additions and 1 deletions
  1. 16 1
      source/app.cpp
  2. 2 0
      source/include/app.h

+ 16 - 1
source/app.cpp

@@ -25,7 +25,7 @@ void application::setup()
     this->millisecsPreviousFrame = SDL_GetTicks();
 
     this->particles.push_back(new particle(50, 100, 1.0, 4));
-    this->particles.push_back(new particle(200, 100, 3.0, 12));
+    //this->particles.push_back(new particle(200, 100, 3.0, 12));
 }
 
 void application::input()
@@ -47,6 +47,20 @@ void application::input()
             {
                 this->running = false;
             }
+
+            if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = -50 * PIXELS_PER_METER;
+            if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 50 * PIXELS_PER_METER;
+            if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 50 * PIXELS_PER_METER;
+            if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = -50 * PIXELS_PER_METER;
+
+            break;
+
+        case SDL_KEYUP:
+            if (event.key.keysym.sym == SDLK_UP) this->pushForce.y = 0;
+            if (event.key.keysym.sym == SDLK_RIGHT) this->pushForce.x = 0;
+            if (event.key.keysym.sym == SDLK_DOWN) this->pushForce.y = 0;
+            if (event.key.keysym.sym == SDLK_LEFT) this->pushForce.x = 0;
+
             break;
         }
     }
@@ -88,6 +102,7 @@ void application::update()
     for (auto part:this->particles)
     {
         part->addForce(wind);
+        part->addForce(this->pushForce);
         part->addForce(weight * part->mass);
         part->integrate(deltaTime);
 

+ 2 - 0
source/include/app.h

@@ -24,6 +24,8 @@ private:
     timeProbe waitTime, renderTime, updateTime;
     double deltaTime;
 
+    vec2 pushForce = vec2(0, 0);
+
 public:
     application() : running(false) {};
     ~application() = default;