]> git.localhorst.tv Git - sdl-test8.git/blob - src/shape/AABB.cpp
added collision engine, more or less stole gameplay from sdl-test7
[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
21 }
22
23
24 bool AABB::CheckCollision(const Shape &other, Ray &na) const {
25         if (other.CheckCollision(*this, na)) {
26                 na.Direction() *= -1;
27                 return true;
28         } else {
29                 return false;
30         }
31 }
32
33 bool AABB::CheckCollision(const AABB &other, Ray &na) const {
34         if (Bottom() <= other.Top()) return false;
35         if (other.Bottom() <= Top()) return false;
36         if (Right() <= other.Left()) return false;
37         if (other.Right() <= Left()) return false;
38
39         if (Bottom() <= other.Top() || other.Bottom() <= Top()) {
40                 if (Left() < other.Left()) {
41                         na.Origin() = Vector(((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2, Right());
42                         na.Direction() = Vector(-1, 0);
43                 } else {
44                         na.Origin() = Vector(((Top() + Bottom()) / 2 + (other.Top() + other.Bottom()) / 2) / 2, Left());
45                         na.Direction() = Vector(1, 0);
46                 }
47         } else {
48                 if (Top() < other.Top()) {
49                         na.Origin() = Vector(Bottom(), ((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2);
50                         na.Direction() = Vector(0, -1);
51                 } else {
52                         na.Origin() = Vector(Top(), ((Left() + Right()) / 2 + (other.Left() + other.Right()) / 2) / 2);
53                         na.Direction() = Vector(0, 1);
54                 }
55         }
56         return true;
57 }
58
59 bool AABB::CheckCollision(const Circle &other, Ray &na) const {
60         if (other.CheckCollision(*this, na)) {
61                 na.Direction() *= -1;
62                 return true;
63         } else {
64                 return false;
65         }
66 }
67
68
69 std::ostream &AABB::Write(std::ostream &out) const {
70         return out << "AABB(" << Width() << 'x' << Height() << ", " << Center() << ')';
71 }
72
73 }