From: Daniel Karbach Date: Tue, 6 May 2014 18:20:37 +0000 (+0200) Subject: addressed some float issues X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=7cf057b5b3a28c3896af27cb725fbf0b4f1459c2;p=orbi.git addressed some float issues --- diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 44a71cc..76ded2d 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -175,7 +175,7 @@ void Application::RenderBackground() { canvas.Fill(); canvas.SetColor(outlineColor); - canvas.Grid(cam.ToScreen(Vector(0, 0)), cam.ToScale(world.Size()), cam.ToScale(Vector(1, 1))); + canvas.Grid(cam.ToScreen(Vector(0, 0)), cam.ToScale(world.Size()), cam.ToScale(world.TileSize())); } void Application::RenderWorld() { @@ -203,20 +203,20 @@ void Application::RenderEntities() { canvas.SetColor(vboxColor); canvas.Line( cam.ToScreen(Vector(e.vbox.Left(), e.vbox.Top())), - cam.ToScreen(Vector(e.vbox.Right(), e.vbox.Top())) + cam.ToScreen(Vector(e.vbox.Right(), e.vbox.Top())) - Vector(1, 0) ); canvas.Line( - cam.ToScreen(Vector(e.vbox.Left(), e.vbox.Bottom())), - cam.ToScreen(Vector(e.vbox.Right(), e.vbox.Bottom())) + cam.ToScreen(Vector(e.vbox.Left(), e.vbox.Bottom())) - Vector(0, 1), + cam.ToScreen(Vector(e.vbox.Right(), e.vbox.Bottom())) - Vector(1, 1) ); canvas.SetColor(hboxColor); canvas.Line( cam.ToScreen(Vector(e.hbox.Left(), e.hbox.Top())), - cam.ToScreen(Vector(e.hbox.Left(), e.hbox.Bottom())) + cam.ToScreen(Vector(e.hbox.Left(), e.hbox.Bottom())) - Vector(0, 1) ); canvas.Line( - cam.ToScreen(Vector(e.hbox.Right(), e.hbox.Top())), - cam.ToScreen(Vector(e.hbox.Right(), e.hbox.Bottom())) + cam.ToScreen(Vector(e.hbox.Right(), e.hbox.Top())) - Vector(1, 0), + cam.ToScreen(Vector(e.hbox.Right(), e.hbox.Bottom())) - Vector(1, 1) ); } } diff --git a/src/graphics/Camera.h b/src/graphics/Camera.h index 7e8ba2f..c50042d 100644 --- a/src/graphics/Camera.h +++ b/src/graphics/Camera.h @@ -23,11 +23,11 @@ public: /// transform v from world coords to screen coords Vector ToScreen(Vector v) const { - return ToScale(v - *target) + offset; + return Vector(round(ToScale(v - *target))) + offset; } /// scale v from world to screen - Vector ToScale(Vector v) const { - return Vector(v * scale); + Vector ToScale(Vector v) const { + return Vector(v * scale); } /// transform v from screen coords to world coords diff --git a/src/graphics/Vector.h b/src/graphics/Vector.h index 0fd5bfe..6259b97 100644 --- a/src/graphics/Vector.h +++ b/src/graphics/Vector.h @@ -164,6 +164,14 @@ constexpr Vector abs(Vector v) { return Vector(std::abs(v.x), std::abs(v.y)); } template +inline Vector round(Vector v) { + return Vector(std::round(v.x), std::round(v.y)); +} +template<> +inline Vector round(Vector v) { + return Vector(std::roundf(v.x), std::roundf(v.y)); +} +template constexpr Vector min(Vector lhs, Vector rhs) { return Vector(std::min(lhs.x, rhs.x), std::min(lhs.y, rhs.y)); } diff --git a/src/orbi.cpp b/src/orbi.cpp index fae15e8..9197497 100644 --- a/src/orbi.cpp +++ b/src/orbi.cpp @@ -32,7 +32,7 @@ int main(int argc, const char *argv[]) { Tileset tiles( canv.LoadTexture(sdl.GetBasePath() + "../../data/test-tile.png"), - Vector(32, 32) + Vector(16, 16) ); World world(Vector(10, 10)); diff --git a/src/world/AABB.h b/src/world/AABB.h index a0e3436..5b482c4 100644 --- a/src/world/AABB.h +++ b/src/world/AABB.h @@ -13,9 +13,9 @@ class AABB { public: constexpr AABB() { } constexpr AABB(Vector pos, Vector size) - : lt(pos), rb(pos + size) { } + : size(size), lt(pos), rb(pos + size) { } constexpr AABB(float x, float y, float w, float h) - : lt(x, y), rb(x + w, y + h) { } + : size(w, h), lt(x, y), rb(x + w, y + h) { } public: float Left() const { return lt.x; } @@ -26,16 +26,17 @@ public: Vector Position() const { return lt; } Vector Center() const { return lt + HalfSize(); } Vector HalfSize() const { return Size() / 2.0f; } - Vector Size() const { return rb - lt; } + Vector Size() const { return size; } public: - void Move(Vector delta) { lt += delta; rb += delta; } + void Move(Vector delta) { lt += delta; rb = lt + size; } void Resize(Vector size) { *this = AABB(lt, size); } bool Intersects(const AABB &other) const; bool Intersects(const AABB &other, Collision &) const; private: + Vector size; Vector lt; Vector rb; diff --git a/src/world/Entity.cpp b/src/world/Entity.cpp index d7d6838..16c8491 100644 --- a/src/world/Entity.cpp +++ b/src/world/Entity.cpp @@ -11,6 +11,7 @@ void Entity::Update(float dt, Vector extAcc, Vector tv) { } void Entity::Move(Vector delta) { + pos += delta; bounds.Move(delta); vbox.Move(delta); hbox.Move(delta); diff --git a/src/world/Entity.h b/src/world/Entity.h index 53087b6..a7e45d2 100644 --- a/src/world/Entity.h +++ b/src/world/Entity.h @@ -17,11 +17,13 @@ public: void Move(Vector delta); public: + Vector pos; + Vector vel; + Vector acc; + AABB bounds; AABB vbox; AABB hbox; - Vector vel; - Vector acc; float mass = 1.0f; float elast = 0.75f; diff --git a/src/world/World.cpp b/src/world/World.cpp index aa17319..088712a 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -24,8 +24,12 @@ World::World(Vector size) void World::Update(float dt) { for (Entity &e : entities) { - e.Update(dt, gravity, terminal); - e.onGround = false; + if (e.onGround) { + e.Update(dt, Vector(), terminal); + e.onGround = false; + } else { + e.Update(dt, gravity, terminal); + } BoundsCollision(e, dt); TileCollision(e, dt); @@ -145,12 +149,6 @@ void World::EntityCollision() { } -const AABB &World::TileShapeAt(Vector pos) const { - tileShape = AABB(pos, Vector(1, 1)); - return tileShape; -} - - Entity &World::AddEntity(const Entity &e) { entities.emplace_back(e); return entities.back(); diff --git a/src/world/World.h b/src/world/World.h index cdfc5d5..6369757 100644 --- a/src/world/World.h +++ b/src/world/World.h @@ -19,6 +19,7 @@ public: public: Vector Size() const { return size; } + Vector TileSize() const { return Vector(1, 1); } public: void Update(float dt); @@ -30,7 +31,6 @@ public: Tile &TileAt(Vector pos) { return tiles[Index(pos)]; } const Tile &TileAt(Vector pos) const { return tiles[Index(pos)]; } void SetTile(Vector pos, const Tile &t) { tiles[Index(pos)] = t; } - const AABB &TileShapeAt(Vector pos) const; const std::list &Entities() const { return entities; } Entity &AddEntity(const Entity &);