-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollisions.h
executable file
·44 lines (40 loc) · 1.08 KB
/
collisions.h
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
// copied from the arduboy2 library and modified to use references
bool collide(Rect const &rect1, Rect const &rect2)
{
return !(rect2.x >= rect1.x + rect1.width ||
rect2.x + rect2.width <= rect1.x ||
rect2.y >= rect1.y + rect1.height ||
rect2.y + rect2.height <= rect1.y);
}
void separate(Solid &first, Solid const &second)
{
float translateX = 0;
float translateY = 0;
// find x translation distance
if (first.cbox.x < second.cbox.x)
{
translateX = (second.cbox.x - first.cbox_conf.width - first.cbox_conf.x + 1) - first.x;
}
else
{
translateX = (second.cbox.x + second.cbox.width - first.cbox_conf.x) - first.x;
}
// find y translation distance
if (first.cbox.y < second.cbox.y)
{
translateY = (second.cbox.y - first.cbox_conf.height - first.cbox_conf.y + 1) - first.y;
}
else
{
translateY = (second.cbox.y + second.cbox.height - first.cbox_conf.y) - first.y;
}
// apply the translation
if (abs(translateX) < abs(translateY))
{
first.x += translateX;
}
else
{
first.y += translateY;
}
}