OVR: Use hand directon and stick direction to walk where actually the stick is pushed to
authorAndreas Eversberg <jolly@eversberg.eu>
Sat, 28 Apr 2018 09:44:41 +0000 (11:44 +0200)
committerAndreas Eversberg <jolly@eversberg.eu>
Sat, 28 Apr 2018 09:44:41 +0000 (11:44 +0200)
src/mercenary/main.c
src/mercenary/mercenary2.c
src/mercenary/mercenary3.c

index 8301adb..6184fa4 100644 (file)
@@ -357,6 +357,7 @@ static void resize_window(int width, int height)
 static int __attribute__((unused)) button_a_last = 0, button_b_last = 0, button_x_last = 0, button_y_last = 0, button_menu_last = 0, button_trigger_last = 0, button_left_thumb_last = 0, button_right_thumb_last = 0;
 static double __attribute__((unused)) stick_left_x_last = 0.0, stick_left_y_last = 0.0, stick_right_x_last = 0.0, stick_right_y_last = 0.0;
 static int thrust_last = KEYCODE_SPACE;
+static int joystick_set_x_last = 0, joystick_set_y_last = 0;
 static int keyboard_on = 0;
 static enum keycode vr_key_pressed = 0, vr_key = 0;
 
@@ -368,6 +369,7 @@ static void handle_vr_poses(void)
        double head_yaw = 0.0, head_pitch = 0.0, head_roll = 0.0;
        double stick_left_x = 0.0, stick_left_y = 0.0, stick_right_x = 0.0, stick_right_y = 0.0;
        int thrust = KEYCODE_SPACE;
+       int joystick_set_x = 0, joystick_set_y = 0;
 
        get_poses_ovr(&button_a, &button_b, &button_x, &button_y, &button_menu, &button_trigger, &button_left_thumb, &button_right_thumb, &hand_right_x, &hand_right_y, &hand_right_z, &hand_right_yaw, &hand_right_pitch, &hand_right_roll, &stick_left_x, &stick_left_y, &stick_right_x, &stick_right_y, &head_yaw, &head_pitch, &head_roll);
        if (button_menu && !button_menu_last) {
@@ -443,21 +445,88 @@ static void handle_vr_poses(void)
                        }
                }
                /* joystick */
