Browse Source

Fix some stuffs and draw circle shapes.

Godzil 1 year ago
parent
commit
aeecdffdce

+ 44 - 6
source/app.cpp

@@ -33,7 +33,7 @@ void application::setup()
     this->millisecsPreviousFrame = SDL_GetTicks();
 
     this->anchor = vec2(graphics::width() / 2.0,30);
-    this->bodies.push_back(new body(circleShape(12), graphics::width() / 2.0, graphics::height() / 2.0,
+    this->bodies.push_back(new body(circleShape(50), graphics::width() / 2.0, graphics::height() / 2.0,
                                     2.0));
 }
 
@@ -161,9 +161,46 @@ void application::update()
                                              this->anchor, this->restLenght, this->k);
     this->bodies[0]->addForce(spring);
 
-    for (auto part:this->bodies)
+    for (auto bod:this->bodies)
+    {
+        bod->integrate(deltaTime);
+    }
+
+    for (auto bod:this->bodies)
     {
-        part->integrate(deltaTime);
+        if (bod->shp->getType() == SHAPE_CIRCLE) {
+            // UGLY HACK
+            circleShape *shp = (circleShape *) bod->shp;
+            if (((bod->position.y - shp->radius) <= 0) ||
+                ((bod->position.y + shp->radius) >= graphics::height()))
+            {
+                if ((bod->position.y - shp->radius) <= 0)
+                {
+                    bod->position.y = shp->radius;
+                }
+                else
+                {
+                    bod->position.y = graphics::height() - shp->radius;
+                }
+
+                bod->velocity.y = -bod->velocity.y;
+            }
+
+            if (((bod->position.x - shp->radius) <= 0) ||
+                ((bod->position.x + shp->radius) >= graphics::width()))
+            {
+                if ((bod->position.x - shp->radius) <= 0)
+                {
+                    bod->position.x = shp->radius;
+                }
+                else
+                {
+                    bod->position.x = graphics::width() - shp->radius;
+                }
+
+                bod->velocity.x = -bod->velocity.x;
+            }
+        }
     }
 
     updateTime.stop();
@@ -185,12 +222,13 @@ void application::render()
                          0xFF313131);
     graphics::draw::fillCircle(this->anchor.x, this->anchor.y, 5, 0xFF001155);
 
-    /*
+
     for(auto bod: this->bodies)
     {
-        graphics::draw::fillCircle(bod->position.x, bod->position.y, bod->radius, 0xFFAA3300);
+        bod->rotation += 0.01;
+        bod->shp->draw(*bod);
     }
-    */
+
 
     for(auto bod: this->bodies)
     {

+ 11 - 2
source/include/physics/body.h

@@ -28,14 +28,19 @@ public:
     double mass;
     double invMass;
 
+    double rotation;
+
     shape *shp;
 
+    uint32_t colour;
+
     /* Only used for debug display */
     std::vector<vec2> forces;
 
 public:
-    body(const shape &s, double x, double y, double mass) : position(x, y), mass(mass)
+    body(const shape &s, double x, double y, double mass) : position(x, y), mass(mass), rotation(0)
     {
+        this->colour = 0xFFFFFFFF;
         this->shp = s.clone();
         this->invMass = 0;
         if (mass != 0.0)
@@ -49,13 +54,17 @@ public:
         delete this->shp;
     };
 
+    void setColour(uint32_t colour)
+    {
+        this->colour = colour;
+    };
+
     void clearForces()
     {
         this->sum_of_forces = vec2(0, 0);
     }
 
     void forceDebug(bool showVelocity, bool showSum, bool showAll);
-
     void addForce(const vec2 &force);
     void integrate(double dt);
 };

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

@@ -10,6 +10,10 @@
 #ifndef PHYSICENGINE_SHAPE_H
 #define PHYSICENGINE_SHAPE_H
 
+#include <physics/body.h>
+
+class body;
+
 enum shapeType
 {
     SHAPE_CIRCLE,
@@ -23,6 +27,7 @@ struct shape
     virtual shapeType getType() const = 0;
     virtual double getMomentOfInertial() const = 0;
     virtual shape *clone() const = 0;
+    virtual void draw(const body &bod) = 0;
 };
 
 #endif /* PHYSICENGINE_SHAPE_H */

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

@@ -10,7 +10,9 @@
 #ifndef PHYSICENGINE_SHAPE_CIRCLE_H
 #define PHYSICENGINE_SHAPE_CIRCLE_H
 
+#include <physics/body.h>
 #include <physics/shape.h>
+#include <graphics.h>
 
 struct circleShape: public shape
 {
@@ -19,6 +21,7 @@ struct circleShape: public shape
     circleShape() = default;
     circleShape(const double radius) : radius(radius) {};
     virtual ~circleShape() = default;
+
     shapeType getType() const override
     {
         return SHAPE_CIRCLE;
@@ -33,6 +36,11 @@ struct circleShape: public shape
     {
         return new circleShape(this->radius);
     };
+
+    void draw(const body &bod) override
+    {
+        graphics::draw::circle(bod.position.x, bod.position.y, this->radius, bod.rotation, bod.colour);
+    };
 };
 
 #endif /* PHYSICENGINE_SHAPE_CIRCLE_H */