Browse Source

RigidBoby: Implements shape skeletons.

Godzil 1 year ago
parent
commit
04a91944e3

+ 11 - 41
source/app.cpp

@@ -17,6 +17,9 @@
 #include <physics/vec2.h>
 #include <physics/force.h>
 
+#include <physics/shape.h>
+#include <physics/shapes/circle.h>
+
 #include <SDL.h>
 
 void application::parseParameters(int argc, char *argv[])
@@ -30,8 +33,8 @@ void application::setup()
     this->millisecsPreviousFrame = SDL_GetTicks();
 
     this->anchor = vec2(graphics::width() / 2.0,30);
-    this->bodies.push_back(new body(graphics::width() / 2.0, graphics::height() / 2.0,
-                                    2.0, 10));
+    this->bodies.push_back(new body(circleShape(12), graphics::width() / 2.0, graphics::height() / 2.0,
+                                    2.0));
 }
 
 void application::input()
@@ -163,40 +166,6 @@ void application::update()
         part->integrate(deltaTime);
     }
 
-    for (auto part:this->bodies)
-    {
-        // 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()))
-        {
-            if ((part->position.y - part->radius) <= 0)
-            {
-                part->position.y = part->radius;
-            }
-            else
-            {
-                part->position.y = graphics::height() - part->radius;
-            }
-
-            part->velocity.y = -part->velocity.y;
-        }
-
-        if ( ((part->position.x - part->radius) <= 0) ||
-             ((part->position.x + part->radius) >= graphics::width()))
-        {
-            if ((part->position.x - part->radius) <= 0)
-            {
-                part->position.x = part->radius;
-            }
-            else
-            {
-                part->position.x = graphics::width() - part->radius;
-            }
-
-            part->velocity.x = -part->velocity.x;
-        }
-    }
-
     updateTime.stop();
 }
 
@@ -216,15 +185,16 @@ void application::render()
                          0xFF313131);
     graphics::draw::fillCircle(this->anchor.x, this->anchor.y, 5, 0xFF001155);
 
-    for(auto part: this->bodies)
+    /*
+    for(auto bod: this->bodies)
     {
-        graphics::draw::fillCircle(part->position.x, part->position.y, part->radius, 0xFFAA3300);
+        graphics::draw::fillCircle(bod->position.x, bod->position.y, bod->radius, 0xFFAA3300);
     }
+    */
 
-
-    for(auto part: this->bodies)
+    for(auto bod: this->bodies)
     {
-        part->forceDebug(this->showVelocity, this->showForceSum, this->showAllForces);
+        bod->forceDebug(this->showVelocity, this->showForceSum, this->showAllForces);
     }
 
     graphics::draw::text(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime.get());

+ 10 - 5
source/include/physics/body.h

@@ -13,12 +13,11 @@
 #include <vector>
 
 #include <physics/vec2.h>
+#include <physics/shape.h>
 
 class body
 {
 public:
-    double radius;
-
     vec2 position;
     vec2 acceleration;
     vec2 velocity;
@@ -29,20 +28,26 @@ public:
     double mass;
     double invMass;
 
+    shape *shp;
+
     /* Only used for debug display */
     std::vector<vec2> forces;
 
 public:
-    body(double x, double y, double mass, double radius = 4.0): radius(radius), position(x, y),
-                                                                mass(mass)
+    body(const shape &s, double x, double y, double mass) : position(x, y), mass(mass)
     {
+        this->shp = s.clone();
         this->invMass = 0;
         if (mass != 0.0)
         {
             this->invMass = 1 / mass;
         }
     };
-    ~body() = default;
+
+    ~body()
+    {
+        delete this->shp;
+    };
 
     void clearForces()
     {

+ 28 - 0
source/include/physics/shape.h

@@ -0,0 +1,28 @@
+/*
+ * 2D Physic Engine
+ * shape.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 18/07/2022.
+ */
+
+#ifndef PHYSICENGINE_SHAPE_H
+#define PHYSICENGINE_SHAPE_H
+
+enum shapeType
+{
+    SHAPE_CIRCLE,
+    SHAPE_BOX,
+    SHAPE_POLYGON,
+};
+
+struct shape
+{
+    virtual ~shape() = default;
+    virtual shapeType getType() const = 0;
+    virtual double getMomentOfInertial() const = 0;
+    virtual shape *clone() const = 0;
+};
+
+#endif /* PHYSICENGINE_SHAPE_H */

+ 45 - 0
source/include/physics/shapes/box.h

@@ -0,0 +1,45 @@
+/*
+ * 2D Physic Engine
+ * box.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 18/07/2022.
+ */
+
+#ifndef PHYSICENGINE_SHAPE_BOX_H
+#define PHYSICENGINE_SHAPE_BOX_H
+
+#include <vector>
+
+#include <physics/vec2.h>
+#include <physics/shapes/polygon.h>
+
+struct boxShape : public polygonShape
+{
+    double width;
+    double height;
+
+    boxShape() = default;
+    boxShape(double width, double height) : width(width), height(height)
+    {
+    };
+
+    virtual ~boxShape() = default;
+    shapeType getType() const override
+    {
+        return SHAPE_BOX;
+    }
+
+    double getMomentOfInertial() const override
+    {
+        return 0.0;
+    };
+
+    shape *clone() const override
+    {
+        return new boxShape(this->width, this->height);
+    };
+};
+
+#endif /* PHYSICENGINE_SHAPE_BOX_H */

+ 38 - 0
source/include/physics/shapes/circle.h

@@ -0,0 +1,38 @@
+/*
+ * 2D Physic Engine
+ * circle.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 18/07/2022.
+ */
+
+#ifndef PHYSICENGINE_SHAPE_CIRCLE_H
+#define PHYSICENGINE_SHAPE_CIRCLE_H
+
+#include <physics/shape.h>
+
+struct circleShape: public shape
+{
+    double radius;
+
+    circleShape() = default;
+    circleShape(const double radius) : radius(radius) {};
+    virtual ~circleShape() = default;
+    shapeType getType() const override
+    {
+        return SHAPE_CIRCLE;
+    }
+
+    double getMomentOfInertial() const override
+    {
+        return 0.0;
+    };
+
+    shape *clone() const override
+    {
+        return new circleShape(this->radius);
+    };
+};
+
+#endif /* PHYSICENGINE_SHAPE_CIRCLE_H */

+ 45 - 0
source/include/physics/shapes/polygon.h

@@ -0,0 +1,45 @@
+/*
+ * 2D Physic Engine
+ * polygon.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 18/07/2022.
+ */
+
+#ifndef PHYSICENGINE_SHAPE_POLYGON_H
+#define PHYSICENGINE_SHAPE_POLYGON_H
+
+#include <vector>
+
+#include <physics/vec2.h>
+#include <physics/shape.h>
+
+struct polygonShape: public shape
+{
+    std::vector<vec2> vertices;
+
+    polygonShape() = default;
+    polygonShape(const std::vector<vec2> vertices)
+    {
+
+    };
+
+    virtual ~polygonShape() = default;
+    shapeType getType() const override
+    {
+        return SHAPE_CIRCLE;
+    }
+
+    double getMomentOfInertial() const override
+    {
+        return 0.0;
+    };
+
+    shape *clone() const override
+    {
+        return new polygonShape(this->vertices);
+    };
+};
+
+#endif /* PHYSICENGINE_SHAPE_POLYGON_H */