-               if (stick_right_x > STICK_ROTATE_THRESHOLD && !mercenary_get_info_walking())
-                       set_joystick(0, 1, -1, -1, -1);
-               else
-               if (stick_right_x < -STICK_ROTATE_THRESHOLD && !mercenary_get_info_walking())
-                       set_joystick(1, 0, -1, -1, -1);
-               else
-               if (stick_right_x_last > STICK_ROTATE_THRESHOLD || stick_right_x_last < -STICK_ROTATE_THRESHOLD)
+               /* reset moving state, so the game is not influenced before we want to */
+               mercenary_vr_move(0, NULL, NULL, 256, 256);
+               joystick_set_x = 0;
+               joystick_set_y = 0;
+               if (!mercenary_get_info_walking()) {
+                       /* turn right and left when not walking */
+                       if (stick_right_x > STICK_ROTATE_THRESHOLD) {
+                               set_joystick(0, 1, -1, -1, -1);
+                               joystick_set_x = 1;
+                       } else
+                       if (stick_right_x < -STICK_ROTATE_THRESHOLD) {
+                               set_joystick(1, 0, -1, -1, -1);
+                               joystick_set_x = 1;
+                       }
+                       /* pitch craft */
+                       if (stick_right_y > STICK_WALK_THRESHOLD) {
+                               set_joystick(-1, -1, 1, 0, -1);
+                               joystick_set_y = 1;
+                       } else
+                       if (stick_right_y < -STICK_WALK_THRESHOLD) {
+                               set_joystick(-1, -1, 0, 1, -1);
+                               joystick_set_y = 1;
+                       }
+                       mercenary_vr_move(0, NULL, NULL,
+                               256,
+                               (int)(256.0 * fabs(stick_right_y) - STICK_WALK_THRESHOLD / (1.0 - STICK_WALK_THRESHOLD) + 0.5));
+               } else {
+                       double roll, pitch, yaw;
+                       double dist, dir, east, north;
+                       int32_t move_east[4], move_north[4];
+
+                       /* check if we push the stick */
+                       dist = sqrt(stick_right_x * stick_right_x + stick_right_y * stick_right_y);
+//printf("dist=%.4f\n", dist);
+                       if (dist > STICK_WALK_THRESHOLD) {
+                               /* get stick amplitude (dist) and direction (dir) */
+                               if (dist > 1.0)
+                                       dist = 1.0;
+                               dist = (dist - STICK_WALK_THRESHOLD) / (1.0 - STICK_WALK_THRESHOLD) * 40.0;
+                               dir = atan2(stick_right_x, stick_right_y);
+                               /* use hand direction to get actual stick direction */
+                               dir = fmod(dir - hand_right_yaw, M_PI * 2.0);
+                               if (dir < -M_PI)
+                                       dir += M_PI * 2.0;
+                               if (dir > M_PI)
+                                       dir -= M_PI * 2.0;
+                               /* flip directin, if we walk backwards */
+                               if (dir > M_PI / 2.0 || dir < -M_PI / 2.0) {
+                                       /* go backwards */
+                                       set_joystick(-1, -1, 0, 1, -1);
+                               } else {
+                                       /* go forward */
+                                       set_joystick(-1, -1, 1, 0, -1);
+                               }
+                               joystick_set_y = 1;
+printf("dir=%.4f\n", dir);
+                               mercenary_get_orientation(&roll, &pitch, &yaw);
+                               east = sin(yaw - dir + M_PI) * dist;
+                               north = -cos(yaw - dir + M_PI) * dist;
+                               /* calculatate the integer positions of 4 steps */
+                               move_east[0] = (int32_t)((east * 0.25) + 0.5);
+                               move_north[0] = (int32_t)((north * 0.25) + 0.5);
+                               move_east[1] = (int32_t)((east * 0.5) + 0.5);
+                               move_north[1] = (int32_t)((north * 0.5) + 0.5);
+                               move_east[2] = (int32_t)((east * 0.75) + 0.5);
+                               move_north[2] = (int32_t)((north * 0.75) + 0.5);
+                               move_east[3] = (int32_t)(east + 0.5);
+                               move_north[3] = (int32_t)(north + 0.5);
+                               /* calculate the delta between each of the 4 step */
+                               move_east[3] -= move_east[2];
+                               move_north[3] -= move_north[2];
+                               move_east[2] -= move_east[1];
+                               move_north[2] -= move_north[1];
+                               move_east[1] -= move_east[0];
+                               move_north[1] -= move_north[0];
+                               /* the game takes 4 steps to move the player */
+                               mercenary_vr_move(1, move_east, move_north, 256, 256);
+                       }
+               }
+               if (joystick_set_x_last && !joystick_set_x)
                        set_joystick(0, 0, -1, -1, -1);
-               if (stick_right_y > STICK_WALK_THRESHOLD)
-                       set_joystick(-1, -1, 1, 0, -1);
-               else
-               if (stick_right_y < -STICK_WALK_THRESHOLD)
-                       set_joystick(-1, -1, 0, 1, -1);
-               else
-               if (stick_right_y_last > STICK_WALK_THRESHOLD || stick_right_y_last < -STICK_WALK_THRESHOLD)
+               if (joystick_set_y_last && !joystick_set_y)
                        set_joystick(-1, -1, 0, 0, -1);
                /* thrust */
                if (stick_left_y > STICK_THRUST_THRESHOLD)
@@ -498,50 +567,14 @@ static void handle_vr_poses(void)
        stick_right_x_last = stick_right_x;
        stick_right_y_last = stick_right_y;
        thrust_last = thrust;
+       joystick_set_x_last = joystick_set_x;
+       joystick_set_y_last = joystick_set_y;
 
        /* we must handle keyboard after toggeling keyboard_on,
         * so that keyboard_on will not change until keyboard is rendered */
        vr_key = 0;
        if (keyboard_on)
                handle_vr_keyboard(keyboard_on - 1, hand_right_x, hand_right_y, hand_right_z, hand_right_yaw, hand_right_pitch, &vr_key);
