Skip to content

Commit

Permalink
Merge pull request #74 from elvissteinjr/overlay-interaction-toggle
Browse files Browse the repository at this point in the history
Activate laser pointer when controller is pointing the at the overlay using the tip matrix
  • Loading branch information
sd805 authored Feb 11, 2023
2 parents dcf9518 + ef66335 commit 97b8f36
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 6 deletions.
63 changes: 59 additions & 4 deletions L4D2VR/sdk/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,24 @@ class VMatrix
{
public:

VMatrix();
VMatrix()
{
}

VMatrix(
vec_t m00, vec_t m01, vec_t m02, vec_t m03,
vec_t m10, vec_t m11, vec_t m12, vec_t m13,
vec_t m20, vec_t m21, vec_t m22, vec_t m23,
vec_t m30, vec_t m31, vec_t m32, vec_t m33
);
)
{
Init(
m00, m01, m02, m03,
m10, m11, m12, m13,
m20, m21, m22, m23,
m30, m31, m32, m33
);
}

// Creates a matrix where the X axis = forward
// the Y axis = left, and the Z axis = up
Expand All @@ -426,7 +437,28 @@ class VMatrix
vec_t m10, vec_t m11, vec_t m12, vec_t m13,
vec_t m20, vec_t m21, vec_t m22, vec_t m23,
vec_t m30, vec_t m31, vec_t m32, vec_t m33
);
)
{
m[0][0] = m00;
m[0][1] = m01;
m[0][2] = m02;
m[0][3] = m03;

m[1][0] = m10;
m[1][1] = m11;
m[1][2] = m12;
m[1][3] = m13;

m[2][0] = m20;
m[2][1] = m21;
m[2][2] = m22;
m[2][3] = m23;

m[3][0] = m30;
m[3][1] = m31;
m[3][2] = m32;
m[3][3] = m33;
}


// Initialize from a 3x4
Expand Down Expand Up @@ -529,7 +561,30 @@ class VMatrix
VMatrix &operator=(const VMatrix &mOther);

// Multiply two matrices (out = this * vm).
void MatrixMul(const VMatrix &vm, VMatrix &out) const;
void MatrixMul(const VMatrix &vm, VMatrix &out) const
{
out.Init(
m[0][0]*vm.m[0][0] + m[0][1]*vm.m[1][0] + m[0][2]*vm.m[2][0] + m[0][3]*vm.m[3][0],
m[0][0]*vm.m[0][1] + m[0][1]*vm.m[1][1] + m[0][2]*vm.m[2][1] + m[0][3]*vm.m[3][1],
m[0][0]*vm.m[0][2] + m[0][1]*vm.m[1][2] + m[0][2]*vm.m[2][2] + m[0][3]*vm.m[3][2],
m[0][0]*vm.m[0][3] + m[0][1]*vm.m[1][3] + m[0][2]*vm.m[2][3] + m[0][3]*vm.m[3][3],

m[1][0]*vm.m[0][0] + m[1][1]*vm.m[1][0] + m[1][2]*vm.m[2][0] + m[1][3]*vm.m[3][0],
m[1][0]*vm.m[0][1] + m[1][1]*vm.m[1][1] + m[1][2]*vm.m[2][1] + m[1][3]*vm.m[3][1],
m[1][0]*vm.m[0][2] + m[1][1]*vm.m[1][2] + m[1][2]*vm.m[2][2] + m[1][3]*vm.m[3][2],
m[1][0]*vm.m[0][3] + m[1][1]*vm.m[1][3] + m[1][2]*vm.m[2][3] + m[1][3]*vm.m[3][3],

m[2][0]*vm.m[0][0] + m[2][1]*vm.m[1][0] + m[2][2]*vm.m[2][0] + m[2][3]*vm.m[3][0],
m[2][0]*vm.m[0][1] + m[2][1]*vm.m[1][1] + m[2][2]*vm.m[2][1] + m[2][3]*vm.m[3][1],
m[2][0]*vm.m[0][2] + m[2][1]*vm.m[1][2] + m[2][2]*vm.m[2][2] + m[2][3]*vm.m[3][2],
m[2][0]*vm.m[0][3] + m[2][1]*vm.m[1][3] + m[2][2]*vm.m[2][3] + m[2][3]*vm.m[3][3],

m[3][0]*vm.m[0][0] + m[3][1]*vm.m[1][0] + m[3][2]*vm.m[2][0] + m[3][3]*vm.m[3][0],
m[3][0]*vm.m[0][1] + m[3][1]*vm.m[1][1] + m[3][2]*vm.m[2][1] + m[3][3]*vm.m[3][1],
m[3][0]*vm.m[0][2] + m[3][1]*vm.m[1][2] + m[3][2]*vm.m[2][2] + m[3][3]*vm.m[3][2],
m[3][0]*vm.m[0][3] + m[3][1]*vm.m[1][3] + m[3][2]*vm.m[2][3] + m[3][3]*vm.m[3][3]
);
}

