Changing Controller Grip Button to 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.
User input for the controller is defined in the following methods of the VRPlayerVR component (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:
// ...
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.
Switch to SDK Browser and launch our application by clicking the Run button on the project's card.
Now you can grab objects by the trigger, and use them by the Grip side button.