foo
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 28 Sep 2021 02:31:46 +0000 (05:31 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 28 Sep 2021 02:31:46 +0000 (05:31 +0300)
src/game.c
src/player.c

index 8dc8d70..64c00df 100644 (file)
@@ -68,14 +68,6 @@ void update(float dt)
 {
        int fwd = 0, right = 0, turn = 0;
 
-       /*
-       cgm_vec3 vdir = {0, 0, -1};
-
-       cgm_vmul_m3v3(&vdir, player.view_xform);
-
-       player.dir = (int)(2.0f * (-atan2(vdir.z, vdir.x) + M_PI) / M_PI + 0.5f) & 3;
-       */
-
        if(time_msec - prev_turn >= TURN_INTERVAL) {
                if(input_state[INP_LTURN]) turn--;
                if(input_state[INP_RTURN]) turn++;
@@ -221,7 +213,6 @@ void game_keyboard(int key, int press)
                if(press) {
                        turn_player(&player, -1);
                        prev_turn = time_msec;
-                       player.dir = (player.dir + 3) & 3;
                }
                break;
 
@@ -230,7 +221,6 @@ void game_keyboard(int key, int press)
                if(press) {
                        turn_player(&player, 1);
                        prev_turn = time_msec;
-                       player.dir = (player.dir + 1) & 3;
                }
                break;
        }
@@ -253,7 +243,7 @@ void game_mmotion(int x, int y)
        if(!(dx | dy)) return;
 
        if(bnstate[0]) {
-               player.theta -= cgm_deg_to_rad(dx * 0.5f);
+               player.theta += cgm_deg_to_rad(dx * 0.5f);
                player.phi -= cgm_deg_to_rad(dy * 0.5f);
                if(player.phi < -M_PI/2) player.phi = -M_PI/2;
                if(player.phi > M_PI/2) player.phi = M_PI/2;
index 24c5851..81ce5b8 100644 (file)
@@ -13,9 +13,18 @@ void init_player(struct player *p)
 void move_player(struct player *p, int right, int fwd)
 {
        static const int step[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
-       int rdir = (p->dir + 1) & 3;
-       p->cx += step[p->dir][0] * fwd + step[rdir][0] * right;
-       p->cy += step[p->dir][1] * fwd + step[rdir][1] * right;
+       int fdir, rdir;
+       float angle;
+       cgm_vec3 vdir = {0, 0, -1};
+
+       cgm_vmul_m3v3(&vdir, p->view_xform);
+
+       angle = atan2(vdir.z, vdir.x) + 3.0 * M_PI;
+       fdir = (p->dir + (int)(2.0 * angle / M_PI)) & 3;
+
+       rdir = (fdir + 1) & 3;
+       p->cx += step[fdir][0] * fwd + step[rdir][0] * right;
+       p->cy += step[fdir][1] * fwd + step[rdir][1] * right;
 }
 
 void turn_player(struct player *p, int turn)
@@ -23,6 +32,7 @@ void turn_player(struct player *p, int turn)
        if(!turn) return;
        turn = turn > 0 ? 1 : 3;
        p->dir = (p->dir + turn) & 3;
+       p->theta = 0;
 }
 
 void upd_player_xform(struct player *p)
@@ -35,7 +45,7 @@ void upd_player_xform(struct player *p)
 
        cgm_midentity(p->view_xform);
        cgm_mprerotate_x(p->view_xform, -p->phi);
-       cgm_mprerotate_y(p->view_xform, p->dir * M_PI / 2.0f);
+       cgm_mprerotate_y(p->view_xform, p->theta + p->dir * M_PI / 2.0f);
        cgm_mrotate_quat(p->view_xform, &p->vrot);
        cgm_mpretranslate(p->view_xform, -pos.x, -pos.y, -pos.z);
 }