// Add two matrices.
const VMatrix &operator+=(const VMatrix &other);
Expand Down
110 changes: 108 additions & 2 deletions L4D2VR/vr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,13 @@ void VR::ProcessMenuInput()
{
vr::VROverlayHandle_t currentOverlay = m_Game->m_EngineClient->IsInGame() ? m_HUDHandle : m_MainMenuHandle;

// Check if left or right hand controller is pointing at the overlay
const bool isHoveringOverlay = CheckOverlayIntersectionForController(currentOverlay, vr::TrackedControllerRole_LeftHand) ||
CheckOverlayIntersectionForController(currentOverlay, vr::TrackedControllerRole_RightHand);

// Overlays can't process action inputs if the laser is active, so
// only activate laser if the controller is aiming forward.
if (abs(m_RightControllerAngAbs.x) < 45 || abs(m_LeftControllerAngAbs.x) < 45)
// only activate laser if a controller is pointing at the overlay
if (isHoveringOverlay)
{
vr::VROverlay()->SetOverlayFlag(currentOverlay, vr::VROverlayFlags_MakeOverlaysInteractiveIfVisible, true);

Expand Down Expand Up @@ -716,6 +720,108 @@ void VR::ProcessInput()
}
}

VMatrix VR::VMatrixFromHmdMatrix(const vr::HmdMatrix34_t &hmdMat)
{
// VMatrix has a different implicit coordinate system than HmdMatrix34_t, but this function does not convert between them
VMatrix vMat(
hmdMat.m[0][0], hmdMat.m[1][0], hmdMat.m[2][0], 0.0f,
hmdMat.m[0][1], hmdMat.m[1][1], hmdMat.m[2][1], 0.0f,
hmdMat.m[0][2], hmdMat.m[1][2], hmdMat.m[2][2], 0.0f,
hmdMat.m[0][3], hmdMat.m[1][3], hmdMat.m[2][3], 1.0f
);

return vMat;
}

vr::HmdMatrix34_t VR::VMatrixToHmdMatrix(const VMatrix &vMat)
{
vr::HmdMatrix34_t hmdMat = {0};

hmdMat.m[0][0] = vMat.m[0][0];
hmdMat.m[1][0] = vMat.m[0][1];
hmdMat.m[2][0] = vMat.m[0][2];

hmdMat.m[0][1] = vMat.m[1][0];
hmdMat.m[1][1] = vMat.m[1][1];
hmdMat.m[2][1] = vMat.m[1][2];

hmdMat.m[0][2] = vMat.m[2][0];
hmdMat.m[1][2] = vMat.m[2][1];
hmdMat.m[2][2] = vMat.m[2][2];

hmdMat.m[0][3] = vMat.m[3][0];
hmdMat.m[1][3] = vMat.m[3][1];
hmdMat.m[2][3] = vMat.m[3][2];

return hmdMat;
}

