Rework the offset collision checking

Change the function to check for collision at the specified point.
This is much straightforward to use.
The offset point to check must be computed before passing into the
function.
scene_man
En Yi 2024-04-22 22:42:32 +08:00
parent fec9ac268d
commit 6f3f391d8a
3 changed files with 20 additions and 18 deletions

View File

@ -155,18 +155,17 @@ uint8_t check_collision_line(const CollideEntity_t* ent, TileGrid_t* grid, bool
}
// TODO: This should be a point collision check, not an AABB check
uint8_t check_collision_offset(Entity_t* p_ent, Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, Vector2 offset)
uint8_t check_collision_at(Entity_t* p_ent, Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid)
{
Vector2 new_pos = Vector2Add(pos, offset);
CollideEntity_t ent = {
.p_ent = p_ent,
.bbox = (Rectangle){new_pos.x, new_pos.y, bbox_sz.x, bbox_sz.y},
.bbox = (Rectangle){pos.x, pos.y, bbox_sz.x, bbox_sz.y},
.prev_bbox = (Rectangle){pos.x, pos.y, bbox_sz.x, bbox_sz.y},
.area = (TileArea_t){
.tile_x1 = (new_pos.x) / grid->tile_size,
.tile_y1 = (new_pos.y) / grid->tile_size,
.tile_x2 = (new_pos.x + bbox_sz.x - 1) / grid->tile_size,
.tile_y2 = (new_pos.y + bbox_sz.y - 1) / grid->tile_size
.tile_x1 = (pos.x) / grid->tile_size,
.tile_y1 = (pos.y) / grid->tile_size,
.tile_x2 = (pos.x + bbox_sz.x - 1) / grid->tile_size,
.tile_y2 = (pos.y + bbox_sz.y - 1) / grid->tile_size
}
};

View File

@ -59,7 +59,7 @@ typedef struct CollideEntity {
void remove_entity_from_tilemap(EntityManager_t *p_manager, TileGrid_t* tilemap, Entity_t* p_ent);
uint8_t check_collision(const CollideEntity_t* ent, TileGrid_t* grid, bool check_oneway);
uint8_t check_collision_line(const CollideEntity_t* ent, TileGrid_t* grid, bool check_oneway);
uint8_t check_collision_offset(Entity_t* p_ent, Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, Vector2 offset);
uint8_t check_collision_at(Entity_t* p_ent, Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid);
bool check_on_ground(Entity_t* p_ent, Vector2 prev_pos, Vector2 bbox_sz, TileGrid_t* grid);
uint8_t check_bbox_edges(TileGrid_t* tilemap, Entity_t* p_ent, Vector2 bbox, bool ignore_fragile);
#endif // __COLLISION_FUNCS_H

View File

@ -134,7 +134,7 @@ static bool check_collision_and_move(
Vector2 point_to_test = {0};
point_to_test.x = ent->position.x;
point_to_test.y = other_pos->y - p_bbox->size.y + 1;
if (!check_collision_offset(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap))
{
ent->position = point_to_test;
goto collision_end;
@ -142,7 +142,7 @@ static bool check_collision_and_move(
point_to_test.x = other_pos->x - p_bbox->size.x + 1;
point_to_test.y = ent->position.y;
if (!check_collision_offset(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap))
{
ent->position = point_to_test;
goto collision_end;
@ -150,7 +150,7 @@ static bool check_collision_and_move(
point_to_test.x = other_pos->x + other_bbox.x - 1;
point_to_test.y = ent->position.y;
if (!check_collision_offset(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap))
{
ent->position = point_to_test;
goto collision_end;
@ -158,7 +158,7 @@ static bool check_collision_and_move(
point_to_test.x = ent->position.x;
point_to_test.y = other_pos->y + other_bbox.y - 1;
if (!check_collision_offset(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap))
{
ent->position = point_to_test;
goto collision_end;
@ -465,9 +465,12 @@ void player_movement_input_system(Scene_t* scene)
}
}
uint8_t collide_type = check_collision_offset(
p_player, p_player->position, p_bbox->size,
&tilemap, (Vector2){0, p_bbox->size.y - PLAYER_HEIGHT}
Vector2 point_to_check = Vector2Add(
p_player->position,
(Vector2){0, p_bbox->size.y - PLAYER_HEIGHT}
);
uint8_t collide_type = check_collision_at(
p_player, point_to_check, p_bbox->size, &tilemap
);
if (collide_type == 1)
{
@ -561,10 +564,10 @@ void player_bbox_update_system(Scene_t* scene)
}
}
Vector2 point_to_check = Vector2Add(p_player->position, offset);
if (
check_collision_offset(
p_player, p_player->position, new_bbox,
&tilemap, offset
check_collision_at(
p_player, point_to_check, new_bbox, &tilemap
) != 1
)
{