freak-engine/src/entity/collide.c

32 lines
729 B
C

#include "entity.h"
struct Entity *
entity_collide(struct Entity *e, int mask)
{
int i;
i = ENTITY_GRID_SIZE;
while (i-- > 0)
if (entity_collide_with(e, g_entity_grid[i], mask))
return g_entity_grid[i];
return NULL;
}
/* axis aligned bounding box collision checking */
int
entity_collide_with(struct Entity *self, struct Entity *other, int mask)
{
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;
if (self == NULL || other == NULL || self == other ||
!(mask & other->layer))
return 0;
return sx < ox + other->hb_w && sx + self->hb_w > ox &&
sy < oy + other->hb_h && sy + self->hb_h > oy;
}