From c8bd190ec145e4640d52927c2bbf73334af4f446 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 23 Dec 2020 13:09:38 +0100 Subject: [PATCH] Nicer acceleration code and gravity --- src/player.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/player.c b/src/player.c index 167cef1..a876f83 100644 --- a/src/player.c +++ b/src/player.c @@ -8,8 +8,10 @@ #include "collide.h" /* TODO: Determine FRICTION and ACCELERATION from UPS. */ +#define MAX_SPD (64 * PXS) #define FRICTION 0.9 -#define ACCELERATION 64 +#define ACCELERATION (MAX_SPD * (1 - FRICTION)) +#define GRAVITY PXS #define SGN(x) ((x > 0) ? (1) : ((x < 0) ? (-1) : (0))) #define PLAYER_COLLIDE(pos) player_collide(player, pos, level, level->solid_layer) @@ -51,9 +53,10 @@ void player_step(Player *player, Input *input, const Level *level) { (input_is_down(input, K_RIGHT) - input_is_down(input, K_LEFT)), (input_is_down(input, K_DOWN) - input_is_down(input, K_UP)) }; - vec_mulf(&player->spd, FRICTION); /* apply friction */ - vec_mul(&move, ACCELERATION); /* apply acceleration */ - vec_add(&player->spd, move); + int xacc = move.x * ACCELERATION; /* calculate horizontal acceleration */ + player->spd.x *= FRICTION; /* apply horizontal friction */ + player->spd.x += xacc; /* apply horizontal acceleration */ + player->spd.y += GRAVITY; /* apply gravity */ player_move(player, level); }