]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Correct acceleration calculation
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 10 Jul 2021 13:11:14 +0000 (15:11 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 10 Jul 2021 13:11:14 +0000 (15:11 +0200)
src/clientplayer.c

index 19e1fdaa7f0859404c5e51f3608de544a9941630..d12d4acb0169b847d7d93684c4e459b8c903b88f 100644 (file)
@@ -44,23 +44,23 @@ static bool collision(ClientPlayer *player)
 void clientplayer_tick(ClientPlayer *player, f64 dtime)
 {
        v3f old_pos = player->pos;
+       v3f old_velocity = player->velocity;
 
-#define CALC_PHYSICS(pos, velocity, old) \
-       pos += velocity * dtime; \
+       player->velocity.y -= 9.81f * dtime;
+
+#define CALC_PHYSICS(pos, velocity, old_pos, old_velocity) \
+       pos += (velocity + old_velocity) / 2.0f * dtime; \
        if (collision(player)) { \
-               pos = old; \
+               pos = old_pos; \
                velocity = 0.0f; \
        }
 
-       CALC_PHYSICS(player->pos.x, player->velocity.x, old_pos.x)
-       CALC_PHYSICS(player->pos.y, player->velocity.y, old_pos.y)
-       CALC_PHYSICS(player->pos.z, player->velocity.z, old_pos.z)
+       CALC_PHYSICS(player->pos.x, player->velocity.x, old_pos.x, old_velocity.x)
+       CALC_PHYSICS(player->pos.y, player->velocity.y, old_pos.y, old_velocity.y)
+       CALC_PHYSICS(player->pos.z, player->velocity.z, old_pos.z, old_velocity.z)
 
 #undef CALC_PHYSICS
 
-       // gravity
-       player->velocity.y -= 9.81f * dtime;
-
        if (old_pos.x != player->pos.x || old_pos.y != player->pos.y || old_pos.z != player->pos.z) {
                clientplayer_send_pos(player);
                set_camera_position((v3f) {player->pos.x, player->pos.y + player->eye_height, player->pos.z});