]> git.localhorst.tv Git - sdl-test8.git/blob - src/shape/AABB.cpp
some comments
[sdl-test8.git] / src / shape / AABB.cpp
1 /*
2  * AABB.cpp
3  *
4  *  Created on: Apr 30, 2012
5  *      Author: holy
6  */
7
8 #include "AABB.h"
9
10 #include "Circle.h"
11
12 namespace shape {
13
14 void AABB::Translate(const Vector &delta) {
15         leftTop += delta;
16         rightBottom += delta;
17 }
18
19 void AABB::Rotate(Scalar delta) {
20         // by definition AABBs cannot rotate
21 }
22
23
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.
30                 na.Direction() *= -1;
31                 return true;
32         } else {
33                 return false;
34         }
35 }
36
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;
42
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);
47                 } else {
48                         na.Origin() = Vector(((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2, Left());
49                         na.Direction() = Vector(1, 0);
50                 }
51         } else {
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);
55                 } else {
56                         na.Origin() = Vector(Top(), ((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2);
57                         na.Direction() = Vector(0, 1);
58                 }
59         }
60         return true;
61 }
62
63 bool AABB::CheckCollision(const Circle &other, Ray &na) const {
64         if (other.CheckCollision(*this, na)) {
65                 na.Direction() *= -1;
66                 return true;
67         } else {
68                 return false;
69         }
70 }
71
72
73 std::ostream &AABB::Write(std::ostream &out) const {
74         return out << "AABB(" << Width() << 'x' << Height() << ", " << Center() << ')';
75 }
76
77 }