-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionShapeRect.cpp
More file actions
130 lines (124 loc) · 4.38 KB
/
Copy pathCollisionShapeRect.cpp
File metadata and controls
130 lines (124 loc) · 4.38 KB
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
#include <math.h>
#include "typeinfo"
#include "CollisionShapeRect.h"
#include "CollisionShapeCircle.h"
#include <iostream>
CollisionShapeRect::CollisionShapeRect(float _x, float _y, float w, float h)
{
x = _x;
y = _y;
width = w;
height = h;
}
bool CollisionShapeRect::isOverlap(CollisionShape * cs)
{
CollisionShape * nonConstCS = cs;
if(typeid(*nonConstCS) == typeid(CollisionShapeRect))
{
//std::cout<<"yesrect"<<std::endl;
/*if(x <= nonConstCS->getX() && x+width >= nonConstCS->getX())//checks the rectangle is inside the rectangle
{
//cout << "CHECKING A" << endl;
if(y <= nonConstCS->getY() && y+height >= nonConstCS->getY())
{
return true;
}
}*/
if(nonConstCS->getX() <= x && nonConstCS->getX()+nonConstCS->getWidth() >= x) //left corner
{
if(nonConstCS->getY() <= y && nonConstCS->getY()+nonConstCS->getHeight() >= y) //upper left in the rectangle
{
return true;
}
/*bottom left*/ else if(nonConstCS->getY() <=y+height && nonConstCS->getY()+nonConstCS->getHeight() >= y+height)
{
return true;
}
}
else if(nonConstCS->getX() <= x+width && nonConstCS->getX()+nonConstCS->getWidth() >= x+width)//right corner
{
if(nonConstCS->getY() <= y && nonConstCS->getY()+nonConstCS->getHeight() >= y) //upper right in the rectangle
{
return true;
}
/*bottom right*/ else if(nonConstCS->getY() <=y+height && nonConstCS->getY()+nonConstCS->getHeight() >= y+height)
{
//printf("x: %f, y: %f, w: %f, h: %f\n", x, y, width, height);
//printf("player x: %f, y: %f, w: %f, h: %f\n", nonConstCS->getX(), nonConstCS->getY(), nonConstCS->getWidth(), nonConstCS->getHeight());
return true;
}
}
if(nonConstCS->getX() <= x && nonConstCS->getX()+ nonConstCS->getWidth() >= x+width)//if its between the x but goes through the rectangle
{
if(nonConstCS->getY() >= y && nonConstCS->getY()+ nonConstCS->getHeight() <= y+height)
{
return true;
}
/*else if(nonConstCS->getY() <= y && nonConstCS->getY()+nonConstCS->getHeight() <= y+height)//vertical rectangle
{
return true;
}*/
}
if(nonConstCS->getY() <= y && nonConstCS->getY()+ nonConstCS->getHeight() >= y+height)//same as last one but y is between the rectangle like horizontal
{
if(nonConstCS->getX() >= x && nonConstCS->getX() + nonConstCS->getWidth() <= x+width)
{
return true;
}
else if(nonConstCS->getX() <= x+width && nonConstCS->getX()+ nonConstCS->getWidth() >= x+width)
{
return true;
}
}
return false;
}
else if(typeid(*nonConstCS) == typeid(CollisionShapeCircle))
{
//std::cout<<"norect"<<std::endl;
//TODO finish this else if statement
//Check if the center of the circle is within the rectanglge
if(nonConstCS->getX() >= x && x+width >= nonConstCS->getX()) //checks center of the circle in rectangle
{
if(nonConstCS->getY() <= y && y-height <= nonConstCS->getY())
{
return true;
}
}
//Check if the line of the rectagle intersects with the circle
if(nonConstCS->getX() + nonConstCS->getWidth()>= x && nonConstCS->getX()-nonConstCS->getWidth()<= x)// checks the left corner is in the circle
{
if(y <= nonConstCS->getY()+sqrt(pow(nonConstCS->getWidth(), 2.0) - pow(x-nonConstCS->getX(), 2.0))) //upper left corner
{
if(y >= nonConstCS->getY()-1.0*sqrt(pow(nonConstCS->getWidth(), 2.0) - pow(x-nonConstCS->getX(), 2.0)))
{
return true;
}
}
else if(y-height <= nonConstCS->getY()+sqrt(pow(nonConstCS->getWidth(), 2.0) - pow(x-nonConstCS->getX(), 2.0)))//bottom left cor
{
if(y-height >= nonConstCS->getY()-1.0*sqrt(pow(nonConstCS->getWidth(), 2.0) - pow(x-nonConstCS->getX(), 2.0)))
{
return true;
}
}
}
if(x+width <= nonConstCS->getX() + nonConstCS->getWidth() && x+width >= nonConstCS->getX() - nonConstCS->getWidth()) // checks the right corner is in the circle
{
if(y <= nonConstCS->getY()+sqrt(pow(nonConstCS->getWidth(), 2.0) - pow(x+width-nonConstCS->getX(), 2.0))) //upper right corner
{
if(y >= nonConstCS->getY()-1.0*sqrt(pow(nonConstCS->getWidth(), 2.0) - pow(x+width-nonConstCS->getX(), 2.0)))
{
return true;
}
}
else if(y-height <= nonConstCS->getY()+sqrt(pow(nonConstCS->getWidth(), 2.0) - pow(x+width-nonConstCS->getX(), 2.0)))//bottom right corner
{
if(y-height >= nonConstCS->getY()-1.0*sqrt(pow(nonConstCS->getWidth(), 2.0) - pow(x+width-nonConstCS->getX(), 2.0)))
{
return true;
}
}
}
return false;
}
}