-
Notifications
You must be signed in to change notification settings - Fork 9
/
utilities.cpp
47 lines (44 loc) · 1.79 KB
/
utilities.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
#include "utilities.h"
#include "Game.h"
#include <unordered_map>
sf::Vector2f RotatePoint(sf::Vector2f Point, sf::Vector2f Origin, double phi)
{
double c = std::cos(phi) ,s = std::sin(phi);
sf::Vector2f TranslatedPoint;
TranslatedPoint.x = (Origin.x + (Point.x - Origin.x) * c
- (Point.y - Origin.y) * s);
TranslatedPoint.y = (Origin.y + (Point.y - Origin.y) * c
+ (Point.x - Origin.x) * s);
return TranslatedPoint;
}
bool intersects(sf::CircleShape circle, sf::RectangleShape rect)
{
//turn the circle to (-angle of rect) so that rect OBB is now AABB
circle.setPosition(RotatePoint(circle.getPosition(),rect.getPosition(),TO_RAD(-rect.getRotation())));
sf::Vector2f circleDistance;
circleDistance.x = std::fabs(circle.getPosition().x - rect.getPosition().x);
circleDistance.y = std::fabs(circle.getPosition().y - rect.getPosition().y);
//check intersection with the edges
if (circleDistance.x > (rect.getLocalBounds().width/2 + circle.getRadius())) { return false; }
if (circleDistance.y > (rect.getLocalBounds().height/2 + circle.getRadius())) { return false; }
if (circleDistance.x <= (rect.getLocalBounds().width/2)) { return true; }
if (circleDistance.y <= (rect.getLocalBounds().height/2)) { return true; }
//check corner intersection
double cornerDistance_sq = sq(circleDistance.x - rect.getLocalBounds().width/2) +
sq(circleDistance.y - rect.getLocalBounds().height/2);
return (cornerDistance_sq <= sq(circle.getRadius()));
}
double cachedSqrt(int num)
{
static std::unordered_map<int,double> cache;
auto a = cache.find(num);
if(a != cache.end())
return a->second;
else
{
double temp = std::sqrt(num);
cache[num] = temp;
return temp;
}
return 0;
}