-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collision_type.cpp
160 lines (131 loc) · 5.14 KB
/
Collision_type.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "Collision_type.h"
bool have_collision(float i_x, float i_y, vector<vector<Cell>>& i_map, Collision_dierction direction, Cell cell_type)
{
if (direction == Collision_dierction::Up)
{
for (int y = 0; y < i_map.size(); y++)
{
for (int x = 0; x < i_map[y].size(); x++)
{
if(i_map[y][x] == cell_type && y*CELL_SIZE < i_y)
{
if ((i_x < (x+1)*CELL_SIZE && (x+1)*CELL_SIZE < i_x + CELL_SIZE) && (y+1) * CELL_SIZE >= i_y)
{
return true;
}
else if ((i_x < x*CELL_SIZE && x*CELL_SIZE < i_x + CELL_SIZE) && (y+1) * CELL_SIZE >= i_y)
{
return true;
}
else if ((i_x == x*CELL_SIZE && (x+1)*CELL_SIZE == i_x + CELL_SIZE) && (y+1)*CELL_SIZE >= i_y)
{
return true;
}
}
}
}
return false;
}
else if (direction == Collision_dierction::Down)
{
for (int y = 0; y < i_map.size(); y++)
{
for (int x = 0; x < i_map[y].size(); x++)
{
if(i_map[y][x] == cell_type && y*CELL_SIZE > i_y)
{
if ((i_x < (x+1)*CELL_SIZE && (x+1)*CELL_SIZE < i_x + CELL_SIZE) && (y)*CELL_SIZE <= i_y + CELL_SIZE)
{
return true;
}
else if ((i_x < x*CELL_SIZE && x*CELL_SIZE < i_x + CELL_SIZE) && (y) * CELL_SIZE <= i_y + CELL_SIZE)
{
return true;
}
else if ((i_x == x*CELL_SIZE && (x+1)*CELL_SIZE == i_x + CELL_SIZE) && (y)*CELL_SIZE <= i_y + CELL_SIZE)
{
return true;
}
}
}
}
return false;
}
else if (direction == Collision_dierction::Right)
{
for (int y = 0; y < i_map.size(); y++)
{
for (int x = 0; x < i_map[y].size(); x++)
{
if(i_map[y][x] == cell_type && x*CELL_SIZE > i_x)
{
if ((i_y < (y+1)*CELL_SIZE && (y+1)*CELL_SIZE < i_y + CELL_SIZE) && (x)*CELL_SIZE <= i_x + CELL_SIZE)
{
return true;
}
else if ((i_y < y*CELL_SIZE && y*CELL_SIZE + 10 < i_y + CELL_SIZE) && (x) * CELL_SIZE <= i_x + CELL_SIZE)
{
return true;
}
else if ((i_y == y*CELL_SIZE && (y+1)*CELL_SIZE == i_y + CELL_SIZE) && (x)*CELL_SIZE <= i_x + CELL_SIZE)
{
return true;
}
}
}
}
return false;
}
else if (direction == Collision_dierction::Left)
{
for (int y = 0; y < i_map.size(); y++)
{
for (int x = 0; x < i_map[y].size(); x++)
{
if(i_map[y][x] == cell_type && x*CELL_SIZE < i_x)
{
if ((i_y < (y+1)*CELL_SIZE && (y+1)*CELL_SIZE < i_y + CELL_SIZE) && (x+1) * CELL_SIZE >= i_x)
{
return true;
}
else if ((i_y < y*CELL_SIZE && y*CELL_SIZE + 10 < i_y + CELL_SIZE) && (x+1) * CELL_SIZE >= i_x)
{
return true;
}
else if ((i_y == y*CELL_SIZE && (y+1)*CELL_SIZE == i_y + CELL_SIZE) && (x+1)*CELL_SIZE >= i_x)
{
return true;
}
}
}
}
return false;
}
cout << "have collision function error\n";
return false;
}
bool have_collision_with_object(int object_x, int object_y, float turtle_x, float turtle_y)
{
float turtle_center_x = turtle_x + CELL_SIZE/2;
float turtle_center_y = turtle_y + CELL_SIZE/2;
if (object_x*CELL_SIZE < turtle_center_x && turtle_center_x < (object_x+1) * CELL_SIZE)
{
if (object_y*CELL_SIZE < turtle_center_y && turtle_center_y < (object_y+1) * CELL_SIZE)
{
return true;
}
}
return false;
}
Collision_dierction find_the_direction_of_collision(float turtle_x, float turtle_y, float object_x, float object_y)
{
if ((turtle_x <= object_x+CELL_SIZE && object_x+CELL_SIZE <= turtle_x ) && (turtle_y <= object_y && object_y <= turtle_y + CELL_SIZE))
{
return Collision_dierction::Down;
}
else if ((turtle_x < object_x && object_x < turtle_x + CELL_SIZE) && (turtle_y <= object_y && object_y <= turtle_y + CELL_SIZE))
{
return Collision_dierction::Down;
}
return Collision_dierction ::Left;
}