a-game

2D platformer written from scratch.
Log | Files | Refs | README | LICENSE

commit f6ce1e41f8261cfa698cc20a151181b6a9018ebc
parent 38486d141302b812c8e1ef5f1392634434ee56e1
Author: Amin Mesbah <dev@aminmesbah.com>
Date:   Wed, 24 Apr 2019 19:36:36 -0700

Add epsilon to player dist from collision boundary

Before it was being added to the distance of the collision boundary from
the associated tile it represented. That meant that if the player ended
up exactly on that boundary, they wouldn't be colliding with the _tile_,
but they _would_ be colliding with the boundary itself, causing them to
get stuck on it.

Diffstat:
Msrc/game.c | 19+++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -270,6 +270,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga if (player_is_in_tile && tile_id > 0) { color = (v3) {0.8f, 0.4f, 0.4f}; + assert(false); } } @@ -430,10 +431,6 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga }; rect tile_player_sum = glmth_minkowski_sum_rect_rect(tile_aabb, player->dimensions); - f32 collision_border_epsilon = 0.0001f; - tile_player_sum = glmth_minkowski_sum_rect_rect( - tile_player_sum, - (v2) {collision_border_epsilon, collision_border_epsilon}); RENDER_COLLISION_DEBUG_QUAD(tile_player_sum, ((v3) {0.8f, 0.4f, 0.4f})); v2 tile_nw = {tile_player_sum.min.x, tile_player_sum.max.y}; @@ -512,7 +509,21 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga v2 delta_to_nearest_collision = glmth_v2f_m(player_delta, smallest_distance_scale_factor); v2 nearest_collision_p = glmth_v2_a(player->pos, delta_to_nearest_collision); + f32 epsilon = 0.0001f; + rect local_inhabitable_region = { + .min = { + glmth_min(player->pos.x, nearest_collision_p.x + epsilon), + glmth_min(player->pos.y, nearest_collision_p.y + epsilon), + }, + .max = { + glmth_max(player->pos.x, nearest_collision_p.x - epsilon), + glmth_max(player->pos.y, nearest_collision_p.y - epsilon), + }, + }; + player->pos = nearest_collision_p; + glmth_clamp(&player->pos.x, local_inhabitable_region.min.x, local_inhabitable_region.max.x); + glmth_clamp(&player->pos.y, local_inhabitable_region.min.y, local_inhabitable_region.max.y); // NOTE(amin): // n * dot(v, n)