Browse Source

Drag & Friction: Add drag force

Godzil 1 year ago
parent
commit
f4cb3ef7c0
4 changed files with 70 additions and 2 deletions
  1. 21 2
      source/app.cpp
  2. 2 0
      source/include/app.h
  3. 22 0
      source/include/physics/force.h
  4. 25 0
      source/physics/force.cpp

+ 21 - 2
source/app.cpp

@@ -13,6 +13,11 @@
 #include <imgui_impl_sdl.h>
 
 #include <physics/constants.h>
+#include <physics/particle.h>
+#include <physics/vec2.h>
+#include <physics/force.h>
+
+#include <SDL.h>
 
 void application::parseParameters(int argc, char *argv[])
 {
@@ -25,7 +30,13 @@ 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));
+
+    this->liquid.x = 0;
+    this->liquid.y = graphics::height() / 2;
+    this->liquid.w = graphics::width();
+    this->liquid.h = graphics::height() / 2;
+
 }
 
 void application::input()
@@ -101,9 +112,17 @@ void application::update()
 
     for (auto part:this->particles)
     {
-        part->addForce(wind);
+        //part->addForce(wind);
         part->addForce(this->pushForce);
         part->addForce(weight * part->mass);
+
+        // Apply drag if in liquid.
+        if (part->position.y >= this->liquid.y)
+        {
+            vec2 drag = force::generateDragForce(*part, 0.01);
+            part->addForce(drag);
+        }
+
         part->integrate(deltaTime);
 
         // check the particles position and keep the particle in the window.

+ 2 - 0
source/include/app.h

@@ -26,6 +26,8 @@ private:
 
     vec2 pushForce = vec2(0, 0);
 
+    SDL_Rect liquid;
+
 public:
     application() : running(false) {};
     ~application() = default;

+ 22 - 0
source/include/physics/force.h

@@ -0,0 +1,22 @@
+/*
+ * 2D Physic Engine
+ * force.h: 
+ * Based on pikuma.com Learn Game Physics Engine Programming course.
+ * Copyright (c) 2022 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 17/06/2022.
+ */
+
+#ifndef PHYSICENGINE_FORCE_H
+#define PHYSICENGINE_FORCE_H
+
+#include <physics/vec2.h>
+#include <physics/particle.h>
+
+class force
+{
+public:
+    static vec2 generateDragForce(const particle &particleRef, double k);
+};
+
+#endif /* PHYSICENGINE_FORCE_H */

+ 25 - 0
source/physics/force.cpp

@@ -0,0 +1,25 @@
+/*
+ * 2D Physic Engine
+ * force.cpp: 
+ * Based on pikuma.com Learn Game Physics Engine Programming course.
+ * Copyright (c) 2022 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 17/06/2022.
+ */
+
+#include <physics/force.h>
+
+vec2 force::generateDragForce(const particle &particleRef, double k)
+{
+    vec2 dragForce;
+
+    if (particleRef.velocity.magnitudeSquared() > 0)
+    {
+        vec2 dragDirection = particleRef.velocity.unitVector() * -1;
+        double dragMagnitude = k * particleRef.velocity.magnitudeSquared();
+
+        dragForce = dragDirection * dragMagnitude;
+    }
+
+    return dragForce;
+}