-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollider.cpp
More file actions
52 lines (46 loc) · 1.22 KB
/
Collider.cpp
File metadata and controls
52 lines (46 loc) · 1.22 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
#include "Collider.h"
#include <SFML/Graphics.hpp>
bool Collider::checkCollision(Collider other, float pushSpeed)
{
sf::Vector2f otherPosition = other.getPosition();
sf::Vector2f otherHalfSize = other.getHalfSize();
sf::Vector2f thisPosition = getPosition();
sf::Vector2f thisHalfSize = getHalfSize();
float dx = otherPosition.x - thisPosition.x;
float dy = otherPosition.y - thisPosition.y;
float intersectX = abs(dx) - (thisHalfSize.x + otherHalfSize.x);
float intersectY = abs(dy) - (thisHalfSize.y + otherHalfSize.y);
bool isCollision = ((intersectX < 0.0f) && (intersectY < 0.0f));
if (isCollision)
{
pushSpeed = std::min(std::max(pushSpeed, 0.0f), 1.0f);
if (intersectX > intersectY)
{
if (dx > 0.0f)
{
move(intersectX * (1.0f - pushSpeed), 0.0f);
other.move(-intersectX * pushSpeed, 0.0f);
}
else
{
move(-intersectX * (1.0f - pushSpeed), 0.0f);
other.move(intersectX * pushSpeed, 0.0f);
}
}
else
{
if (dy > 0.0f)
{
move(0.0f, intersectY * (1.0f - pushSpeed));
other.move(0.0f, -intersectY * pushSpeed);
}
else
{
move(0.0f, -intersectY * (1.0f - pushSpeed));
other.move(0.0f, intersectY * pushSpeed);
}
}
return true;
}
return false;
}