]> git.localhorst.tv Git - orbi.git/commitdiff
entity/entity collision stub
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 1 May 2014 19:35:33 +0000 (21:35 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 1 May 2014 19:35:33 +0000 (21:35 +0200)
src/app/Application.cpp
src/orbi.cpp
src/world/AABB.cpp
src/world/AABB.h
src/world/Entity.cpp
src/world/Entity.h
src/world/World.cpp
src/world/World.h

index 94901bfd44df41c8f431931f8049132c5ea40a2a..44a71ccd7f5fddb33f06c0873c3228e2c1fd00d1 100644 (file)
@@ -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<float>(e.bounds.Left(), e.bounds.Top())),
+                       cam.ToScale(Vector<float>(e.bounds.Size()))
+               );
                canvas.SetColor(vboxColor);
                canvas.Line(
                        cam.ToScreen(Vector<float>(e.vbox.Left(), e.vbox.Top())),
index 5775459dd5a6f5f6c9a95c578b01d46d4c80c13b..fae15e87e8b53d9d09ac7796a5131da7b5682dd2 100644 (file)
@@ -53,11 +53,19 @@ int main(int argc, const char *argv[]) {
        world.SetTile(Vector<int>(3, 9), Tile(0));
 
        Entity e;
-       e.vbox = AABB(Vector<float>(.1, 0), Vector<float>(1.8, 3));
-       e.hbox = AABB(Vector<float>(0, .1), Vector<float>(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<float>(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<float>(1, 0));
+       world.AddEntity(mob);
+
        Application app(canv, world, tiles);
        app.Control(player);
        app.Run();
index edce97e313dfe3f832dab4204f6ca0d100dc7643..92c8ba2014f017632c54e7c50074010f8f973a86 100644 (file)
@@ -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);
index 9a2d5ebbef4bbe4a3e7a9214d3c9ab9765805163..a0e3436194fd57dbee28b06682babc553ef6edd2 100644 (file)
@@ -32,6 +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) const;
        bool Intersects(const AABB &other, Collision &) const;
 
 private:
index 8a26d7d2bbed2a0998ce92f376aa8c2d85833a1c..d7d683830d6920132108f0c9f0363ce5c7823a9a 100644 (file)
@@ -11,6 +11,7 @@ void Entity::Update(float dt, Vector<float> extAcc, Vector<float> tv) {
 }
 
 void Entity::Move(Vector<float> delta) {
+       bounds.Move(delta);
        vbox.Move(delta);
        hbox.Move(delta);
 }
index b0dcc69b50803753842bd145715761678f110b65..53087b6972ef8e80cab44debef75c0373b1622a0 100644 (file)
@@ -17,6 +17,7 @@ public:
        void Move(Vector<float> delta);
 
 public:
+       AABB bounds;
        AABB vbox;
        AABB hbox;
        Vector<float> vel;
index c76f8608bb82b30c4aaadda76413658df2fe0999..aa17319cd8dfca6cd9e91d1fcddd7b66ce0e064c 100644 (file)
@@ -6,6 +6,8 @@
 #include <algorithm>
 #include <cmath>
 
+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<int> pos) const {
        tileShape = AABB(pos, Vector<float>(1, 1));
index 038f9dcce39db83362bea625a72f13384f041ef8..cdfc5d5d807072e2ba546639e9d3b0fe9b10d479 100644 (file)
@@ -38,6 +38,7 @@ public:
 private:
        void BoundsCollision(Entity &, float dt);
        void TileCollision(Entity &, float dt);
+       void EntityCollision();
 
 private:
        Vector<int> size;