From 14b5c8f74572fc6915567a53ca72dd29b3c0351b Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 15 Apr 2023 21:36:13 +0200 Subject: [PATCH] rotate lasers to face HMD so they don't look flat --- src/app.cpp | 1 - src/controller.cpp | 11 ++++++++--- src/util.h | 5 +++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index 6cb55da..cdf7552 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -137,7 +137,6 @@ void App::InitRootOverlay() panel.GetOverlay()->ControllerRelease(); } }; - printf("Created root overlay instance\n"); } void App::Update() diff --git a/src/controller.cpp b/src/controller.cpp index 8928517..3a6d14c 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -52,12 +52,17 @@ void Controller::Update() return; auto controller_pose = _app->GetTrackerPose(_device_index); - auto origin = GetPos(controller_pose); + auto controller_pos = GetPos(controller_pose); auto forward = -glm::vec3(controller_pose[2]); - auto ray = _app->IntersectRay(origin, forward, 5.0f); + auto ray = _app->IntersectRay(controller_pos, forward, 5.0f); float len = ray.distance; - VRMat transform = {{{width, 0, 0, 0}, {0, 0, width, 0}, {0, len, 0, len * -0.5f}}}; + auto hmd_global_pos = GetPos(_app->GetTrackerPose(0)); + auto hmd_local_pos = glm::inverse(controller_pose) * glm::vec4(hmd_global_pos - controller_pos, 0); + hmd_local_pos.z = 0; + auto hmd_dir = glm::normalize(hmd_local_pos); + + VRMat transform = {{{width * hmd_dir.y, 0, width * hmd_dir.x, 0}, {width * -hmd_dir.x, 0, width * hmd_dir.y, 0}, {0, len, 0, len * -0.5f}}}; _laser.SetTransformTracker(_device_index, &transform); if (ray.overlay != nullptr) diff --git a/src/util.h b/src/util.h index 4cac8c8..bd29e39 100644 --- a/src/util.h +++ b/src/util.h @@ -10,6 +10,11 @@ typedef vr::HmdMatrix34_t VRMat; const VRMat VRMatIdentity{{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}}}; const int MAX_TRACKERS = vr::k_unMaxTrackedDeviceCount; +inline void PrintVec(glm::vec3 v) +{ + printf("(%.2f, %.2f, %.2f)\n", v.x, v.y, v.z); +} + inline void PrintMat(VRMat m) { printf("[%.2f, %.2f, %.2f, %.2f]\n", m.m[0][0], m.m[0][1], m.m[0][2], m.m[0][3]);