-
-       if (stick_right_y > STICK_WALK_THRESHOLD || stick_right_y < -STICK_WALK_THRESHOLD) {
-               double roll, pitch, yaw;
-               double dist, east, north;
-               int32_t move_east[4], move_north[4];
-
-               mercenary_get_orientation(&roll, &pitch, &yaw);
-               /* we use a maximum move of 40 for all 4 steps (this is 10 for each step - equal to 'running') */
-               if (stick_right_y > 0.0)
-                       dist = (stick_right_y - STICK_WALK_THRESHOLD) / (1.0 - STICK_WALK_THRESHOLD) * 40.0;
-               else
-                       dist = (stick_right_y + STICK_WALK_THRESHOLD) / (1.0 - STICK_WALK_THRESHOLD) * 40.0;
-               east = sin(hand_right_yaw + yaw + M_PI) * dist;
-               north = -cos(hand_right_yaw + yaw + M_PI) * dist;
-               /* calculatate the integer positions of 4 steps */
-               move_east[0] = (int32_t)((east * 0.25) + 0.5);
-               move_north[0] = (int32_t)((north * 0.25) + 0.5);
-               move_east[1] = (int32_t)((east * 0.5) + 0.5);
-               move_north[1] = (int32_t)((north * 0.5) + 0.5);
-               move_east[2] = (int32_t)((east * 0.75) + 0.5);
-               move_north[2] = (int32_t)((north * 0.75) + 0.5);
-               move_east[3] = (int32_t)(east + 0.5);
-               move_north[3] = (int32_t)(north + 0.5);
-               /* calculate the delta between each of the 4 step */
-               move_east[3] -= move_east[2];
-               move_north[3] -= move_north[2];
-               move_east[2] -= move_east[1];
-               move_north[2] -= move_north[1];
-               move_east[1] -= move_east[0];
-               move_north[1] -= move_north[0];
-               /* the game takes 4 steps to move the player */
-               mercenary_vr_move(1, move_east, move_north,
-                       256,
-                       (int)(256.0 * fabs(stick_right_y) - STICK_WALK_THRESHOLD / (1.0 - STICK_WALK_THRESHOLD) + 0.5));
-       } else {
-               /* don't change what the game actually does when moving joystick in y-direction */
-               mercenary_vr_move(0, NULL, NULL, 256, 256);
-       }
 }
 #endif
 
index 4dc9e2b..13b1487 100644 (file)
@@ -132,6 +132,8 @@ void mercenary_load(void)
        int i;
 
        memset(&vr_move, 0, sizeof(vr_move));
+       vr_move.yaw = 256;
+       vr_move.pitch = 256;
 
        /* load game binary from constant to volatile memory */
        for (i = 0; i < mercenary2_hex_size; i += 4) {
@@ -222,9 +224,7 @@ void mercenary_patch_vr(void)
                }
                break;
        case 0x58060: /* soft pitch over surface */
-               if (vr_move.override) {
-                       REG_D[1] = ((int16_t)REG_D[1] * vr_move.pitch / 256) & 0xfff;
-               }
+               REG_D[1] = ((int16_t)REG_D[1] * vr_move.pitch / 256) & 0xfff;
                break;
        }
 }
index 341036a..f59271b 100644 (file)
@@ -138,6 +138,8 @@ void mercenary_load(void)
        int i;
 
        memset(&vr_move, 0, sizeof(vr_move));
+       vr_move.yaw = 256;
+       vr_move.pitch = 256;
 
        /* load game binary from constant to volatile memory */
        for (i = 0; i < mercenary3_hex_size; i += 4) {
@@ -258,9 +260,7 @@ void mercenary_patch_vr(void)
                }
                break;
        case 0x58F44: /* soft pitch over surface */
-               if (vr_move.override) {
-                       REG_D[1] = ((int16_t)REG_D[1] * vr_move.pitch / 256) & 0xfff;
-               }
+               REG_D[1] = ((int16_t)REG_D[1] * vr_move.pitch / 256) & 0xfff;
                break;
 #if 0
        case 0x5946E: /* pitch in space doesn't work! */