Browse Source

Add some debug tool to show force and velocity vectors.

Godzil 1 year ago
parent
commit
f0c8291827
4 changed files with 76 additions and 2 deletions
  1. 25 1
      source/app.cpp
  2. 5 0
      source/include/app.h
  3. 9 1
      source/include/physics/particle.h
  4. 37 0
      source/physics/particle.cpp

+ 25 - 1
source/app.cpp

@@ -48,6 +48,19 @@ void application::input()
             break;
 
         case SDL_KEYDOWN:
+            if (event.key.keysym.sym == SDLK_1)
+            {
+                this->showVelocity ^= true;
+            }
+            if (event.key.keysym.sym == SDLK_2)
+            {
+                this->showForceSum ^= true;
+            }
+            if (event.key.keysym.sym == SDLK_3)
+            {
+                this->showAllForces ^= true;
+            }
+
             if (event.key.keysym.sym == SDLK_ESCAPE)
             {
                 this->running = false;
@@ -195,10 +208,21 @@ void application::render()
     graphics::draw::fillCircle(this->particles[1]->position.x, this->particles[1]->position.y,
                                this->particles[1]->radius, 0xFF00FFFF);
 
+    if (this->showForceSum || this->showVelocity || this->showAllForces)
+    {
+        for(auto part: this->particles)
+        {
+            part->forceDebug(this->showVelocity, this->showForceSum, this->showAllForces);
+        }
+    }
+
     graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());
     graphics::draw::text(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime.get());
     graphics::draw::text(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime.get());
-    graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f", 1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
+    graphics::draw::text(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f",
+                         1000 / (waitTime.get() + updateTime.get() + renderTime.get()), deltaTime);
+    graphics::draw::text(5, 53, 0x11FF22, "DBG: V:%c S:%c A:%c", this->showVelocity ? 'Y':'N',
+                         this->showForceSum ? 'Y':'N', this->showAllForces ? 'Y':'N');
 
     graphics::renderFrame();
 

+ 5 - 0
source/include/app.h

@@ -33,6 +33,11 @@ private:
     const double G = 0.00000000006674;
     bool leftButtonPressed;
 
+    /* debug */
+    bool showVelocity = false;
+    bool showForceSum = false;
+    bool showAllForces = false;
+
 public:
     application() : running(false), leftButtonPressed(false) {};
     ~application() = default;

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

@@ -10,22 +10,28 @@
 #ifndef PHYSICENGINE_PHYSICS_PARTICLE_H
 #define PHYSICENGINE_PHYSICS_PARTICLE_H
 
+#include <vector>
+
 #include <physics/vec2.h>
 
 class particle
 {
 public:
-    uint16_t radius;
+    double radius;
 
     vec2 position;
     vec2 acceleration;
     vec2 velocity;
 
     vec2 sum_of_forces;
+    vec2 frame_sum;
 
     double mass;
     double invMass;
 
+    /* Only used for debug display */
+    std::vector<vec2> forces;
+
 public:
     particle(double x, double y, double mass, double radius = 4.0): radius(radius), position(x, y),
                                                               mass(mass)
@@ -43,6 +49,8 @@ public:
         this->sum_of_forces = vec2(0, 0);
     }
 
+    void forceDebug(bool showVelocity, bool showSum, bool showAll);
+
     void addForce(const vec2 &force);
     void integrate(double dt);
 };

+ 37 - 0
source/physics/particle.cpp

@@ -7,8 +7,16 @@
  * Created by Manoël Trapier on 07/06/2022.
  */
 
+#include <graphics.h>
 #include <physics/particle.h>
 
+uint32_t forceColours[] =
+{
+    0xFF0000FF, 0xFF00FF00, 0xFFFF0000,
+    0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00,
+    0xFFFFFFFF,
+};
+
 void particle::integrate(double dt)
 {
     this->acceleration = this->sum_of_forces * this->invMass;
@@ -16,11 +24,40 @@ void particle::integrate(double dt)
     this->velocity += this->acceleration * dt;
     this->position += this->velocity * dt;
 
+    this->frame_sum = this->sum_of_forces;
+
     this->clearForces();
 }
 
 void particle::addForce(const vec2 &force)
 {
     this->sum_of_forces += force;
+    this->forces.push_back(force);
 }
 
+void particle::forceDebug(bool showVelocity, bool showSum, bool showAll)
+{
+    int c = 0;
+
+    if (showAll)
+    {
+        for (auto f: this->forces)
+        {
+            graphics::draw::arrow(this->position.x, this->position.y, f.x, f.y, forceColours[c]);
+            c = (c + 1) % 7;
+        }
+    }
+    if (showSum)
+    {
+        graphics::draw::arrow(this->position.x, this->position.y,
+                              this->frame_sum.x, this->frame_sum.y,
+                              0xFF00FFCC);
+    }
+    if (showVelocity)
+    {
+        graphics::draw::arrow(this->position.x, this->position.y,
+                              this->velocity.x, this->velocity.y,
+                              0xFF9966ff);
+    }
+    this->forces.clear();
+}