Browse Source

core update, physic experiments

Kajetan Johannes Hammerle 3 years ago
parent
commit
da3cd5f5c8
9 changed files with 142 additions and 67 deletions
  1. 21 0
      Entity.cpp
  2. 23 0
      Entity.h
  3. 68 51
      Game.cpp
  4. 7 1
      Game.h
  5. 6 4
      Main.cpp
  6. 1 1
      gaming-core
  7. 2 0
      meson.build
  8. 13 8
      rendering/Renderer.cpp
  9. 1 2
      rendering/Renderer.h

+ 21 - 0
Entity.cpp

@@ -0,0 +1,21 @@
+#include "Entity.h"
+
+Entity::Entity(const Vector2& size, float mass)
+    : size(size), inverseMass(1.0f / mass), onGround(false) {
+}
+
+void Entity::addForce(const Vector2& force) {
+    acceleration += force * inverseMass;
+}
+
+void Entity::preTick() {
+    lastPosition = position;
+    // start off with gravity for the next frame
+    acceleration = Vector2(0.0f, -0.5f);
+}
+
+void Entity::tick() {
+    // euler integration, timestep is constant
+    velocity += acceleration;
+    position += velocity;
+}

+ 23 - 0
Entity.h

@@ -0,0 +1,23 @@
+#ifndef ENTITY_H
+#define ENTITY_H
+
+#include "gaming-core/math/Vector.h"
+
+struct Entity {
+    Vector2 lastPosition;
+    Vector2 position;
+    Vector2 size;
+    Vector2 velocity;
+    Vector2 acceleration;
+    Vector2 force;
+    float inverseMass;
+    bool onGround;
+
+    Entity(const Vector2& size, float mass);
+
+    void addForce(const Vector2& force);
+    void preTick();
+    void tick();
+};
+
+#endif

+ 68 - 51
Game.cpp

@@ -1,69 +1,86 @@
 #include "Game.h"
+#include "gaming-core/utils/Utils.h"
 #include "rendering/Renderer.h"
 #include "utils/StringBuffer.h"
 
