-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionShapeCircle.cpp
More file actions
85 lines (77 loc) · 2.74 KB
/
Copy pathCollisionShapeCircle.cpp
File metadata and controls
85 lines (77 loc) · 2.74 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
#include "CollisionShapeCircle.h"
#include "typeinfo"
#include "CollisionShapeRect.h"
#include <math.h>
#include <iostream>
CollisionShapeCircle::CollisionShapeCircle(float _x, float _y, float r)
{
x = _x;
y = _y;
width = r;
height = r;
}
bool CollisionShapeCircle::isOverlap(CollisionShape * cs)
{
//std::cout<<"jas"<<std::endl;
CollisionShape * nonConstCS = cs;
if(typeid(*nonConstCS) == typeid(CollisionShapeCircle))
{
//std::cout<<"yes"<<std::endl;
float xDist = nonConstCS->getX() - x;
float yDist = nonConstCS->getY() - y;
//Check to see if the circles overlap
float distBetCenters = sqrt(pow(xDist,2.0) + pow(yDist,2.0));
float sumRadius = nonConstCS->getHeight() + height;
//When the sum of the radii is greater than the distance between the centers then they overlap
return distBetCenters <= sumRadius;
}
else if(typeid(*nonConstCS) == typeid(CollisionShapeRect))
{
//std::cout<<"no"<<std::endl;
//TODO finish this else if statement
//Check if the center of the circle is within the rectanglge
if(nonConstCS->getX() <= x && x <= nonConstCS->getX() + nonConstCS->getWidth()) //checks center of the circle in rectangle
{
if(nonConstCS->getY() >= y && y >= nonConstCS->getY() - nonConstCS->getHeight())
{
return true;
}
}
//Check if the line of the rectagle intersects with the circle
if(nonConstCS->getX() <= x + width && nonConstCS->getX() >= x - width) // checks the left corner is in the circle
{
if(nonConstCS->getY() <= y+sqrt(pow(width, 2.0) - pow(nonConstCS->getX(), 2.0))) //upper left corner
{
if(nonConstCS->getY() >= y-1*sqrt(pow(width, 2.0) - pow(nonConstCS->getX()-x, 2.0)))
{
return true;
}
}
if(nonConstCS->getY()-nonConstCS->getHeight() <= y+sqrt(pow(width, 2.0) - pow(nonConstCS->getX()-x, 2.0)))//bottom left cor
{
if(nonConstCS->getY()-nonConstCS->getHeight() >= y-1*sqrt(pow(width, 2.0) - pow(nonConstCS->getX()-x, 2.0)))
{
return true;
}
}
}
if(nonConstCS->getX()+ nonConstCS->getWidth() <= x + width && nonConstCS->getX()+ nonConstCS->getWidth() >= x - width) // checks the right corner is in the circle
{
if(nonConstCS->getY() <= y+sqrt(pow(width, 2.0) - pow(nonConstCS->getX()+ nonConstCS->getWidth()-x, 2.0))) //upper right corner
{
if(nonConstCS->getY() >= y-1.0*sqrt(pow(width, 2.0) - pow(nonConstCS->getX()+ nonConstCS->getWidth()-x, 2.0)))
{
return true;
}
}
if(nonConstCS->getY()-nonConstCS->getHeight() <= y+sqrt(pow(width, 2.0) - pow(nonConstCS->getX()+ nonConstCS->getWidth()-x, 2.0)))//bottom right cor
{
if(nonConstCS->getY()-nonConstCS->getHeight() >= y-1.0*sqrt(pow(width, 2.0) - pow(nonConstCS->getX()+ nonConstCS->getWidth()-x, 2.0)))
{
return true;
}
}
}
return false;
}
}