-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollider.cpp
More file actions
57 lines (51 loc) · 1.25 KB
/
Copy pathCollider.cpp
File metadata and controls
57 lines (51 loc) · 1.25 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
#include "Collider.h"
Collider::Collider(RectangleShape& body) :
body(body)
{
}
Collider::~Collider()
{
}
bool Collider::CheckCollision(Collider other, float push)
{
Vector2f otherPosition = other.GetPosition();
Vector2f otherHalfSize = other.GetHalfSize();
Vector2f thisPosition = GetPosition();
Vector2f thisHalfSize = GetHalfSize();
float deltaX = otherPosition.x - thisPosition.x;
float deltaY = otherPosition.y - thisPosition.y;
float intersectX = abs(deltaX) - (otherHalfSize.x + thisHalfSize.x);
float intersectY = abs(deltaY) - (otherHalfSize.y + thisHalfSize.y);
if (intersectX < 0.0f && intersectY < 0.0f)
{
push = std::min(std::max(push, 0.0f), 1.0f);
if (intersectX > intersectY)
{
if (deltaX > 0.0f)
{
Move(intersectX * (1.0f - push), 0.0f);
other.Move(-intersectX * push, 0.0f);
}
else
{
Move(-intersectX * (1.0f - push), 0.0f);
other.Move(intersectX * push, 0.0f);
}
}
else
{
if (deltaY > 0.0f)
{
Move(0.0f,intersectY * (1.0f - push));
other.Move(0.0f, -intersectY * push);
}
else
{
Move(0.0f, -intersectY * (1.0f - push));
other.Move(0.0f, intersectY * push);
}
}
return true;
}
return false;
}