]> git.localhorst.tv Git - orbi.git/commitdiff
some cleanup
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 10 Apr 2014 07:54:31 +0000 (09:54 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 24 Apr 2014 08:36:47 +0000 (10:36 +0200)
src/app/Application.cpp
src/app/Controller.cpp
src/collision.cpp
src/graphics/Vector.h
src/orbi.cpp
src/world/AABB.cpp
src/world/AABB.h
src/world/Collision.h [new file with mode: 0644]
src/world/World.cpp
src/world/World.h

index cbfafd374537935625dbaad63955d4d7edacbcd6..b6a08f115cc48b66d57367297380afad1b1a43a4 100644 (file)
@@ -148,15 +148,15 @@ void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
 
 void Application::Update(int dt) {
        const float delta = dt / 1e3;
-       ctrl.Update(delta);
+       for (int i = 0; i < dt; ++i) {
+               ctrl.Update(1e-3);
+               world.Update(1e-3);
+       }
        target.Update(delta);
        focus = ctrl.Controlling()
                ? ctrl.Controlled().bounds.Center()
                : target.Pos();
        cam.Update(delta);
-       for (int i = 0; i < dt; ++i) {
-               world.Update(1e-3);
-       }
 }
 
 
@@ -169,9 +169,13 @@ void Application::Render() {
 
 void Application::RenderBackground() {
        constexpr Color background(0x00, 0x00, 0x00);
+       constexpr Color outlineColor(0x00, 0x00, 0xFA);
 
        canvas.SetColor(background);
        canvas.Fill();
+
+       canvas.SetColor(outlineColor);
+       canvas.Grid(cam.ToScreen(Vector<int>(0, 0)), cam.ToScale(world.Size()), cam.ToScale(Vector<float>(1, 1)));
 }
 
 void Application::RenderWorld() {
@@ -197,12 +201,8 @@ void Application::RenderEntities() {
 }
 
 void Application::RenderUI() {
-       constexpr Color outlineColor(0x00, 0x00, 0xFA);
        constexpr Color targetColor(0xFA, 0xFA, 0x00);
 
-       canvas.SetColor(outlineColor);
-       canvas.Grid(cam.ToScreen(Vector<int>(0, 0)), cam.ToScale(world.Size()), cam.ToScale(Vector<float>(1, 1)));
-
        canvas.SetColor(targetColor);
        canvas.Cross(cam.ToScreen(target.Pos()), 15);
 }
index 88dd5eb725f6a39c4c2ebc85da8f9dd766e2bd1b..4d1bf0f31f0255bd4aafbed49bc027f8a984dce2 100644 (file)
@@ -14,7 +14,7 @@ Controller::Controller(Entity *ent)
 , moveAcc(3.5)
 , moveTerm(5)
 , jumping(false)
-, jumpVel(-5) {
+, jumpVel(-6) {
 
 }
 
index af4234aa10e7613335fe9a1bbb1d5fe50178dd3b..4212507f8fca29749862b077ebeeee8cd8348ee3 100644 (file)
@@ -5,6 +5,7 @@
 #include "graphics/Vector.h"
 #include "graphics/Window.h"
 #include "world/AABB.h"
+#include "world/Collision.h"
 
 #include <SDL.h>
 #include <vector>
@@ -27,11 +28,6 @@ struct World {
        AABB controlled;
        vector<AABB> stationary;
 
-       struct Collision {
-               Vector<float> pos;
-               Vector<float> norm;
-               Vector<float> depth;
-       };
        vector<Collision> coll;
 
        World()
@@ -42,171 +38,167 @@ struct World {
        , stationary()
        { }
 
-};
-
-void key_down(const SDL_KeyboardEvent &e, World &world) {
-       switch (e.keysym.sym) {
-               case SDLK_UP:
-                       world.move.y -= 1;
-                       break;
-               case SDLK_DOWN:
-                       world.move.y += 1;
-                       break;
-               case SDLK_LEFT:
-                       world.move.x -= 1;
-                       break;
-               case SDLK_RIGHT:
-                       world.move.x += 1;
-                       break;
-               default:
-                       break;
-       }
-}
 
-void key_up(const SDL_KeyboardEvent &e, World &world) {
-       switch (e.keysym.sym) {
-               case SDLK_UP:
-                       world.move.y += 1;
-                       break;
-               case SDLK_DOWN:
-                       world.move.y -= 1;
-                       break;
-               case SDLK_LEFT:
-                       world.move.x += 1;
-                       break;
-               case SDLK_RIGHT:
-                       world.move.x -= 1;
-                       break;
-               default:
-                       break;
+       void key_down(const SDL_KeyboardEvent &e) {
+               switch (e.keysym.sym) {
+                       case SDLK_UP:
+                               move.y -= 1;
+                               break;
+                       case SDLK_DOWN:
+                               move.y += 1;
+                               break;
+                       case SDLK_LEFT:
+                               move.x -= 1;
+                               break;
+                       case SDLK_RIGHT:
+                               move.x += 1;
+                               break;
+                       default:
+                               break;
+               }
        }
-}
 
-void handle(World &world) {
-       SDL_Event event;
-       while (SDL_PollEvent(&event)) {
-               switch (event.type) {
-                       case SDL_QUIT:
-                               world.alive = false;
+       void key_up(const SDL_KeyboardEvent &e) {
+               switch (e.keysym.sym) {
+                       case SDLK_UP:
+                               move.y += 1;
+                               break;
+                       case SDLK_DOWN:
+                               move.y -= 1;
                                break;
-                       case SDL_WINDOWEVENT:
-                               if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
-                                       world.cam.Resize(event.window.data1, event.window.data2);
-                               }
+                       case SDLK_LEFT:
+                               move.x += 1;
                                break;
-                       case SDL_KEYDOWN:
-                               if (!event.key.repeat) {
-                                       key_down(event.key, world);
-                               }
+                       case SDLK_RIGHT:
+                               move.x -= 1;
                                break;
-                       case SDL_KEYUP:
-                               if (!event.key.repeat) {
-                                       key_up(event.key, world);
-                               }
+                       default:
                                break;
                }
        }
-}
 
-void update(int dt, World &world) {
-       const Vector<float> speed { 5, 5 };
+       void handle() {
+               SDL_Event event;
+               while (SDL_PollEvent(&event)) {
+                       switch (event.type) {
+                               case SDL_QUIT:
+                                       alive = false;
+                                       break;
+                               case SDL_WINDOWEVENT:
+                                       if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
+                                               cam.Resize(event.window.data1, event.window.data2);
+                                       }
+                                       break;
+                               case SDL_KEYDOWN:
+                                       if (!event.key.repeat) {
+                                               key_down(event.key);
+                                       }
+                                       break;
+                               case SDL_KEYUP:
+                                       if (!event.key.repeat) {
+                                               key_up(event.key);
+                                       }
+                                       break;
+                       }
+               }
+       }
+
+       void update(int dt) {
+               const Vector<float> speed { 5, 5 };
 
-       const float delta = dt / 1e3;
+               const float delta = dt / 1e3;
 
-       world.controlled.Move(Vector<float>(world.move) * speed * delta);
-       world.focus = world.controlled.Center();
+               controlled.Move(Vector<float>(move) * speed * delta);
+               focus = controlled.Center();
 
-       world.coll.clear();
-       for (const AABB &e : world.stationary) {
-               World::Collision coll;
-               if (world.controlled.Intersects(
-                               e,
-                               coll.pos,
-                               coll.norm,
-                               coll.depth)) {
-                       world.coll.push_back(coll);
+               coll.clear();
+               for (const AABB &e : stationary) {
+                       Collision c;
+                       if (controlled.Intersects(e, c)) {
+                               coll.push_back(c);
+                       }
                }
        }
-}
 
-void render(Canvas &canvas, const World &world) {
-       constexpr Color background(0x00, 0x00, 0x00);
-       constexpr Color outlineColor(0x00, 0x00, 0xFA);
-       constexpr Color controlledColor(0xFA, 0xFA, 0x00);
-       constexpr Color entityColor(0x00, 0xFA, 0x00);
-       constexpr Color collisionColor(0xFA, 0x00, 0x00);
-       constexpr Color normalColor(0xFA, 0x00, 0x00);
-
-       canvas.SetColor(background);
-       canvas.Fill();
-
-       canvas.SetColor(outlineColor);
-       canvas.Grid(
-               world.cam.ToScreen(Vector<int>(0, 0)),
-               world.cam.ToScale(Vector<float>(10, 10)),
-               world.cam.ToScale(Vector<float>(1, 1)));
-
-       canvas.SetColor(entityColor);
-       for (const AABB &e : world.stationary) {
-               canvas.OutlineRect(
-                       world.cam.ToScreen(Vector<float>(e.Left(), e.Top())),
-                       world.cam.ToScale(e.Size()));
-       }
+       void render(Canvas &canvas) const {
+               constexpr Color background(0x00, 0x00, 0x00);
+               constexpr Color outlineColor(0x00, 0x00, 0xFA);
+               constexpr Color controlledColor(0xFA, 0xFA, 0x00);
+               constexpr Color entityColor(0x00, 0xFA, 0x00);
+               constexpr Color collisionColor(0xFA, 0x00, 0x00);
+               constexpr Color normalColor(0xFA, 0x00, 0x00);
+
+               canvas.SetColor(background);
+               canvas.Fill();
+
+               canvas.SetColor(outlineColor);
+               canvas.Grid(
+                       cam.ToScreen(Vector<int>(0, 0)),
+                       cam.ToScale(Vector<float>(10, 10)),
+                       cam.ToScale(Vector<float>(1, 1)));
+
+               canvas.SetColor(entityColor);
+               for (const AABB &e : stationary) {
+                       canvas.OutlineRect(
+                               cam.ToScreen(Vector<float>(e.Left(), e.Top())),
+                               cam.ToScale(e.Size()));
+               }
 
-       canvas.SetColor(controlledColor);
-       canvas.OutlineRect(
-               world.cam.ToScreen(Vector<float>(world.controlled.Left(), world.controlled.Top())),
-               world.cam.ToScale(world.controlled.Size()));
-
-       if (world.coll.empty()) return;
-
-       for (const World::Collision &c : world.coll) {
-               canvas.SetColor(collisionColor);
-               canvas.Arrow(
-                       world.cam.ToScreen(c.pos),
-                       world.cam.ToScreen(c.pos + c.depth));
-               canvas.SetColor(normalColor);
-               canvas.Arrow(
-                       world.cam.ToScreen(c.pos),
-                       world.cam.ToScreen(c.pos) + Vector<int>(c.norm * 25.0f));
+               canvas.SetColor(controlledColor);
+               canvas.OutlineRect(
+                       cam.ToScreen(Vector<float>(controlled.Left(), controlled.Top())),
+                       cam.ToScale(controlled.Size()));
+
+               if (coll.empty()) return;
+
+               for (const Collision &c : coll) {
+                       canvas.SetColor(collisionColor);
+                       canvas.Arrow(
+                               cam.ToScreen(c.pos),
+                               cam.ToScreen(c.pos + c.depth));
+                       canvas.SetColor(normalColor);
+                       canvas.Arrow(
+                               cam.ToScreen(c.pos),
+                               cam.ToScreen(c.pos) + Vector<int>(c.norm * 25.0f));
+               }
        }
-}
 
-void run(Canvas &canvas) {
-       World world;
-       world.cam.SetScale(Vector<float>(32, 32));
-       world.controlled.Resize(Vector<float>(2, 3));
-       world.controlled.Move(Vector<float>(1, 1.5));
-
-       AABB e;
-       e.Resize(Vector<float>(2, 2));
-       e.Move(Vector<float>(5, 5));
-       world.stationary.push_back(e);
-       e.Move(Vector<float>(0, 2));
-       world.stationary.push_back(e);
-       e.Move(Vector<float>(-2, 0));
-       world.stationary.push_back(e);
-
-       Uint32 last = SDL_GetTicks();
-       while (world.alive) {
-               handle(world);
-               Uint32 now = SDL_GetTicks();
-
-               int delta = now - last;
-               if (delta == 0) {
-                       SDL_Delay(1);
-                       continue;
-               } else if (delta > 30) {
-                       delta = 30;
+       void run(Canvas &canvas) {
+               cam.SetScale(Vector<float>(32, 32));
+               controlled.Resize(Vector<float>(2, 3));
+               controlled.Move(Vector<float>(1, 1.5));
+
+               AABB e;
+               e.Resize(Vector<float>(2, 2));
+               e.Move(Vector<float>(5, 5));
+               stationary.push_back(e);
+               e.Move(Vector<float>(0, 2));
+               stationary.push_back(e);
+               e.Move(Vector<float>(-2, 0));
+               stationary.push_back(e);
+
+               Uint32 last = SDL_GetTicks();
+               while (alive) {
+                       handle();
+                       Uint32 now = SDL_GetTicks();
+
+                       int delta = now - last;
+                       if (delta == 0) {
+                               SDL_Delay(1);
+                               continue;
+                       } else if (delta > 30) {
+                               delta = 30;
+                       }
+
+                       update(delta);
+                       render(canvas);
+
+                       canvas.Present();
+                       last = now;
                }
-
-               update(delta, world);
-               render(canvas, world);
-
-               canvas.Present();
-               last = now;
        }
-}
+
+};
 
 }
 
@@ -224,7 +216,8 @@ int main(int argc, const char *argv[]) {
                0
        ));
 
-       run(canv);
+       World world;
+       world.run(canv);
 
        return 0;
 }
index c355091c114e21fd789997ddef53dd4130e67725..0fd5bfe49f84d878a688df600b919b29f8cc367e 100644 (file)
@@ -163,6 +163,14 @@ template<class Scalar>
 constexpr Vector<Scalar> abs(Vector<Scalar> v) {
        return Vector<Scalar>(std::abs(v.x), std::abs(v.y));
 }
+template<class Scalar>
+constexpr Vector<Scalar> min(Vector<Scalar> lhs, Vector<Scalar> rhs) {
+       return Vector<Scalar>(std::min(lhs.x, rhs.x), std::min(lhs.y, rhs.y));
+}
+template<class Scalar>
+constexpr Vector<Scalar> max(Vector<Scalar> lhs, Vector<Scalar> rhs) {
+       return Vector<Scalar>(std::max(lhs.x, rhs.x), std::max(lhs.y, rhs.y));
+}
 
 
 template<class Scalar>
index b05244189b05a5c9535fee342f1cde49eb522d92..59c0e0c1cbdbf8ecd9254da156ce212a43d1bcb6 100644 (file)
@@ -48,8 +48,12 @@ int main(int argc, const char *argv[]) {
        world.SetTile(Vector<int>(9, 5), Tile(0));
        world.SetTile(Vector<int>(9, 6), Tile(0));
 
+       world.SetTile(Vector<int>(3, 8), Tile(0));
+       world.SetTile(Vector<int>(2, 9), Tile(0));
+       world.SetTile(Vector<int>(3, 9), Tile(0));
+
        Entity e;
-       e.bounds = AABB(Vector<float>(5, 0), Vector<float>(2, 3));
+       e.bounds = AABB(Vector<float>(5, 0), Vector<float>(1.9, 2.9));
        Entity &player = world.AddEntity(e);
 
        Application app(canv, world, tiles);
index c54537e16ee9f6e40b99b75e552a7f720fe2ab6e..0ed5a63934b8e1e11a9243a3e76b0a3255279253 100644 (file)
@@ -1,39 +1,46 @@
 #include "AABB.h"
 
+#include "Collision.h"
+
+
 namespace orbi {
 
-bool AABB::Intersects(const AABB &other, Vector<float> &p, Vector<float> &n, Vector<float> &d) const {
+bool AABB::Intersects(const AABB &other, Collision &coll) const {
        if (Bottom() < other.Top()) return false;
        if (other.Bottom() < Top()) return false;
        if (Right() < other.Left()) return false;
        if (other.Right() < Left()) return false;
 
        AABB diff;
-       diff.lt.x = std::max(Left(), other.Left());
-       diff.lt.y = std::max(Top(), other.Top());
-       diff.rb.x = std::min(Right(), other.Right());
-       diff.rb.y = std::min(Bottom(), other.Bottom());
+       diff.lt = max(lt, other.lt);
+       diff.rb = min(rb, other.rb);
        const Vector<float> sdiff = diff.Size();
 
        if (sdiff.x < sdiff.y) {
+               coll.pos.y = diff.Center().y;
+               coll.norm.y = 0;
+               coll.depth.y = 0;
                if (Center().x < other.Center().x) {
-                       p = Vector<float>(Right(), ((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2);
-                       n = Vector<float>(-1, 0);
-                       d = Vector<float>(other.Left() - Right(), 0);
+                       coll.pos.x = Right();
+                       coll.norm.x = -1;
+                       coll.depth.x = other.Left() - Right();
                } else {
-                       p = Vector<float>(Left(), ((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2);
-                       n = Vector<float>(1, 0);
-                       d = Vector<float>(other.Right() - Left(), 0);
+                       coll.pos.x = Left();
+                       coll.norm.x = 1;
+                       coll.depth.x = other.Right() - Left();
                }
        } else {
+               coll.pos.x = diff.Center().x;
+               coll.norm.x = 0;
+               coll.depth.x = 0;
                if (Center().y < other.Center().y) {
-                       p = Vector<float>(((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2, Bottom());
-                       n = Vector<float>(0, -1);
-                       d = Vector<float>(0, other.Top() - Bottom());
+                       coll.pos.y = Bottom();
+                       coll.norm.y = -1;
+                       coll.depth.y = other.Top() - Bottom();
                } else {
-                       p = Vector<float>(((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2, Top());
-                       n = Vector<float>(0, 1);
-                       d = Vector<float>(0, other.Bottom() - Top());
+                       coll.pos.y = Top();
+                       coll.norm.y = 1;
+                       coll.depth.y = other.Bottom() - Top();
                }
        }
        return true;
index 20580caaded42e57f994432de8890820cbcc9b2c..9a2d5ebbef4bbe4a3e7a9214d3c9ab9765805163 100644 (file)
@@ -6,6 +6,8 @@
 
 namespace orbi {
 
+class Collision;
+
 class AABB {
 
 public:
@@ -30,7 +32,7 @@ public:
        void Move(Vector<float> delta) { lt += delta; rb += delta; }
        void Resize(Vector<float> size) { *this = AABB(lt, size); }
 
-       bool Intersects(const AABB &other, Vector<float> &p, Vector<float> &n, Vector<float> &d) const;
+       bool Intersects(const AABB &other, Collision &) const;
 
 private:
        Vector<float> lt;
diff --git a/src/world/Collision.h b/src/world/Collision.h
new file mode 100644 (file)
index 0000000..740afa4
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef ORBI_COLLISION_H_
+#define ORBI_COLLISION_H_
+
+#include "../graphics/Vector.h"
+
+
+namespace orbi {
+
+struct Collision {
+
+       Vector<float> pos;
+       Vector<float> norm;
+       Vector<float> depth;
+
+};
+
+}
+
+#endif
index f4d80f95476beab74a2e32eac354f5f10c062659..7276ece6fdaa5b16519e3e24c0724e16a6b2d7f8 100644 (file)
@@ -1,5 +1,6 @@
 #include "World.h"
 
+#include "Collision.h"
 #include "../graphics/const.h"
 
 
@@ -50,17 +51,15 @@ void World::Update(float dt) {
                for (Vector<int> pos(cBegin); pos.y < cEnd.y; ++pos.y) {
                        for (pos.x = cBegin.x; pos.x < cEnd.x; ++pos.x) {
                                if (!TileAt(pos).IsSolid()) continue;
-                               const AABB tBounds(pos, Vector<float>(1, 1));
-                               Vector<float> pos;
-                               Vector<float> norm;
-                               Vector<float> depth;
-                               if (!e.bounds.Intersects(tBounds, pos, norm, depth)) {
+                               const AABB &tBounds = TileShapeAt(pos);
+                               Collision coll;
+                               if (!e.bounds.Intersects(tBounds, coll)) {
                                        continue;
                                }
-                               if (depth.x < min.x) min.x = depth.x;
-                               if (depth.x > max.x) max.x = depth.x;
-                               if (depth.y < min.y) min.y = depth.y;
-                               if (depth.y > max.y) max.y = depth.y;
+                               if (coll.depth.x < min.x) min.x = coll.depth.x;
+                               if (coll.depth.x > max.x) max.x = coll.depth.x;
+                               if (coll.depth.y < min.y) min.y = coll.depth.y;
+                               if (coll.depth.y > max.y) max.y = coll.depth.y;
                        }
                }
 
@@ -93,6 +92,12 @@ void World::Update(float dt) {
 }
 
 
+const AABB &World::TileShapeAt(Vector<int> pos) const {
+       tileShape = AABB(pos, Vector<float>(1, 1));
+       return tileShape;
+}
+
+
 Entity &World::AddEntity(const Entity &e) {
        entities.emplace_back(e);
        return entities.back();
index d77b15b3d55db40ed96822a852e32d7cae273438..7c982059514cfaee0924066727cde375fb89867e 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef ORBI_WORLD_H_
 #define ORBI_WORLD_H_
 
+#include "AABB.h"
 #include "Entity.h"
 #include "Tile.h"
 #include "../graphics/Vector.h"
@@ -29,6 +30,7 @@ public:
        Tile &TileAt(Vector<int> pos) { return tiles[Index(pos)]; }
        const Tile &TileAt(Vector<int> pos) const { return tiles[Index(pos)]; }
        void SetTile(Vector<int> pos, const Tile &t) { tiles[Index(pos)] = t; }
+       const AABB &TileShapeAt(Vector<int> pos) const;
 
        const std::list<Entity> &Entities() const { return entities; }
        Entity &AddEntity(const Entity &);
@@ -44,6 +46,8 @@ private:
 
        std::list<Entity> entities;
 
+       mutable AABB tileShape;
+
 };
 
 }