From 03b142b877e19a2355e1a79e279e024922d44655 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 1 May 2014 21:35:33 +0200 Subject: [PATCH] entity/entity collision stub --- src/app/Application.cpp | 8 +++++++- src/orbi.cpp | 12 ++++++++++-- src/world/AABB.cpp | 7 ++++++- src/world/AABB.h | 1 + src/world/Entity.cpp | 1 + src/world/Entity.h | 1 + src/world/World.cpp | 18 ++++++++++++++++++ src/world/World.h | 1 + 8 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 94901bf..44a71cc 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -190,10 +190,16 @@ void Application::RenderWorld() { } void Application::RenderEntities() { - constexpr Color vboxColor(0xFA, 0x00, 0x00); + constexpr Color boundsColor(0xFA, 0x00, 0x00); + constexpr Color vboxColor(0xFA, 0xFA, 0x00); constexpr Color hboxColor(0x00, 0xFA, 0x00); for (const Entity &e : world.Entities()) { + canvas.SetColor(boundsColor); + canvas.OutlineRect( + cam.ToScreen(Vector(e.bounds.Left(), e.bounds.Top())), + cam.ToScale(Vector(e.bounds.Size())) + ); canvas.SetColor(vboxColor); canvas.Line( cam.ToScreen(Vector(e.vbox.Left(), e.vbox.Top())), diff --git a/src/orbi.cpp b/src/orbi.cpp index 5775459..fae15e8 100644 --- a/src/orbi.cpp +++ b/src/orbi.cpp @@ -53,11 +53,19 @@ int main(int argc, const char *argv[]) { world.SetTile(Vector(3, 9), Tile(0)); Entity e; - e.vbox = AABB(Vector(.1, 0), Vector(1.8, 3)); - e.hbox = AABB(Vector(0, .1), Vector(2, 1.8)); + e.bounds = AABB(0, 0, 2, 3); + e.vbox = AABB(.1, 0, 1.8, 3); + e.hbox = AABB(0, .1, 2, 1.8); e.Move(Vector(5, 0)); Entity &player = world.AddEntity(e); + Entity mob; + mob.bounds = AABB(0, 0, 2, 1.5); + mob.vbox = mob.bounds; + mob.hbox = mob.bounds; + mob.Move(Vector(1, 0)); + world.AddEntity(mob); + Application app(canv, world, tiles); app.Control(player); app.Run(); diff --git a/src/world/AABB.cpp b/src/world/AABB.cpp index edce97e..92c8ba2 100644 --- a/src/world/AABB.cpp +++ b/src/world/AABB.cpp @@ -6,11 +6,16 @@ namespace orbi { -bool AABB::Intersects(const AABB &other, Collision &coll) const { +bool AABB::Intersects(const AABB &other) 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; + return true; +} + +bool AABB::Intersects(const AABB &other, Collision &coll) const { + if (!Intersects(other)) return false; AABB diff; diff.lt = max(lt, other.lt); diff --git a/src/world/AABB.h b/src/world/AABB.h index 9a2d5eb..a0e3436 100644 --- a/src/world/AABB.h +++ b/src/world/AABB.h @@ -32,6 +32,7 @@ public: void Move(Vector delta) { lt += delta; rb += delta; } void Resize(Vector size) { *this = AABB(lt, size); } + bool Intersects(const AABB &other) const; bool Intersects(const AABB &other, Collision &) const; private: diff --git a/src/world/Entity.cpp b/src/world/Entity.cpp index 8a26d7d..d7d6838 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) { + bounds.Move(delta); vbox.Move(delta); hbox.Move(delta); } diff --git a/src/world/Entity.h b/src/world/Entity.h index b0dcc69..53087b6 100644 --- a/src/world/Entity.h +++ b/src/world/Entity.h @@ -17,6 +17,7 @@ public: void Move(Vector delta); public: + AABB bounds; AABB vbox; AABB hbox; Vector vel; diff --git a/src/world/World.cpp b/src/world/World.cpp index c76f860..aa17319 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -6,6 +6,8 @@ #include #include +using namespace std; + namespace orbi { @@ -126,6 +128,22 @@ void World::TileCollision(Entity &e, float dt) { e.Move(response); } +void World::EntityCollision() { + if (entities.size() <= 1) return; + + auto firstEnd(entities.end()); + --firstEnd; + for (auto first(entities.begin()); first != firstEnd; ++first) { + auto second(first); + ++second; + for (auto secondEnd(entities.end()); second != secondEnd; ++second) { + if (first->bounds.Intersects(second->bounds)) { + // damage + knockback + } + } + } +} + const AABB &World::TileShapeAt(Vector pos) const { tileShape = AABB(pos, Vector(1, 1)); diff --git a/src/world/World.h b/src/world/World.h index 038f9dc..cdfc5d5 100644 --- a/src/world/World.h +++ b/src/world/World.h @@ -38,6 +38,7 @@ public: private: void BoundsCollision(Entity &, float dt); void TileCollision(Entity &, float dt); + void EntityCollision(); private: Vector size; -- 2.39.2