OVR: Rework on controller handling, so it works with xbox also
authorAndreas Eversberg <jolly@eversberg.eu>
Sat, 28 Apr 2018 13:19:11 +0000 (15:19 +0200)
committerAndreas Eversberg <jolly@eversberg.eu>
Sun, 29 Apr 2018 06:17:36 +0000 (08:17 +0200)
src/libovr/ovr.c
src/libovr/ovr.h
src/mercenary/main.c

index ff4a25a..bfab6c4 100755 (executable)
@@ -240,7 +240,7 @@ error:
        return -EINVAL;
 }
 
-void get_poses_ovr(int *button_a, int *button_b, int *button_x, int *button_y, int *button_menu, int *button_trigger, int *button_left_thumb, int *button_right_thumb, double *hand_right_x, double *hand_right_y, double *hand_right_z, double *hand_right_yaw, double *hand_right_pitch, double *hand_right_roll, double *stick_left_x, double *stick_left_y, double *stick_right_x, double *stick_right_y, double *head_yaw, double *head_pitch, double *head_roll)
+void get_poses_ovr(int *button_a, int *button_b, int *button_x, int *button_y, int *button_menu, int *button_left_trigger, int *button_right_trigger, int *button_left_thumb, int *button_right_thumb, double *hand_right_x, double *hand_right_y, double *hand_right_z, double *hand_right_yaw, double *hand_right_pitch, double *hand_right_roll, double *stick_left_x, double *stick_left_y, double *stick_right_x, double *stick_right_y, double *head_yaw, double *head_pitch, double *head_roll)
 {      
        ovrResult result;
        float yaw, pitch, roll;
@@ -252,8 +252,14 @@ void get_poses_ovr(int *button_a, int *button_b, int *button_x, int *button_y, i
        ovr_CalcEyePoses(hmdState.HeadPose.ThePose, hmdToEyeViewPose, layer.RenderPose);
        /* Grab hand poses useful for rendering head/hand or controller representation */
        headPose = hmdState.HeadPose.ThePose;
-       handPoses[ovrHand_Left]  = hmdState.HandPoses[ovrHand_Left].ThePose;
-       handPoses[ovrHand_Right] = hmdState.HandPoses[ovrHand_Right].ThePose;
+       if (hmdState.HandStatusFlags[ovrHand_Left] == (ovrStatus_OrientationTracked | ovrStatus_PositionTracked))
+               handPoses[ovrHand_Left]  = hmdState.HandPoses[ovrHand_Left].ThePose;
+       else
+               handPoses[ovrHand_Left] = headPose;
+       if (hmdState.HandStatusFlags[ovrHand_Right] == (ovrStatus_OrientationTracked | ovrStatus_PositionTracked))
+               handPoses[ovrHand_Right] = hmdState.HandPoses[ovrHand_Right].ThePose;
+       else
+               handPoses[ovrHand_Right] = headPose;
        x = handPoses[ovrHand_Right].Position.x;
        y = handPoses[ovrHand_Right].Position.y;
        z = handPoses[ovrHand_Right].Position.z;
@@ -272,7 +278,7 @@ void get_poses_ovr(int *button_a, int *button_b, int *button_x, int *button_y, i
        *hand_right_pitch = pitch;
        *hand_right_roll = roll;
 
-       result = ovr_GetInputState(session, ovrControllerType_Touch, &inputState);
+       result = ovr_GetInputState(session, ovrControllerType_Active, &inputState);
        if (OVR_SUCCESS(result)) {
                if (inputState.Buttons & ovrButton_A)
                        *button_a = 1;
@@ -302,14 +308,18 @@ void get_poses_ovr(int *button_a, int *button_b, int *button_x, int *button_y, i
                        *button_right_thumb = 1;
                else
                        *button_right_thumb = 0;
+               if (inputState.IndexTrigger[ovrHand_Left] >= 0.8)
+                       *button_left_trigger = 1;
+               else
+                       *button_left_trigger = 0;
                if (inputState.IndexTrigger[ovrHand_Right] >= 0.8)
-                       *button_trigger = 1;
+                       *button_right_trigger = 1;
                else
-                       *button_trigger = 0;
-               *stick_left_x = inputState.Thumbstick[ovrHand_Left].x;
-               *stick_left_y = inputState.Thumbstick[ovrHand_Left].y;
-               *stick_right_x = inputState.Thumbstick[ovrHand_Right].x;
-               *stick_right_y = inputState.Thumbstick[ovrHand_Right].y;
+                       *button_right_trigger = 0;
+               *stick_left_x = inputState.ThumbstickNoDeadzone[ovrHand_Left].x;
+               *stick_left_y = inputState.ThumbstickNoDeadzone[ovrHand_Left].y;
+               *stick_right_x = inputState.ThumbstickNoDeadzone[ovrHand_Right].x;
+               *stick_right_y = inputState.ThumbstickNoDeadzone[ovrHand_Right].y;
        }
 }
 
index 18e8c8f..6480216 100755 (executable)
@@ -1,11 +1,11 @@
 
 int init_ovr(int multisampling);
 void exit_ovr(void);
-void get_poses_ovr(int *button_a, int *button_b, int *button_x, int *button_y, int *button_menu, int *button_trigger, int *button_left_thumb, int *button_right_thumb, double *hand_right_x, double *hand_right_y, double *hand_right_z, double *hand_right_yaw, double *hand_right_pitch, double *hand_right_roll, double *stick_left_x, double *stick_left_y, double *stick_right_x, double *stick_right_y, double *head_yaw, double *head_pitch, double *head_roll);
+void get_poses_ovr(int *button_a, int *button_b, int *button_x, int *button_y, int *button_menu, int *button_left_trigger, int *button_right_trigger, int *button_left_thumb, int *button_right_thumb, double *hand_right_x, double *hand_right_y, double *hand_right_z, double *hand_right_yaw, double *hand_right_pitch, double *hand_right_roll, double *stick_left_x, double *stick_left_y, double *stick_right_x, double *stick_right_y, double *head_yaw, double *head_pitch, double *head_roll);
 void begin_render_ovr(void);
 void begin_render_ovr_eye(int eye, double *camera_x, double *camera_y, double *camera_z);
 void end_render_ovr_eye(int eye);
 void end_render_ovr(void);
 void render_mirror_ovr(int view_width, int view_height);
-void normalize_observer_ovr(void);
+void reset_observer_ovr(void);
 
index 0249547..0bd216d 100644 (file)
@@ -51,7 +51,7 @@
 
 #define STICK_WALK_THRESHOLD 0.3 /* move the stick until the player moves */
 #define STICK_ROTATE_THRESHOLD 0.3 /* move the stick until the player rotates */
-#define STICK_THRUST_THRESHOLD 0.3 /* move the stick until the craft moves */
+#define STICK_THRUST_THRESHOLD 0.1 /* move the stick until the craft moves */
 
 static int config_ctrl_c = 0;
 static int config_amiga_speed = 0; /* fast speed */
@@ -74,7 +74,7 @@ static double config_fov = FOV_NOVAGEN;
 static double config_monitor_distance = 31.5; /* inch */
 #ifdef HAVE_OVR
 static double config_keyboard_distance = 28.5; /* inch */
-static double config_keyboard_height = 10.0; /* inch */
+static double config_keyboard_height = 4.0; /* inch */
 #endif
 static int config_debug_transparent = 0;
 static int config_debug_opengl = 0;
@@ -354,24 +354,27 @@ static void resize_window(int width, int height)
 }
 
 #ifdef HAVE_OVR
-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 int __attribute__((unused)) button_a_last = 0, button_b_last = 0, button_x_last = 0, button_y_last = 0, button_menu_last = 0, button_left_trigger_last = 0, button_right_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 thrust_last = 0;
 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;
 
 static void handle_vr_poses(void)
 {
-       int button_a = 0, button_b = 0, button_x = 0, button_y = 0, button_menu = 0, button_trigger = 0, button_left_thumb = 0, button_right_thumb = 0;
+       int button_a = 0, button_b = 0, button_x = 0, button_y = 0, button_menu = 0, button_left_trigger = 0, button_right_trigger = 0, button_left_thumb = 0, button_right_thumb = 0;
        double hand_right_x = 0.0, hand_right_y = 0.0, hand_right_z = 0.0;
        double hand_right_yaw = 0.0, hand_right_pitch = 0.0, hand_right_roll = 0.0;
        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 thrust = 0;
        int joystick_set_x = 0, joystick_set_y = 0;
+       static uint32_t current_time, last_time = 0, diff;
+       static double increment, inc_count = 0.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);
+       /* handle input */
+       get_poses_ovr(&button_a, &button_b, &button_x, &button_y, &button_menu, &button_left_trigger, &button_right_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) {
                /* menu toggle */
                if (help_view == help_views)
@@ -409,23 +412,13 @@ static void handle_vr_poses(void)
                        set_amiga_key(KEYCODE_l, 1);
                        set_amiga_key(KEYCODE_l, 0);
                }
-               if (button_left_thumb && !button_left_thumb_last) {
-                       /* 'escape' pressed */
-                       set_amiga_key(KEYCODE_ESCAPE, 1);
-                       set_amiga_key(KEYCODE_ESCAPE, 0);
-               }
-               if (!button_left_thumb && button_left_thumb_last) {
-                       /* 'escape' released */
-                       set_amiga_key(KEYCODE_SPACE, 1);
-                       set_amiga_key(KEYCODE_SPACE, 0);
-               }
                if (button_right_thumb && !button_right_thumb_last) {
                        /* 'change orientation' pressed */
                        double roll, pitch, yaw;
                        mercenary_get_orientation(&roll, &pitch, &yaw);
                        mercenary_set_orientation(yaw + hand_right_yaw);
                }
-               if (button_trigger && !button_trigger_last) {
+               if (button_right_trigger && !button_right_trigger_last) {
                        /* trigger pressed */
                        if (!keyboard_on) {
                                set_joystick(-1, -1, -1, -1, 1);
@@ -434,7 +427,7 @@ static void handle_vr_poses(void)
                                set_amiga_key(vr_key_pressed, 1);
                        }
                }
-               if (!button_trigger && button_trigger_last) {
+               if (!button_right_trigger && button_right_trigger_last) {
                        /* trigger released */
                        set_joystick(-1, -1, -1, -1, 0);
                        if (vr_key_pressed) {
@@ -466,9 +459,11 @@ static void handle_vr_poses(void)
                                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));
+                       if (joystick_set_x || joystick_set_y) {
+                               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;
@@ -476,7 +471,6 @@ static void handle_vr_poses(void)
 
                        /* 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)
@@ -493,12 +487,13 @@ static void handle_vr_poses(void)
                                if (dir > M_PI / 2.0 || dir < -M_PI / 2.0) {
                                        /* go backwards */
                                        set_joystick(-1, -1, 0, 1, -1);
+                                       joystick_set_y = 1;
                                } else {
                                        /* go forward */
                                        set_joystick(-1, -1, 1, 0, -1);
+                                       joystick_set_y = 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;
@@ -527,29 +522,64 @@ printf("dir=%.4f\n", dir);
                if (joystick_set_y_last && !joystick_set_y)
                        set_joystick(-1, -1, 0, 0, -1);
                /* thrust */
+               thrust = thrust_last;
+               /* button to toggle between stop and escape */
+               if (button_left_thumb && !button_left_thumb_last) {
+                       /* 'escape' pressed */
+                       if (thrust_last == 0)
+                               thrust = -99;
+                       else
+                               thrust = 0;
+               }
+               /* get stick to increment or decrement thrust */
                if (stick_left_y > STICK_THRUST_THRESHOLD)
-                       thrust = (stick_left_y - STICK_THRUST_THRESHOLD) / (1.0 - STICK_THRUST_THRESHOLD) * 10.5;
+                       increment = (stick_left_y - STICK_THRUST_THRESHOLD) / (1.0 - STICK_THRUST_THRESHOLD);
                else
                if (stick_left_y < -STICK_THRUST_THRESHOLD)
-                       thrust = (stick_left_y - -STICK_THRUST_THRESHOLD) / (1.0 - -STICK_THRUST_THRESHOLD) * 10.5;
-               else
-                       thrust = 0;
-               if (thrust >= 10)
-                       thrust = KEYCODE_0;
-               else
-               if (thrust > 0)
-                       thrust = KEYCODE_1 + thrust - 1;
-               else
-               if (thrust < 0)
-                       thrust = KEYCODE_F1 - thrust - 1;
+                       increment = (stick_left_y + STICK_THRUST_THRESHOLD) / (1.0 - STICK_THRUST_THRESHOLD);
                else
-               if (thrust <= -10)
-                       thrust = KEYCODE_F10;
-               else
-                       thrust = KEYCODE_SPACE;
+                       increment = 0;
+               current_time = ticks_sdl();
+               if (increment) {
+                       diff = current_time - last_time;
+                       inc_count += increment * diff;
+                       if (inc_count > 150.0 && thrust >= -10 && thrust < 10) {
+                               thrust++;
+                               inc_count = 0.0;
+                       }
+                       if (inc_count < -150.0 && thrust <= 10 && thrust > -10) {
+                               thrust--;
+                               inc_count = 0.0;
+                       }
+               } else {
+                       inc_count = 0;
+               }
+               last_time = current_time;
+               /* send thrust change as keycodes */
                if (thrust_last != thrust) {
-                       set_amiga_key(thrust, 1);
-                       set_amiga_key(thrust, 0);
+                       if (thrust >= 10) {
+                               set_amiga_key(KEYCODE_0, 1);
+                               set_amiga_key(KEYCODE_0, 0);
+                       } else
+                       if (thrust <= -10 && thrust > -99) {
+                               set_amiga_key(KEYCODE_F10, 1);
+                               set_amiga_key(KEYCODE_F10, 0);
+                       } else
+                       if (thrust <= -99) {
+                               set_amiga_key(KEYCODE_ESCAPE, 1);
+                               set_amiga_key(KEYCODE_ESCAPE, 0);
+                       } else
+                       if (thrust > 0) {
+                               set_amiga_key(KEYCODE_1 + thrust - 1, 1);
+                               set_amiga_key(KEYCODE_1 + thrust - 1, 0);
+                       } else
+                       if (thrust < 0) {
+                               set_amiga_key(KEYCODE_F1 - thrust - 1, 1);
+                               set_amiga_key(KEYCODE_F1 - thrust - 1, 0);
+                       } else {
+                               set_amiga_key(KEYCODE_SPACE, 1);
+                               set_amiga_key(KEYCODE_SPACE, 0);
+                       }
                }
        }
        button_a_last = button_a;
@@ -557,7 +587,8 @@ printf("dir=%.4f\n", dir);
        button_x_last = button_x;
        button_y_last = button_y;
        button_menu_last = button_menu;
-       button_trigger_last = button_trigger;
+       button_left_trigger_last = button_left_trigger;
+       button_right_trigger_last = button_right_trigger;
        button_left_thumb_last = button_left_thumb;
        button_right_thumb_last = button_right_thumb;
        stick_left_x_last = stick_left_x;
@@ -1313,7 +1344,7 @@ int main(int argc, char *argv[])
                "        Press `CTRL' + `+' or `-' to change field-of-view (OpenGL).\n"
                "        Press `CTRL' + `I' to skip intro (approaching to Eris).\n"
 #ifdef HAVE_OVR
-               "        Press `CTRL' + `O' (or right trigger) to reset observer position.\n"
+               "        Press `CTRL' + `O' (or both triggers) to reset observer position.\n"
 #endif
                "\n"
                "Answer to a Question:\n"
@@ -1358,7 +1389,8 @@ int main(int argc, char *argv[])
                "        Press `B' button to emulate keyboard to enter alphanumeric keys.\n"
                "        Press `A' or `B' button again to dismiss keyboard.\n"
                "        Point right controller to a key on keyboard. The key will highlight.\n"
-               "        Press `Trigger' on right controller enter the highlighted key.\n"
+               "        Pull `Trigger' on right controller enter the highlighted key.\n"
+               "        Pull `Trigger' on both controllers to reset observer position.\n"
                "\n"
                "Walking / Driving / Flying using Controller:\n"
                "        Use thumb stick on right controller, to move player / craft.\n"
@@ -1366,9 +1398,9 @@ int main(int argc, char *argv[])
                "        Press thumb stick to change orientation to that direction.\n"
                "        Press `X' button to board, `Y' button to leave.\n"
                "        Move thumb stick on left controller to drive/fly forward or backward.\n"
-               "        Press and hold thumb stick on left controller for escape sequence.\n"
-               "        Release thumb stick on left controller stop craft.\n"
-               "        Press `Trigger' on right controller to fire.\n"
+               "        Press thumb stick on left controller stop craft.\n"
+               "        Press thumb stick on left controller for escape sequence, after stop.\n"
+               "        Pull `Trigger' on right controller to fire.\n"
                "\n"
                "For all other game function, use the emulated keyboards!\n"
                ,HELP_ALPHA, 1, 2, 5, 0);