]> git.localhorst.tv Git - sdl-test8.git/blobdiff - src/shape/AABB.cpp
added collision engine, more or less stole gameplay from sdl-test7
[sdl-test8.git] / src / shape / AABB.cpp
diff --git a/src/shape/AABB.cpp b/src/shape/AABB.cpp
new file mode 100644 (file)
index 0000000..75007ec
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * AABB.cpp
+ *
+ *  Created on: Apr 30, 2012
+ *      Author: holy
+ */
+
+#include "AABB.h"
+
+#include "Circle.h"
+
+namespace shape {
+
+void AABB::Translate(const Vector &delta) {
+       leftTop += delta;
+       rightBottom += delta;
+}
+
+void AABB::Rotate(Scalar delta) {
+
+}
+
+
+bool AABB::CheckCollision(const Shape &other, Ray &na) const {
+       if (other.CheckCollision(*this, na)) {
+               na.Direction() *= -1;
+               return true;
+       } else {
+               return false;
+       }
+}
+
+bool AABB::CheckCollision(const AABB &other, Ray &na) 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;
+
+       if (Bottom() <= other.Top() || other.Bottom() <= Top()) {
+               if (Left() < other.Left()) {
+                       na.Origin() = Vector(((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2, Right());
+                       na.Direction() = Vector(-1, 0);
+               } else {
+                       na.Origin() = Vector(((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2, Left());
+                       na.Direction() = Vector(1, 0);
+               }
+       } else {
+               if (Top() < other.Top()) {
+                       na.Origin() = Vector(Bottom(), ((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2);
+                       na.Direction() = Vector(0, -1);
+               } else {
+                       na.Origin() = Vector(Top(), ((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2);
+                       na.Direction() = Vector(0, 1);
+               }
+       }
+       return true;
+}
+
+bool AABB::CheckCollision(const Circle &other, Ray &na) const {
+       if (other.CheckCollision(*this, na)) {
+               na.Direction() *= -1;
+               return true;
+       } else {
+               return false;
+       }
+}
+
+
+std::ostream &AABB::Write(std::ostream &out) const {
+       return out << "AABB(" << Width() << 'x' << Height() << ", " << Center() << ')';
+}
+
+}