]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
dtime independent movement
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 10 Jul 2021 11:39:33 +0000 (13:39 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 10 Jul 2021 11:40:43 +0000 (13:40 +0200)
src/client.c
src/clientplayer.c
src/clientplayer.h
src/input.c

index c7aea0b0d6cbcdc96022efb68c57c2b826d8d840..ab6460b912634b6adee4d359561c08d2680ec2c8 100644 (file)
@@ -99,12 +99,20 @@ static void client_loop()
 
        init_input(&client, window);
 
+       struct timespec ts, ts_old;
+       clock_gettime(CLOCK_REALTIME, &ts_old);
+
        while (! glfwWindowShouldClose(window) && client.state != CS_DISCONNECTED && ! interrupted) {
+               clock_gettime(CLOCK_REALTIME, &ts);
+               f64 dtime = (f64) (ts.tv_sec - ts_old.tv_sec) + (f64) (ts.tv_nsec - ts_old.tv_nsec) / 1000000000.0;
+               ts_old = ts;
+
                glEnable(GL_DEPTH_TEST);
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glClearColor(0.52941176470588f, 0.8078431372549f, 0.92156862745098f, 1.0f);
 
                process_input();
+               clientplayer_tick(&client.player, dtime);
 
                scene_render(client.scene, prog);
 
index 7dc3ecaecd3f1ef663254563a18c46fdca05fc9c..8bee6700294f24514de52751b5b9801c74fadeb9 100644 (file)
@@ -1,9 +1,24 @@
+#include "camera.h"
 #include "client.h"
 #include "clientplayer.h"
 
-void clientplayer_send_pos(ClientPlayer *player)
+static void send_pos(ClientPlayer *player)
 {
        pthread_mutex_lock(&player->client->mtx);
        (void) (write_u32(player->client->fd, SC_POS) && write_v3f32(player->client->fd, player->pos));
        pthread_mutex_unlock(&player->client->mtx);
 }
+
+void clientplayer_tick(ClientPlayer *player, f64 dtime)
+{
+       v3f old_pos = player->pos;
+
+       player->pos.x += player->velocity.x * dtime;
+       player->pos.y += player->velocity.y * dtime;
+       player->pos.z += player->velocity.z * dtime;
+
+       if (old_pos.x != player->pos.x || old_pos.y != player->pos.y || old_pos.z != player->pos.z) {
+               send_pos(player);
+               set_camera_position(player->pos);
+       }
+}
index d8cc50ac719d53d834aa24f3242be55d24a60907..357e84f9e3b6fea92d5f8b999476711bed31fa4b 100644 (file)
@@ -8,8 +8,9 @@ typedef struct
        struct Client *client;
        v3f pos;
        f32 yaw, pitch;
+       v3f velocity;
 } ClientPlayer;
 
-void clientplayer_send_pos(ClientPlayer *player);
+void clientplayer_tick(ClientPlayer *player, f64 dtime);
 
 #endif
index 6cd76ff41d920159473462cf17273d534ca7b988..c648a42150acb32ab00b1ad94b028411ebcf82eb 100644 (file)
@@ -26,9 +26,10 @@ static void cursor_pos_callback(__attribute__((unused)) GLFWwindow* window, doub
        set_camera_angle(input.client->player.yaw, input.client->player.pitch);
 }
 
-static bool move(int forward, int backward, vec3 speed)
+static bool move(int forward, int backward, vec3 dir)
 {
        f32 sign;
+       f32 speed = 10.0f;
 
        if (glfwGetKey(input.window, forward) == GLFW_PRESS)
                sign = +1.0f;
@@ -37,23 +38,22 @@ static bool move(int forward, int backward, vec3 speed)
        else
                return false;
 
-       input.client->player.pos.x += speed[0] * sign;
-       input.client->player.pos.y += speed[1] * sign;
-       input.client->player.pos.z += speed[2] * sign;
+       input.client->player.velocity.x += dir[0] * speed * sign;
+       input.client->player.velocity.y += dir[1] * speed * sign;
+       input.client->player.velocity.z += dir[2] * speed * sign;
 
        return true;
 }
 
 void process_input()
 {
-       bool moved_forward = move(GLFW_KEY_W, GLFW_KEY_S, movement_dirs.front);
-       bool moved_up = move(GLFW_KEY_SPACE, GLFW_KEY_LEFT_SHIFT, movement_dirs.up);
-       bool moved_right = move(GLFW_KEY_D, GLFW_KEY_A, movement_dirs.right);
-
-       if (moved_forward || moved_up || moved_right) {
-               set_camera_position(input.client->player.pos);
-               clientplayer_send_pos(&input.client->player);
-       }
+       input.client->player.velocity.x = 0.0f;
+       input.client->player.velocity.y = 0.0f;
+       input.client->player.velocity.z = 0.0f;
+
+       move(GLFW_KEY_W, GLFW_KEY_S, movement_dirs.front);
+       move(GLFW_KEY_SPACE, GLFW_KEY_LEFT_SHIFT, movement_dirs.up);
+       move(GLFW_KEY_D, GLFW_KEY_A, movement_dirs.right);
 }
 
 void init_input(Client *client, GLFWwindow *window)