-
Notifications
You must be signed in to change notification settings - Fork 2
/
collision.h
101 lines (80 loc) · 2.8 KB
/
collision.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
bool entityCheckPlayerCollision(Entity* e, Player* p)
{
if(e->active)
return checkRectangleIntersectSprite(&p->hitBox, &p->position, &e->sprite, &e->position);
return false;
}
bool entityCheckPlayerCollisionAtOffset(Entity* e, Player* p, int x, int y)
{
e->position.x += x;
e->position.y += y;
bool result;
if(e->active)
result = checkRectangleIntersectSprite(&p->hitBox, &p->position, &e->sprite, &e->position);
e->position.x -= x;
e->position.y -= y;
return result;
}
Entity* rectangleCheckCollision(Rectanglef* r, Iwbtg* iw, EntityType type)
{
Vector2f offset = { 0.0f, 0.0f };
for(int i = 0; i < MAX_ENTITIES; ++i)
{
Entity* e = &iw->entities[i];
if(e->active && e->type == type)
{
if(checkRectangleIntersectSprite(r, &offset, &e->sprite, &e->position))
return e;
}
}
return 0;
}
Entity* playerCheckCollision(Player* p, Iwbtg* iw, EntityType type)
{
for(int i = 0; i < MAX_ENTITIES; ++i)
{
Entity* e = &iw->entities[i];
if(e->active && e->type == type)
{
if(checkRectangleIntersectSprite(&p->hitBox, &p->position, &e->sprite, &e->position))
return e;
}
}
return 0;
}
Entity* playerCheckCollisionAtOffset(Player* p, Iwbtg* iw, EntityType type, int x, int y)
{
Vector2f pos = p->position;
p->position.x += x;
p->position.y += y;
Entity* result = playerCheckCollision(p, iw, type);
p->position = pos;
return result;
}
bool rectangleIsCollidingWithGround(Rectanglef* r, Iwbtg* iw, float offsetX, float offsetY)
{
Rectanglef block = { 0.0f, 0.0f, 32.0f, 32.0f };
int px = (int)(offsetX);
int py = (int)(offsetY);
int fx = (px / 32) - 1;
int fy = (py / 32) - 1;
int tx = (px / 32) + 2;
int ty = (py / 32) + 2;
fx = clampi(fx, 0, iw->level.ground.width);
fy = clampi(fy, 0, iw->level.ground.height);
tx = clampi(tx, 0, iw->level.ground.width);
ty = clampi(ty, 0, iw->level.ground.height);
for(int i = fx; i < tx; ++i)
for(int t = fy; t < ty; ++t)
if(iw->level.ground.data[i + (t * iw->level.ground.width)] == 1
&& rectanglefIntersectAt(offsetX, offsetY, r,
i * 32, t * 32, &block))
return true;
return false;
}
bool playerIsCollidingWithGround(Player* p, Iwbtg* iw, float offsetX, float offsetY)
{
return rectangleIsCollidingWithGround(&p->hitBox, iw, p->position.x + offsetX, p->position.y + offsetY)
|| (playerCheckCollisionAtOffset(p, iw, EntityType_movingPlatform, offsetX, offsetY)
&& !playerCheckCollisionAtOffset(p, iw, EntityType_movingPlatform, offsetX, offsetY-1) && p->velocity.y >= 0);
}