-
Notifications
You must be signed in to change notification settings - Fork 1
/
collisions.c
53 lines (46 loc) · 1.26 KB
/
collisions.c
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
#include "game.h"
void detect_collisions()
{
int i, j;
for(i = 0; i < num_entities; i++)
{
for(j = 0; j < num_entities; j++)
{
if (j != i)
{
entity_t *e1 = (entity_t*)entities[i];
entity_t *e2 = (entity_t*)entities[j];
if (e1->type != e2->type &&
e1->x < e2->x + e2->w &&
e1->x + e1->w > e2->x &&
e1->y < e2->y + e2->h &&
e1->h + e1->y > e2->y &&
e1->on_collide != NULL)
{
int e1cx = e1->x + e1->w / 2;
int e2cx = e2->x + e2->w / 2;
int dx = e1cx < e2cx ? e2->x - (e1->x + e1->w) : (e2->x + e2->w) - e1->x;
int e1cy = e1->y + e1->h / 2;
int e2cy = e2->y + e2->h / 2;
int dy = e1cy < e2cy ? e2->y - (e1->y + e1->h) : (e2->y + e2->h) - e1->y;
e1->on_collide(e1, e2, dx, dy);
}
}
}
}
}
bool solid_at(int x, int y)
{
int i;
for(i = 0; i < num_entities; i++)
{
entity_t *e1 = (entity_t*)entities[i];
if (x >= e1->x && x < e1->x + e1->w
&& y >= e1->y && y < e1->y + e1->h
&& e1->type == 1)
{
return true;
}
}
return false;
}