]> git.localhorst.tv Git - sdl-test8.git/blob - src/shape/Circle.cpp
added collision engine, more or less stole gameplay from sdl-test7
[sdl-test8.git] / src / shape / Circle.cpp
1 /*
2  * Circle.cpp
3  *
4  *  Created on: Apr 29, 2012
5  *      Author: holy
6  */
7
8 #include "Circle.h"
9
10 #include "AABB.h"
11
12 namespace shape {
13
14 void Circle::Translate(const Vector &delta) {
15         center += delta;
16 }
17
18 void Circle::Rotate(Scalar delta) {
19
20 }
21
22
23 bool Circle::CheckCollision(const Shape &other, Ray &na) const {
24         if (other.CheckCollision(*this, na)) {
25                 na.Direction() *= -1;
26                 return true;
27         } else {
28                 return false;
29         }
30 }
31
32 bool Circle::CheckCollision(const AABB &other, Ray &na) const {
33         int xZone(
34                         center.X() < other.Left() ? 0 : (
35                         other.Right() < center.X() ? 2 : 1));
36         int yZone(
37                                 center.Y() < other.Top() ? 0 : (
38                                 other.Bottom() < center.Y() ? 2 : 1));
39         int zone(xZone + 3 * yZone);
40
41         if (zone == 4) {
42                 na.Origin() = center;
43                 na.Direction() = (center - other.Center()).Unit();
44                 return true;
45         }
46
47         if (zone % 2) {
48                 if (xZone == 1) { // vertical
49                         if (Bottom() < other.Top()) return false;
50                         if (other.Bottom() < Top()) return false;
51
52                         if (Bottom() < other.Bottom()) {
53                                 na.Origin() = Vector(center.X(), Bottom());
54                                 na.Direction() = Vector(0, -1);
55                         } else {
56                                 na.Origin() = Vector(center.X(), Top());
57                                 na.Direction() = Vector(0, 1);
58                         }
59
60                         return true;
61                 } else {
62                         if (Right() < other.Left()) return false;
63                         if (other.Right() < Left()) return false;
64
65                         if (Left() < other.Left()) {
66                                 na.Origin() = Vector(Left(), center.Y());
67                                 na.Direction() = Vector(-1, 0);
68                         } else {
69                                 na.Origin() = Vector(Right(), center.Y());
70                                 na.Direction() = Vector(1, 0);
71                         }
72
73                         return true;
74                 }
75         } else {
76                 Vector near(
77                                 yZone ? other.Right() : other.Left(),
78                                 xZone ? other.Bottom() : other.Top());
79                 Vector distance(Center() - near);
80                 Scalar distanceSquared(distance.LengthSquared());
81                 if (distanceSquared < (Radius() * Radius())) {
82                         na.Origin() = near;
83                         na.Direction() = distance.Unit();
84                         return true;
85                 } else {
86                         return false;
87                 }
88         }
89 }
90
91 bool Circle::CheckCollision(const Circle &other, Ray &na) const {
92         const Vector distance(center - other.center);
93         const Scalar distanceSquared(distance.LengthSquared());
94         const Scalar radii(radius + other.radius);
95
96         if (distanceSquared < (radii * radii)) {
97                 na.Direction() = distance.Unit();
98                 na.Origin() = center + (na.Direction() * radius);
99                 return true;
100         } else {
101                 return false;
102         }
103 }
104
105
106 std::ostream &Circle::Write(std::ostream &out) const {
107         return out << "Circle(" << Radius() << ", " << Center() << ')';
108 }
109
110 }