freak-engine/src/entity/collide.c

32 lines
729 B
C
Raw Permalink Normal View History

2021-06-11 01:32:47 +02:00
#include "entity.h"
2021-06-11 11:10:10 +02:00
struct Entity *
2021-06-11 12:47:16 +02:00
entity_collide(struct Entity *e, int mask)
2021-06-11 01:32:47 +02:00
{
int i;
i = ENTITY_GRID_SIZE;
while (i-- > 0)
2021-06-11 11:10:10 +02:00
if (entity_collide_with(e, g_entity_grid[i], mask))
return g_entity_grid[i];
2021-06-11 01:32:47 +02:00
2021-06-11 11:10:10 +02:00
return NULL;
2021-06-11 01:32:47 +02:00
}
/* axis aligned bounding box collision checking */
2021-06-11 12:47:16 +02:00
int
entity_collide_with(struct Entity *self, struct Entity *other, int mask)
2021-06-11 01:32:47 +02:00
{
const int sx = self->x + self->hb_x;
const int sy = self->y + self->hb_y;
const int ox = other->x + other->hb_x;
const int oy = other->y + other->hb_y;
2021-06-11 11:10:10 +02:00
if (self == NULL || other == NULL || self == other ||
!(mask & other->layer))
2021-06-11 01:32:47 +02:00
return 0;
return sx < ox + other->hb_w && sx + self->hb_w > ox &&
sy < oy + other->hb_h && sy + self->hb_h > oy;
}