a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit 576ead88e8561f8a1a5ec53a30346f92f1d50d39
parent 6d5d3a11488221a52886c567f4e1ce9137966ef9
Author: amin <dev@aminmesbah.com>
Date:   Fri, 19 Apr 2019 23:17:57 +0000

Do hilariously untuned semi-implicit Euler

Also highlight colliding tiles in a different color.

FossilOrigin-Name: f74669ace8ecb04812bc93e557869f0823e7cdd81336970ad865818e54568b54
Diffstat:
Msrc/game.c | 41++++++++++++++++++++++++++++++++++-------
Msrc/game.h | 2++
Msrc/glmth.h | 6++++++
3 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -123,28 +123,51 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga // update player { + struct Entity *player = &game_state->player; + f32 dt = game_input->dt; f32 max_meters_per_second = 5.0f; - f32 movement_speed = max_meters_per_second * game_input->dt; + f32 acceleration_rate = 10.0f; if (game_input->key_up) { - game_state->player.pos.y += movement_speed; + player->acceleration.y = +acceleration_rate; } else if (game_input->key_down) { - game_state->player.pos.y -= movement_speed; + player->acceleration.y = -acceleration_rate; + } + else + { + player->acceleration.y = 0.0f; } if (game_input->key_left) { - game_state->player.pos.x -= movement_speed; + player->acceleration.x = -acceleration_rate; } else if (game_input->key_right) { - game_state->player.pos.x += movement_speed; + player->acceleration.x = +acceleration_rate; + } + else + { + player->acceleration.x = 0.0f; } - glmth_clamp(&(game_state->player.pos.x), 0, ROOM_TILE_DIM_X); - glmth_clamp(&(game_state->player.pos.y), 0, ROOM_TILE_DIM_Y); + + glmth_clamp(&player->velocity.x, -max_meters_per_second, max_meters_per_second); + + // Semi implicit Euler integration: https://gafferongames.com/post/integration_basics/ + player->velocity = glmth_v2_a(player->velocity, glmth_v2f_m(player->acceleration, dt)); + player->pos = glmth_v2_a(player->pos, glmth_v2f_m(player->velocity, dt)); + + glmth_clamp(&(player->pos.x), 0, ROOM_TILE_DIM_X); + glmth_clamp(&(player->pos.y), 0, ROOM_TILE_DIM_Y); + printf("A: "); + glmth_v2_print(player->acceleration); + printf("V: "); + glmth_v2_print(player->velocity); + printf("P: "); + glmth_v2_print(player->pos); } // render tiles @@ -208,6 +231,10 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga { color = (v3) {0.4f, 0.8f, 0.4f}; } + if (player_is_in_tile && tile_id > 0) + { + color = (v3) {0.8f, 0.4f, 0.4f}; + } shader_setv3(&game_state->tiles.shader, "color", &color); shader_setm4(&game_state->tiles.shader, "model", &model); diff --git a/src/game.h b/src/game.h @@ -27,6 +27,8 @@ struct Tiles struct Entity { + v2 acceleration; + v2 velocity; v2 pos; struct Shader shader; }; diff --git a/src/glmth.h b/src/glmth.h @@ -121,6 +121,12 @@ internal inline v2 glmth_v2_a(v2 vec1, v2 vec2) return r; } +internal inline v2 glmth_v2f_m(v2 v, f32 s) +{ + v2 r = {v.x * s, v.y * s}; + return r; +} + internal inline void glmth_v2_print(v2 v) { printf("( %f, %f )\n", v.x, v.y);