commit 5ae8724e047c415f75d1c615b3bbb3f27b121b47
parent 576ead88e8561f8a1a5ec53a30346f92f1d50d39
Author: amin <dev@aminmesbah.com>
Date: Fri, 19 Apr 2019 23:17:57 +0000
Tune new movement code
It's surprising how much nicer this feels compared to simply adding a
velocity constant to position.
FossilOrigin-Name: 647c24d1494143cd01947b5a47f9aa32616f1c6c3fa3ccb554fe40b2bd80b533
Diffstat:
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/src/game.c b/src/game.c
@@ -126,11 +126,12 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
struct Entity *player = &game_state->player;
f32 dt = game_input->dt;
f32 max_meters_per_second = 5.0f;
- f32 acceleration_rate = 10.0f;
+ f32 acceleration_rate = 50.0f;
+ f32 friction = 0.7f;
if (game_input->key_up)
{
- player->acceleration.y = +acceleration_rate;
+ player->acceleration.y = acceleration_rate;
}
else if (game_input->key_down)
{
@@ -139,6 +140,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
else
{
player->acceleration.y = 0.0f;
+ player->velocity.y = player->velocity.y * friction;
}
if (game_input->key_left)
@@ -147,21 +149,23 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
}
else if (game_input->key_right)
{
- player->acceleration.x = +acceleration_rate;
+ player->acceleration.x = acceleration_rate;
}
else
{
player->acceleration.x = 0.0f;
+ player->velocity.x = player->velocity.x * friction;
}
- 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->velocity.x, -max_meters_per_second, max_meters_per_second);
+ glmth_clamp(&player->velocity.y, -max_meters_per_second, max_meters_per_second);
+ 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: ");