Allow null input for overlap for AABB check

scene_man
En Yi 2023-09-25 22:08:31 +08:00
parent ce8284ab39
commit 26fa9af6b8
2 changed files with 9 additions and 11 deletions

View File

@ -23,19 +23,21 @@ uint8_t find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl
// Note that we include one extra pixel for checking // Note that we include one extra pixel for checking
// This avoid overlapping on the border // This avoid overlapping on the border
Vector2 l1, l2; Vector2 l1, l2;
Vector2 tmp;
uint8_t overlap_x, overlap_y; uint8_t overlap_x, overlap_y;
l1.x = tl1.x; l1.x = tl1.x;
l1.y = tl1.x + sz1.x; l1.y = tl1.x + sz1.x;
l2.x = tl2.x; l2.x = tl2.x;
l2.y = tl2.x + sz2.x; l2.y = tl2.x + sz2.x;
overlap_x = find_1D_overlap(l1, l2, &overlap->x); overlap_x = find_1D_overlap(l1, l2, &tmp.x);
l1.x = tl1.y; l1.x = tl1.y;
l1.y = tl1.y + sz1.y; l1.y = tl1.y + sz1.y;
l2.x = tl2.y; l2.x = tl2.y;
l2.y = tl2.y + sz2.y; l2.y = tl2.y + sz2.y;
overlap_y = find_1D_overlap(l1, l2, &overlap->y); overlap_y = find_1D_overlap(l1, l2, &tmp.y);
if (overlap) *overlap = tmp;
if (overlap_x == 2 && overlap_y == 2) return 2; if (overlap_x == 2 && overlap_y == 2) return 2;
return (overlap_x < overlap_y) ? overlap_x : overlap_y; return (overlap_x < overlap_y) ? overlap_x : overlap_y;
} }

View File

@ -569,7 +569,6 @@ void spike_collision_system(Scene_t* scene)
tile_y1 = (tile_y1 < 0) ? 0 : tile_y1; tile_y1 = (tile_y1 < 0) ? 0 : tile_y1;
tile_y2 = (tile_y2 >= tilemap.height) ? tilemap.height - 1 : tile_y2; tile_y2 = (tile_y2 >= tilemap.height) ? tilemap.height - 1 : tile_y2;
Vector2 overlap;
for (unsigned int tile_y = tile_y1; tile_y <= tile_y2; tile_y++) for (unsigned int tile_y = tile_y1; tile_y <= tile_y2; tile_y++)
{ {
for (unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++) for (unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++)
@ -584,7 +583,7 @@ void spike_collision_system(Scene_t* scene)
tile_y * TILE_SIZE + tilemap.tiles[tile_idx].offset.y tile_y * TILE_SIZE + tilemap.tiles[tile_idx].offset.y
}, },
tilemap.tiles[tile_idx].size, tilemap.tiles[tile_idx].size,
&overlap); NULL);
if (collide) if (collide)
{ {
@ -1309,11 +1308,10 @@ void state_transition_update_system(Scene_t* scene)
uint32_t water_height = data->tilemap.tiles[tile_idx].water_level * WATER_BBOX_STEP; uint32_t water_height = data->tilemap.tiles[tile_idx].water_level * WATER_BBOX_STEP;
Vector2 tl = {tile_x * data->tilemap.tile_size, (tile_y + 1) * data->tilemap.tile_size - water_height}; Vector2 tl = {tile_x * data->tilemap.tile_size, (tile_y + 1) * data->tilemap.tile_size - water_height};
Vector2 overlap;
in_water |= find_AABB_overlap( in_water |= find_AABB_overlap(
p_ctransform->position, p_bbox->size, p_ctransform->position, p_bbox->size,
tl, (Vector2){data->tilemap.tile_size, water_height} tl, (Vector2){data->tilemap.tile_size, water_height}
, &overlap , NULL
) > 0; ) > 0;
} }
} }
@ -1422,7 +1420,6 @@ void hitbox_update_system(Scene_t* scene)
if (tile_idx >= tilemap.n_tiles) break; if (tile_idx >= tilemap.n_tiles) break;
unsigned int other_ent_idx; unsigned int other_ent_idx;
Entity_t* p_other_ent; Entity_t* p_other_ent;
Vector2 overlap;
if (tilemap.tiles[tile_idx].tile_type != EMPTY_TILE) if (tilemap.tiles[tile_idx].tile_type != EMPTY_TILE)
{ {
@ -1431,7 +1428,7 @@ void hitbox_update_system(Scene_t* scene)
if ( if (
find_AABB_overlap( find_AABB_overlap(
hitbox_pos, (Vector2){p_hitbox->boxes[i].width, p_hitbox->boxes[i].height}, hitbox_pos, (Vector2){p_hitbox->boxes[i].width, p_hitbox->boxes[i].height},
tile_pos, tilemap.tiles[tile_idx].size, &overlap tile_pos, tilemap.tiles[tile_idx].size, NULL
) )
) )
{ {
@ -1462,7 +1459,7 @@ void hitbox_update_system(Scene_t* scene)
if ( if (
find_AABB_overlap( find_AABB_overlap(
hitbox_pos, (Vector2){p_hitbox->boxes[i].width, p_hitbox->boxes[i].height}, hitbox_pos, (Vector2){p_hitbox->boxes[i].width, p_hitbox->boxes[i].height},
hurtbox_pos, p_other_hurtbox->size, &overlap hurtbox_pos, p_other_hurtbox->size, NULL
) )
) )
{ {
@ -1758,12 +1755,11 @@ void level_end_detection_system(Scene_t* scene)
CTransform_t* p_other_ct = get_component(p_other_ent, CTRANSFORM_COMP_T); CTransform_t* p_other_ct = get_component(p_other_ent, CTRANSFORM_COMP_T);
CBBox_t* p_other_bbox = get_component(p_other_ent, CBBOX_COMP_T); CBBox_t* p_other_bbox = get_component(p_other_ent, CBBOX_COMP_T);
Vector2 overlap;
Vector2 pos = Vector2Subtract(p_ct->position,(Vector2){tilemap.tile_size >> 1, tilemap.tile_size >> 1}); Vector2 pos = Vector2Subtract(p_ct->position,(Vector2){tilemap.tile_size >> 1, tilemap.tile_size >> 1});
if ( if (
find_AABB_overlap( find_AABB_overlap(
pos, (Vector2){tilemap.tile_size, tilemap.tile_size}, pos, (Vector2){tilemap.tile_size, tilemap.tile_size},
p_other_ct->position, p_other_bbox->size, &overlap p_other_ct->position, p_other_bbox->size, NULL
) )
) )
{ {