Browse Source

Rename particle to body.

Godzil 1 year ago
parent
commit
7c1d2934d0

+ 17 - 17
source/app.cpp

@@ -13,7 +13,7 @@
 #include <imgui_impl_sdl.h>
 
 #include <physics/constants.h>
-#include <physics/particle.h>
+#include <physics/body.h>
 #include <physics/vec2.h>
 #include <physics/force.h>
 
@@ -30,8 +30,8 @@ void application::setup()
     this->millisecsPreviousFrame = SDL_GetTicks();
 
     this->anchor = vec2(graphics::width() / 2.0,30);
-    this->particles.push_back(new particle(graphics::width() / 2.0, graphics::height() / 2.0,
-                                       2.0, 10));
+    this->bodies.push_back(new body(graphics::width() / 2.0, graphics::height() / 2.0,
+                                    2.0, 10));
 }
 
 void application::input()
@@ -94,9 +94,9 @@ void application::input()
             {
                 if (this->leftButtonPressed)
                 {
-                    vec2 impulseDirection = (particles[0]->position - this->mouseCursor).unitVector();
-                    double impulseMagnitude = (particles[0]->position - this->mouseCursor).magnitude() * 5.0;
-                    particles[0]->velocity = impulseDirection * impulseMagnitude;
+                    vec2 impulseDirection = (bodies[0]->position - this->mouseCursor).unitVector();
+                    double impulseMagnitude = (bodies[0]->position - this->mouseCursor).magnitude() * 5.0;
+                    bodies[0]->velocity = impulseDirection * impulseMagnitude;
                 }
                 this->leftButtonPressed = false;
             }
@@ -140,7 +140,7 @@ void application::update()
     this->syncAndDeltatime();
     updateTime.start();
 
-    for (auto part:this->particles)
+    for (auto part:this->bodies)
     {
         part->addForce(pushForce);
 
@@ -154,18 +154,18 @@ void application::update()
         part->addForce(weight);
     }
 
