-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collision.cpp
115 lines (82 loc) · 2.85 KB
/
Collision.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
#include"Collision.h"
bool np::Collision::BoundingBoxColl(sf::Sprite &object1, sf::Sprite &object2)
{
leftA = object1.GetPosition().x;
rightA = object1.GetPosition().x + object1.GetSubRect().GetWidth();
topA = object1.GetPosition().y;
bottomA = object1.GetPosition().y + object1.GetSubRect().GetHeight();
leftB = object2.GetPosition().x;
rightB = object2.GetPosition().x + object2.GetSubRect().GetWidth();
topB = object2.GetPosition().y;
bottomB = object2.GetPosition().y + object2.GetSubRect().GetHeight();
if(bottomA <= topB)
return false;
if(topA >= bottomB)
return false;
if(leftA >= rightB)
return false;
if(rightA <= leftB)
return false;
return true;
}
/*
bool np::Collision::CircuralColl(sf::Sprite &object1, sf::Sprite &object2)
{
int radious1,radious2;
int x1,y1,x2,y2;
//Get the current position of the sprite
x1 = object1.GetPosition().x;
x2 = object2.GetPosition().x;
y1 = object1.GetPosition().y;
y2 = object2.GetPosition().y;
//determine the radious of the first object
if(object1.GetSubRect().GetHeight() > object1.GetSubRect().GetWidth())
{
radious1 = object1.GetSubRect().GetHeight();
}
else
radious1 = object1.GetSubRect().GetWidth();
//determine the radious of the second object
if(object2.GetSubRect().GetHeight() > object2.GetSubRect().GetWidth())
{
radious2 = object2.GetSubRect().GetHeight();
}
else
radious2 = object2.GetSubRect().GetWidth();
//if the distance forom the two circles is less then the radious
if(DistanceBetwen2Points(x1,y1,x2,y2)< (radious1 + radious2))
return true;
return false;
}*/
bool np::Collision::PixelColl(sf::Sprite &object1, sf::Sprite &object2)
{
return true;
//will do later :)
}
bool np::Collision::Collided(np::Image object1, np::Image object2)
{
leftA = object1.getSprite()->GetPosition().x;
rightA = object1.getSprite()->GetPosition().x + object1.getSprite()->GetSubRect().GetWidth();
topA = object1.getSprite()->GetPosition().y;
bottomA = object1.getSprite()->GetPosition().y + object1.getSprite()->GetSubRect().GetHeight();
leftB = object2.getSprite()->GetPosition().x;
rightB = object2.getSprite()->GetPosition().x + object2.getSprite()->GetSubRect().GetWidth();
topB = object2.getSprite()->GetPosition().y;
bottomB = object2.getSprite()->GetPosition().y + object2.getSprite()->GetSubRect().GetHeight();
if(bottomA <= topB)
return false;
if(topA >= bottomB)
return false;
if(leftA >= rightB)
return false;
if(rightA <= leftB)
return false;
return true;
}
//Private function distance between two points
/*
int np::Collision::DistanceBetwen2Points(int x1, int y1, int x2, int y2)
{
return sqrt(std::pow(x2-x1,2)+std::pow(y2-y1,2));std::pow
}
*/