/
collisions.cpp
119 lines (102 loc) · 2.12 KB
/
collisions.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
#include "gameinclude.h"
extern unit *enemy;
extern int enemy_size;
extern player mainPlayer;
extern bool _move;
int collide_with;
//collisons.cpp
//these are basic functions to check collisions, many overloaded funcs available
//CODES FOR COLLISION POINT MODIFIER(X && Y)
// -1 == move in left/down from P1 caused collision
// 1 == move in right/up from P1 cause collision
// 0 == no collision
//collide check function that is internal
bool theCollide(Point P1, Point P2, float r1, float r2)
{
bool x,y;
//check the x collision
//Set x and y in the Point C to their flags
if (P1.x > P2.x)
{
if ((P1.x - r1) < (P2.x + r2))
x = true;
else
x = false;
}
else
{
if ((P1.x + r1) > (P2.x - r2))
x = true;
else
x = false;
}
//check the y collision
//set bool y true if the y's cross
if (P1.y < P2.y)
{
if ((P1.y + r1) > (P2.y - r2))
y = true;
else
y = false;
}
else
{
if ((P1.y - r1) < (P2.y + r2))
y = true;
else
y = false;
}
if (x == true && y == true)
return true;
else
return false;
}
//player to unit
bool checkCollidePU(player P, unit U)
{
return (theCollide(P.getPosition(), U.getPosition(), P.getRadius(), U.getRadius()));
}
//player to player
bool checkCollidePP(player P1, player P2)
{
return (theCollide(P1.getPosition(), P2.getPosition(), P1.getRadius(), P2.getRadius()));
}
//unit to unit
bool checkCollideUU(unit U1, unit U2)
{
return (theCollide(U1.getPosition(), U2.getPosition(), U1.getRadius(), U2.getRadius()));
}
//player to hard code point
bool checkCollidePH(player P, float ox, float oy, float or)
{
Point p;
p.x = ox;
p.y = oy;
return theCollide(P.getPosition(), p, P.getRadius(), or);
}
//unit to hard codde point
bool checkCollideUH(unit U, float ox, float oy, float or)
{
Point p;
p.x = ox;
p.y = oy;
return theCollide(U.getPosition(), p, U.getRadius(), or);
}
bool checkCollisions()
{
bool collide = false;
float x, y;
if (_move == true)
{
for (int x=0; x < enemy_size; x++)
{
collide = checkCollidePU(mainPlayer, enemy[x]);
if (_move == true && collide == true)
{
collide_with = x;
x = enemy_size;
}
}
}
return collide;
}