]> git.localhorst.tv Git - sdl-test7.git/blob - src/geometry/AABB.cpp
added some comments
[sdl-test7.git] / src / geometry / AABB.cpp
1 /*
2  * AABB.cpp
3  *
4  *  Created on: Apr 20, 2012
5  *      Author: holy
6  */
7
8 #include "AABB.h"
9
10 #include "Circle.h"
11 #include "FakeLens.h"
12
13 namespace geometry {
14
15 bool AABB::Overlaps(const Shape &other, Vector &normal) const {
16         // This call reveals the real type of this shape by overloading and the
17         // other one via polymorphism.
18         if (other.Overlaps(*this, normal)) {
19                 // Since the other shape's Overlaps() returns its own surface normal,
20                 // it has to be inverted here.
21                 normal *= -1;
22                 return true;
23         } else {
24                 return false;
25         }
26 }
27
28 bool AABB::Overlaps(const AABB &other, Vector &normal) const {
29         if (Bottom() <= other.Top()) return false;
30         if (other.Bottom() <= Top()) return false;
31         if (Right() <= other.Left()) return false;
32         if (other.Right() <= Left()) return false;
33
34         if (HorizontalOverlap(other)) {
35                 if (Top() < other.Top()) {
36                         normal = Vector(0, -1);
37                 } else {
38                         normal = Vector(0, 1);
39                 }
40         } else {
41                 if (Left() < other.Left()) {
42                         normal = Vector(-1, 0);
43                 } else {
44                         normal = Vector(1, 0);
45                 }
46         }
47
48         return true;
49 }
50
51 bool AABB::Overlaps(const Circle &other, Vector &normal) const {
52         if (other.Overlaps(*this, normal)) {
53                 normal *= -1;
54                 return true;
55         } else {
56                 return false;
57         }
58 }
59
60 bool AABB::Overlaps(const FakeLens &other, Vector &normal) const {
61         if (other.Overlaps(*this, normal)) {
62                 normal *= -1;
63                 return true;
64         } else {
65                 return false;
66         }
67 }
68
69 std::ostream &AABB::Write(std::ostream &out) const {
70         return out << "AABB(" << Width() << 'x' << Height() << '+' << X() << '+' << Y() << ')';
71 }
72
73 } /* namespace geometry */