Изменение кнопки захвата для контроллера Vive на Trigger
You might want to remap actions or swap the controls in the VR application. As an example, let's do this for the controller. We will remap the Use action from the Grip button to Trigger, and the grab action to the Grip button.При разработке приложений часто бывает нужно переназначить действия или поменять местами те или иные элементы управления. Разберем это на примере Vive-контроллера. Переназначим использование (Use) c кнопки Grip на Trigger, а захват объекта (Grab) – наоборот, на Grip.
User input for the controller is defined in the following methods of the VRPlayerVR component (Framework\Components\Players\VRPlayerVR.cpp):Пользовательский ввод для контроллера определяется в следующих методах компонента VRPlayerVR (Framework\Components\Players\VRPlayerVR.cpp):
- getControllerButtonPressed(int controller_num, VRPlayer::BUTTONS button)
- getControllerButtonDown(int controller_num, VRPlayer::BUTTONS button)
- getControllerButtonUp(int controller_num, VRPlayer::BUTTONS button)
To remap actions, you can simply swap VRPlayer::GRAB and VRPlayer::USE in the switch statements inside the methods listed above:Чтобы переназначить действия, просто поменяйте местами VRPlayer::GRAB и VRPlayer::USE в конструкциях switch внутри вышеупомянутых методов:
// ...
int VRPlayerVR::getControllerButtonPressed(int controller_num, VRPlayer::BUTTONS button)
{
if (!controller_valid[controller_num])
return 0;
InputVRControllerPtr controller = (controller_num == 0 ? left_controller_device : right_controller_device);
if (!controller)
return 0;
switch (button)
{
case VRPlayer::TELEPORT:
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRACKPAD_X);
if (axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_JOYSTICK_X);
if (axis_index != -1)
return controller->isButtonPressed(Input::VR_BUTTON(Input::VR_BUTTON_AXIS_0 + axis_index));
}
${#HL}$ case VRPlayer::GRAB: ${HL#}$
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_VALUE);
if (axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_FORCE);
if (axis_index != -1)
return controller->getAxis(axis_index) > 0.5f;
}
${#HL}$ case VRPlayer::USE: ${HL#}$
return controller->isButtonPressed(Input::VR_BUTTON_GRIP);
case VRPlayer::MENU:
return controller->isButtonPressed(Input::VR_BUTTON_APPLICATION) || controller->isButtonPressed(Input::VR_BUTTON_Y);
}
return 0;
}
int VRPlayerVR::getControllerButtonDown(int controller_num, VRPlayer::BUTTONS button)
{
if (!controller_valid[controller_num])
return 0;
InputVRControllerPtr controller = (controller_num == 0 ? left_controller_device : right_controller_device);
if (!controller)
return 0;
switch(button)
{
case VRPlayer::TELEPORT:
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRACKPAD_X);
if(axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_JOYSTICK_X);
if(axis_index != -1)
return controller->isButtonDown(Input::VR_BUTTON(Input::VR_BUTTON_AXIS_0 + axis_index));
}
${#HL}$ case VRPlayer::GRAB: ${HL#}$
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_VALUE);
if(axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_FORCE);
if(axis_index != -1)
return controller->getAxis(axis_index) > 0.5f;
}
${#HL}$ case VRPlayer::USE: ${HL#}$
return controller->isButtonDown(Input::VR_BUTTON_GRIP);
case VRPlayer::MENU:
return controller->isButtonDown(Input::VR_BUTTON_APPLICATION) || controller->isButtonDown(Input::VR_BUTTON_Y);
}
return 0;
}
int VRPlayerVR::getControllerButtonUp(int controller_num, VRPlayer::BUTTONS button)
{
if (!controller_valid[controller_num])
return 0;
InputVRControllerPtr controller = (controller_num == 0 ? left_controller_device : right_controller_device);
if (!controller)
return 0;
switch (button)
{
case VRPlayer::TELEPORT:
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRACKPAD_X);
if (axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_JOYSTICK_X);
if (axis_index != -1)
return controller->isButtonUp(Input::VR_BUTTON(Input::VR_BUTTON_AXIS_0 + axis_index));
}
${#HL}$ case VRPlayer::GRAB: ${HL#}$
{
int axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_VALUE);
if (axis_index == -1)
axis_index = controller->findAxisByType(InputVRController::AXIS_TYPE_TRIGGER_FORCE);
if (axis_index != -1)
return controller->getAxis(axis_index) < 0.5f;
}
${#HL}$ case VRPlayer::USE: ${HL#}$
return controller->isButtonUp(Input::VR_BUTTON_GRIP);
case VRPlayer::MENU:
return controller->isButtonUp(Input::VR_BUTTON_APPLICATION) || controller->isButtonUp(Input::VR_BUTTON_Y);
}
return 0;
}
// ...
Save your changes, then build and run the application by hitting Ctrl + F5 to update component's logic. Close the application after running it and switch to UnigineEditor.Сохраните изменения, а затем соберите и запустите приложение нажав Ctrl + F5 чтобы обновить логику компонента. После запуска приложения закройте его и вернитесь в UnigineEditor.
Switch to SDK Browser and launch our application by clicking the Run button on the project's card.Переключитесь в SDK Browser и запустите приложение, нажав кнопку Run на карточке проекта.
Now you can grab objects by the trigger, and use them by the Grip side button.Теперь захват объекта осуществляется курком, а использование – боковой кнопкой Grip.