vr::HmdMatrix34_t VR::GetControllerTipMatrix(vr::ETrackedControllerRole controllerRole)
{
vr::VRInputValueHandle_t inputValue = vr::k_ulInvalidInputValueHandle;

if (controllerRole == vr::TrackedControllerRole_RightHand)
{
m_Input->GetInputSourceHandle("/user/hand/right", &inputValue);
}
else if (controllerRole == vr::TrackedControllerRole_LeftHand)
{
m_Input->GetInputSourceHandle("/user/hand/left", &inputValue);
}

if (inputValue != vr::k_ulInvalidInputValueHandle)
{
char buffer[vr::k_unMaxPropertyStringSize];

m_System->GetStringTrackedDeviceProperty(vr::VRSystem()->GetTrackedDeviceIndexForControllerRole(controllerRole), vr::Prop_RenderModelName_String,
buffer, vr::k_unMaxPropertyStringSize);

vr::RenderModel_ControllerMode_State_t controllerState = {0};
vr::RenderModel_ComponentState_t componentState = {0};

if (vr::VRRenderModels()->GetComponentStateForDevicePath(buffer, vr::k_pch_Controller_Component_Tip, inputValue, &controllerState, &componentState))
{
return componentState.mTrackingToComponentLocal;
}
}

// Not a hand controller role or tip lookup failed, return identity
const vr::HmdMatrix34_t identity =
{
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f
};

return identity;
}

bool VR::CheckOverlayIntersectionForController(vr::VROverlayHandle_t overlayHandle, vr::ETrackedControllerRole controllerRole)
{
vr::TrackedDeviceIndex_t deviceIndex = m_System->GetTrackedDeviceIndexForControllerRole(controllerRole);

if (deviceIndex == vr::k_unTrackedDeviceIndexInvalid)
return false;

vr::TrackedDevicePose_t &controllerPose = m_Poses[deviceIndex];

if (!controllerPose.bPoseIsValid)
return false;

VMatrix controllerVMatrix = VMatrixFromHmdMatrix(controllerPose.mDeviceToAbsoluteTracking);
VMatrix tipVMatrix = VMatrixFromHmdMatrix(GetControllerTipMatrix(controllerRole));
tipVMatrix.MatrixMul(controllerVMatrix, controllerVMatrix);

vr::VROverlayIntersectionParams_t params = {0};
vr::VROverlayIntersectionResults_t results = {0};

params.eOrigin = vr::VRCompositor()->GetTrackingSpace();
params.vSource = { controllerVMatrix.m[3][0], controllerVMatrix.m[3][1], controllerVMatrix.m[3][2]};
params.vDirection = {-controllerVMatrix.m[2][0], -controllerVMatrix.m[2][1], -controllerVMatrix.m[2][2]};

return m_Overlay->ComputeOverlayIntersection(overlayHandle, &params, &results);
}

QAngle VR::GetRightControllerAbsAngle()
{
return m_RightControllerAngAbs;
Expand Down
4 changes: 4 additions & 0 deletions L4D2VR/vr.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ class VR
void GetViewParameters();
void ProcessMenuInput();
void ProcessInput();
VMatrix VMatrixFromHmdMatrix(const vr::HmdMatrix34_t &hmdMat);
vr::HmdMatrix34_t VMatrixToHmdMatrix(const VMatrix &vMat);
vr::HmdMatrix34_t GetControllerTipMatrix(vr::ETrackedControllerRole controllerRole);
bool CheckOverlayIntersectionForController(vr::VROverlayHandle_t overlayHandle, vr::ETrackedControllerRole controllerRole);
QAngle GetRightControllerAbsAngle();
Vector GetRightControllerAbsPos();
Vector GetRecommendedViewmodelAbsPos();
Expand Down

0 comments on commit 97b8f36

Please sign in to comment.