4 * Created on: Apr 30, 2012
14 void AABB::Translate(const Vector &delta) {
19 void AABB::Rotate(Scalar delta) {
20 // by definition AABBs cannot rotate
24 bool AABB::CheckCollision(const Shape &other, Ray &na) const {
25 // This call reveals the real type of this shape by overloading and the
26 // other one via polymorphism.
27 if (other.CheckCollision(*this, na)) {
28 // Since the other shape's collision check returns its own surface
29 // normal, it has to be inverted here.
37 bool AABB::CheckCollision(const AABB &other, Ray &na) const {
38 if (Bottom() <= other.Top()) return false;
39 if (other.Bottom() <= Top()) return false;
40 if (Right() <= other.Left()) return false;
41 if (other.Right() <= Left()) return false;
43 if (Bottom() <= other.Top() || other.Bottom() <= Top()) {
44 if (Left() < other.Left()) {
45 na.Origin() = Vector(((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2, Right());
46 na.Direction() = Vector(-1, 0);
48 na.Origin() = Vector(((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2, Left());
49 na.Direction() = Vector(1, 0);
52 if (Top() < other.Top()) {
53 na.Origin() = Vector(Bottom(), ((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2);
54 na.Direction() = Vector(0, -1);
56 na.Origin() = Vector(Top(), ((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2);
57 na.Direction() = Vector(0, 1);
63 bool AABB::CheckCollision(const Circle &other, Ray &na) const {
64 if (other.CheckCollision(*this, na)) {
73 std::ostream &AABB::Write(std::ostream &out) const {
74 return out << "AABB(" << Width() << 'x' << Height() << ", " << Center() << ')';