-    vec2 spring = force::generateSpringForce(*this->particles[0],
+    vec2 spring = force::generateSpringForce(*this->bodies[0],
                                              this->anchor, this->restLenght, this->k);
-    this->particles[0]->addForce(spring);
+    this->bodies[0]->addForce(spring);
 
-    for (auto part:this->particles)
+    for (auto part:this->bodies)
     {
         part->integrate(deltaTime);
     }
 
-    for (auto part:this->particles)
+    for (auto part:this->bodies)
     {
-        // check the particles position and keep the particle in the window.
+        // check the bodies position and keep the body in the window.
         if ( ((part->position.y - part->radius) <= 0) ||
              ((part->position.y + part->radius) >= graphics::height()))
         {
@@ -207,22 +207,22 @@ void application::render()
     graphics::clearScreen(0xFF0F0721);
     if (this->leftButtonPressed)
     {
-        graphics::draw::line(this->particles[0]->position.x, this->particles[0]->position.y,
+        graphics::draw::line(this->bodies[0]->position.x, this->bodies[0]->position.y,
                              this->mouseCursor.x, this->mouseCursor.y, 0xFFFFFFFF);
     }
 
     graphics::draw::line(this->anchor.x, this->anchor.y,
-                         this->particles[0]->position.x, this->particles[0]->position.y,
+                         this->bodies[0]->position.x, this->bodies[0]->position.y,
                          0xFF313131);
     graphics::draw::fillCircle(this->anchor.x, this->anchor.y, 5, 0xFF001155);
 
-    for(auto part: this->particles)
+    for(auto part: this->bodies)
     {
         graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFAA3300);
     }
 
 
-    for(auto part: this->particles)
+    for(auto part: this->bodies)
     {
         part->forceDebug(this->showVelocity, this->showForceSum, this->showAllForces);
     }
@@ -243,7 +243,7 @@ void application::render()
 void application::destroy()
 {
     // Nothing for now.
-    for (auto part:this->particles)
+    for (auto part:this->bodies)
     {
         delete part;
     }

+ 2 - 2
source/include/app.h

@@ -15,14 +15,14 @@
 #include <SDL.h>
 
 #include <timeprobe.h>
-#include <physics/particle.h>
+#include <physics/body.h>
 
 class application
 {
 private:
     uint32_t millisecsPreviousFrame;
     bool running;
-    std::vector<particle *>particles;
+    std::vector<body *>bodies;
     timeProbe waitTime, renderTime, updateTime;
     double deltaTime;
 

+ 8 - 8
source/include/physics/particle.h → source/include/physics/body.h

@@ -1,20 +1,20 @@
 /*
  * 2D Physic Engine
- * particle.h:
+ * body.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 07/06/2022.
  */
 
-#ifndef PHYSICENGINE_PHYSICS_PARTICLE_H
-#define PHYSICENGINE_PHYSICS_PARTICLE_H
+#ifndef PHYSICENGINE_PHYSICS_BODY_H
+#define PHYSICENGINE_PHYSICS_BODY_H
 
 #include <vector>
 
 #include <physics/vec2.h>
 
-class particle
+class body
 {
 public:
     double radius;
@@ -33,8 +33,8 @@ public:
     std::vector<vec2> forces;
 
 public:
-    particle(double x, double y, double mass, double radius = 4.0): radius(radius), position(x, y),
-                                                              mass(mass)
+    body(double x, double y, double mass, double radius = 4.0): radius(radius), position(x, y),
+                                                                mass(mass)
     {
         this->invMass = 0;
         if (mass != 0.0)
@@ -42,7 +42,7 @@ public:
             this->invMass = 1 / mass;
         }
     };
-    ~particle() = default;
+    ~body() = default;
 
     void clearForces()
     {
@@ -55,4 +55,4 @@ public:
     void integrate(double dt);
 };
 
-#endif /* PHYSICENGINE_PHYSICS_PARTICLE_H */
+#endif /* PHYSICENGINE_PHYSICS_BODY_H */

+ 6 - 6
source/include/physics/force.h

@@ -11,17 +11,17 @@
 #define PHYSICENGINE_FORCE_H
 
 #include <physics/vec2.h>
-#include <physics/particle.h>
+#include <physics/body.h>
 
 class force
 {
 public:
-    static vec2 generateFrictionForce(const particle &particleRef, double k);
-    static vec2 generateDragForce(const particle &particleRef, double k);
-    static vec2 generateGravitationalForce(const particle &a, const particle &b, double g,
+    static vec2 generateFrictionForce(const body &bodyRef, double k);
+    static vec2 generateDragForce(const body &bodyRef, double k);
+    static vec2 generateGravitationalForce(const body &a, const body &b, double g,
                                            double minDistance = 0, double maxDistance = INFINITY);
-    static vec2 generateSpringForce(const particle &particle, vec2 anchor, double restLength, double k);
-    static vec2 generateSpringForce(const particle &a, const particle & b, double restLength, double k);
+    static vec2 generateSpringForce(const body &body, vec2 anchor, double restLength, double k);
+    static vec2 generateSpringForce(const body &a, const body & b, double restLength, double k);
 };
 
 #endif /* PHYSICENGINE_FORCE_H */

+ 5 - 5
source/physics/particle.cpp → source/physics/body.cpp

@@ -1,6 +1,6 @@
 /*
  * 2D Physic Engine
- * particules.cpp: 
+ * body.cpp:
  * Based on pikuma.com Learn Game Physics Engine Programming course.
  * Copyright (c) 2022 986-Studio. All rights reserved.
  *
@@ -8,7 +8,7 @@
  */
 
 #include <graphics.h>
-#include <physics/particle.h>
+#include <physics/body.h>
 
 uint32_t forceColours[] =
 {
@@ -17,7 +17,7 @@ uint32_t forceColours[] =
     0xFFFFFFFF,
 };
 
-void particle::integrate(double dt)
+void body::integrate(double dt)
 {
     this->acceleration = this->sum_of_forces * this->invMass;
 
@@ -29,13 +29,13 @@ void particle::integrate(double dt)
     this->clearForces();
 }
 
-void particle::addForce(const vec2 &force)
+void body::addForce(const vec2 &force)
 {
     this->sum_of_forces += force;
     this->forces.push_back(force);
 }
 
-void particle::forceDebug(bool showVelocity, bool showSum, bool showAll)
+void body::forceDebug(bool showVelocity, bool showSum, bool showAll)
 {
     int c = 0;
 

+ 10 - 10
source/physics/force.cpp

@@ -11,14 +11,14 @@
 
 #include <algorithm>
 
-vec2 force::generateDragForce(const particle &particleRef, double k)
+vec2 force::generateDragForce(const body &bodyRef, double k)
 {
     vec2 dragForce;
 
-    if (particleRef.velocity.magnitudeSquared() > 0)
+    if (bodyRef.velocity.magnitudeSquared() > 0)
     {
-        vec2 dragDirection = particleRef.velocity.unitVector() * -1;
-        double dragMagnitude = k * particleRef.velocity.magnitudeSquared();
+        vec2 dragDirection = bodyRef.velocity.unitVector() * -1;
+        double dragMagnitude = k * bodyRef.velocity.magnitudeSquared();
 
         dragForce = dragDirection * dragMagnitude;
     }
@@ -26,11 +26,11 @@ vec2 force::generateDragForce(const particle &particleRef, double k)
     return dragForce;
 }
 
-vec2 force::generateFrictionForce(const particle &particleRef, double k)
+vec2 force::generateFrictionForce(const body &bodyRef, double k)
 {
     vec2 frictionForce;
 
-    vec2 frictionDirection = particleRef.velocity.unitVector() * -1;
+    vec2 frictionDirection = bodyRef.velocity.unitVector() * -1;
     double frictionMagnitude = k;
 
     frictionForce = frictionDirection * frictionMagnitude;
@@ -38,7 +38,7 @@ vec2 force::generateFrictionForce(const particle &particleRef, double k)
     return frictionForce;
 }
 
-vec2 force::generateGravitationalForce(const particle &a, const particle &b, double g, double minDistance,
+vec2 force::generateGravitationalForce(const body &a, const body &b, double g, double minDistance,
                                        double maxDistance)
 {
     vec2 attractionForce;
@@ -56,9 +56,9 @@ vec2 force::generateGravitationalForce(const particle &a, const particle &b, dou
     return attractionForce;
 }
 
-vec2 force::generateSpringForce(const particle &particle, vec2 anchor, double restLength, double k)
+vec2 force::generateSpringForce(const body &body, vec2 anchor, double restLength, double k)
 {
-    vec2 d = particle.position - anchor;
+    vec2 d = body.position - anchor;
     double displacement = d.magnitude() - restLength;
 
     vec2 springDirection = d.unitVector();
@@ -69,7 +69,7 @@ vec2 force::generateSpringForce(const particle &particle, vec2 anchor, double re
     return springForce;
 }
 
-vec2 force::generateSpringForce(const particle &a, const particle &b, double restLength, double k)
+vec2 force::generateSpringForce(const body &a, const body &b, double restLength, double k)
 {
     vec2 d = a.position - b.position;
     double displacement = d.magnitude() - restLength;