freak-engine/src/entity/collide.c

35 lines
820 B
C

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