-float x = 0.0f;
-float y = 0.0f;
-float lastX = 0.0f;
-float lastY = 0.0f;
-float playerWidth = 80.0f;
-float playerHeight = 80.0f;
-
-float width = 800.0f;
-float height = 480.0f;
-
-float motionY = 0.0f;
-bool onGround = false;
-
-Game::Game(Controller& c, const Clock& fps, const Clock& tps) : controller(c), fps(fps), tps(tps) {
+Game::Game(Controller& c, const Clock& fps, const Clock& tps, const Size& size)
+    : controller(c), fps(fps), tps(tps), size(size), physicsToggle(true),
+      player(Vector2(50.0f, 50.0f), 5.0f) {
 }
 
 void Game::tick() {
-    lastX = x;
-    lastY = y;
-    if(controller.left.isDown()) {
-        x -= 7;
-    }
-    if(controller.right.isDown()) {
-        x += 7;
+    if(controller.start.wasReleased()) {
+        physicsToggle = !physicsToggle;
     }
-    if(controller.a.isDown() && onGround) {
-        motionY = -15.0f;
-        onGround = false;
+    player.preTick();
+    if(physicsToggle) {
+        // forces from the player
+        if(controller.a.isDown() && player.onGround) {
+            player.addForce(Vector2(0.0f, 70.0f));
+            player.onGround = false;
+        }
+        if(controller.right.isDown()) {
+            player.addForce(Vector2(2.5f, 0.0f));
+        }
+        if(controller.left.isDown()) {
+            player.addForce(Vector2(-2.5f, 0.0f));
+        }
+        // pseudo drag
+        player.addForce(Vector2(-player.velocity[0] * 0.5f, 0.0f));
+    } else {
+        player.velocity[0] = 0.0f;
+        if(controller.a.isDown() && player.onGround) {
+            player.velocity[1] = 15.0f;
+            player.onGround = false;
+        }
+        if(controller.right.isDown()) {
+            player.velocity[0] = 5.0f;
+        }
+        if(controller.left.isDown()) {
+            player.velocity[0] = -5.0f;
+        }
     }
+    player.tick();
 
-    motionY += 0.5f;
-    y += motionY;
-
-    if(y > height - playerHeight) {
-        y = height - playerHeight;
-        motionY = 0.0f;
-        onGround = true;
+    // collision detection
+    // floor crush
+    if(player.position[1] < 0.0f) {
+        player.position[1] = 0.0f;
+        player.velocity[1] = 0.0f;
+        player.onGround = true;
     }
-    while(x > width) {
-        x -= width + playerWidth;
-        lastX -= width + playerWidth;
+    // right wall
+    if(player.position[0] + player.size[0] > size.width) {
+        player.position[0] = size.width - player.size[0];
+        // player.velocity[0] = 0.0f;
     }
-    while(x < -playerWidth) {
-        x += width + playerWidth;
-        lastX += width + playerWidth;
+    // left wall
+    if(player.position[0] < 0.0f) {
+        player.position[0] = 0.0f;
+        // player.velocity[0] = 0.0f;
     }
 }
 
-void Game::render(float lag, Renderer& renderer) const {
-    renderer.translateTo(0.0f, 0.0f).update();
-    renderer.drawRectangle(lastX + (x - lastX) * lag, lastY + (y - lastY) * lag, playerWidth, playerHeight,
-                           Color4(255, 0, 0, 255));
-    renderer.translateTo(0.0f, 0.0f).update();
-    StringBuffer<100> s;
-    s.append("FPS: ").append(fps.getUpdatesPerSecond());
-    float y = 0.0f;
-    renderer.setStringSize(2);
-    y = renderer.drawString(0.0f, y, s);
-    for(const Button& b : controller.list) {
-        s.clear().append(b.getName()).append(": ").append(b.getDownTime()).append(" ").append(b.wasReleased());
-        y = renderer.drawString(0.0f, y, s);
-    }
+void Game::render(float lag, Renderer& r) const {
+    r.translateTo(0.0f, 0.0f)
+        .scale(1.0f, -1.0f)
+        .translateY(size.height)
+        .update();
+
+    Vector2 pos = Utils::interpolate(player.lastPosition, player.position, lag);
+    r.drawRectangle(pos, player.size, Color4(0xFF, 0x00, 0x00, 0xFF));
+
+    r.translateTo(0.0f, 0.0f).update();
+    r.setStringSize(2);
+    StringBuffer<100> s("FPS: ");
+    s.append(fps.getUpdatesPerSecond()).append(" ");
+    s.append(physicsToggle ? "Force Physics" : "Velocity Physics");
+    float y = 10.0f;
+    y = r.drawString(10.0f, y, s);
+    s.clear().append("a = ").append(player.acceleration);
+    y = r.drawString(10.0f, y, s);
+    s.clear().append("v = ").append(player.velocity);
+    y = r.drawString(10.0f, y, s);
 }
 
 bool Game::isRunning() const {

+ 7 - 1
Game.h

@@ -1,6 +1,7 @@
 #ifndef GAME_H
 #define GAME_H
 
+#include "Entity.h"
 #include "input/Buttons.h"
 #include "input/Controller.h"
 #include "rendering/Renderer.h"
@@ -10,9 +11,14 @@ class Game final {
     const Controller& controller;
     const Clock& fps;
     const Clock& tps;
+    const Size& size;
+
+    bool physicsToggle;
+
+    Entity player;
 
 public:
-    Game(Controller& c, const Clock& fps, const Clock& tps);
+    Game(Controller& c, const Clock& fps, const Clock& tps, const Size& size);
 
     void tick();
     void render(float lag, Renderer& renderer) const;

+ 6 - 4
Main.cpp

@@ -24,12 +24,14 @@ bool parseArgs(int argAmount, char* const* args, WindowOptions& options) {
 }
 
 int main(int argAmount, char* const* args) {
-    WindowOptions options(3, 0, Size(800, 480), true, "Pigine");
+    Size size(800, 480);
+    WindowOptions options(3, 0, size, true, "Pigine");
     if(parseArgs(argAmount, args, options) || GLFW::init()) {
         return 0;
     }
 
-    Window window(options);
+    TextInput* input = nullptr;
+    Window window(input, options);
     if(window.hasError() || GLEW::init()) {
         return 0;
     }
@@ -46,7 +48,7 @@ int main(int argAmount, char* const* args) {
 
     Buttons buttons(window);
     Controller controller(buttons);
-    static Game game(controller, fps, tps);
+    static Game game(controller, fps, tps, size);
 
     window.show();
 
@@ -63,7 +65,7 @@ int main(int argAmount, char* const* args) {
             game.tick();
         }
 
-        Size size = window.getSize();
+        size = window.getSize();
         GL::setViewport(size.width, size.height);
 
         Matrix view;

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit 3f19a9343926b7ee3dc8c8fca6c503062adc6a0b
+Subproject commit 18c11759be38328c95fea4e3ef067f4a0c390c4d

+ 2 - 0
meson.build

@@ -22,7 +22,9 @@ sources = [
     'gaming-core/images/PNGReader.cpp',
     'gaming-core/input/Buttons.cpp',
     'gaming-core/input/Button.cpp',
+    'gaming-core/input/TextInput.cpp',
     'Game.cpp',
+    'Entity.cpp',
     'input/Controller.cpp',
     'rendering/Renderer.cpp',
     'rendering/ColorRenderer.cpp',

+ 13 - 8
rendering/Renderer.cpp

@@ -1,7 +1,8 @@
 #include "rendering/Renderer.h"
 #include "gaming-core/wrapper/GL.h"
 
-Renderer::Renderer(Shader& shader) : shader(shader), texture(true), color(true) {
+Renderer::Renderer(Shader& shader)
+    : shader(shader), texture(true), color(true) {
     shader.use();
     GL::enableBlending();
     setTextureMode(false);
@@ -79,21 +80,25 @@ float Renderer::drawString(float x, float y, const char* text) {
     return fontRenderer.drawString(x, y, text);
 }
 
-void Renderer::drawTriangle(const ColorRenderer::Vertex& v1, const ColorRenderer::Vertex& v2,
+void Renderer::drawTriangle(const ColorRenderer::Vertex& v1,
+                            const ColorRenderer::Vertex& v2,
                             const ColorRenderer::Vertex& v3) {
     setTextureMode(false);
     setColorMode(true);
     colorRenderer.draw(v1, v2, v3);
 }
 
-void Renderer::drawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, Color4 color) {
-    ColorRenderer::Vertex v1 = {x1, y1, color};
+void Renderer::drawTriangle(float x1, float y1, float x2, float y2, float x3,
+                            float y3, Color4 color) {
     ColorRenderer::Vertex v2 = {x2, y2, color};
     ColorRenderer::Vertex v3 = {x3, y3, color};
-    drawTriangle(v1, v2, v3);
+    drawTriangle({x1, y1, color}, v2, v3);
 }
 
-void Renderer::drawRectangle(float x, float y, float width, float height, Color4 color) {
-    drawTriangle(x, y, x + width, y, x, y + height, color);
-    drawTriangle(x + width, y, x, y + height, x + width, y + height, color);
+void Renderer::drawRectangle(const Vector2& pos, const Vector2& size,
+                             Color4 color) {
+    drawTriangle(pos[0], pos[1], pos[0] + size[0], pos[1], pos[0],
+                 pos[1] + size[1], color);
+    drawTriangle(pos[0] + size[0], pos[1], pos[0], pos[1] + size[1],
+                 pos[0] + size[0], pos[1] + size[1], color);
 }

+ 1 - 2
rendering/Renderer.h

@@ -39,8 +39,7 @@ public:
                       const ColorRenderer::Vertex& v3);
     void drawTriangle(float x1, float y1, float x2, float y2, float x3,
                       float y3, Color4 color);
-    void drawRectangle(float x, float y, float width, float height,
-                       Color4 color);
+    void drawRectangle(const Vector2& pos, const Vector2& size, Color4 color);
 
 private:
     void setTextureMode(bool b);