X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fshape%2FAABB.cpp;fp=src%2Fshape%2FAABB.cpp;h=75007ecbd5b42d3e62e53dbc84130995f7975098;hb=8b4877fe48d21d7e789cf52f81c1d6a87b06bcbc;hp=0000000000000000000000000000000000000000;hpb=42db59e3850c958821e8396fadc20fbeb71050c4;p=sdl-test8.git diff --git a/src/shape/AABB.cpp b/src/shape/AABB.cpp new file mode 100644 index 0000000..75007ec --- /dev/null +++ b/src/shape/AABB.cpp @@ -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() << ')'; +} + +}