From 54dd868b3c8ed42cc183bf4ebfc4fdc609b766d4 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Thu, 13 Sep 2018 11:54:59 +1200 Subject: [PATCH] Rotate mini tablet to keep it "vertical" --- scripts/system/miniTablet.js | 54 +++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/scripts/system/miniTablet.js b/scripts/system/miniTablet.js index 2482efec8d..8c2c8f8dc8 100644 --- a/scripts/system/miniTablet.js +++ b/scripts/system/miniTablet.js @@ -317,6 +317,7 @@ dimensions: Vec3.multiply(scaleFactor, MINI_UI_DIMENSIONS), dpi: MINI_UI_DPI / scaleFactor }); + updateRotation(); } function startExpandingTablet(hand) { @@ -380,6 +381,51 @@ }); } + function updateRotation() { + // Update the rotation of the tablet about its face normal so that its base is horizontal. + var COS_5_DEGREES = 0.996, + RADIANS_TO_DEGREES = DEGREES_180 / Math.PI, + defaultLocalRotation, + handOrientation, + defaultOrientation, + faceNormal, + desiredOrientation, + defaultYAxis, + desiredYAxis, + cross, + dot, + deltaAngle, + deltaRotation, + localRotation; + + defaultLocalRotation = MINI_ROTATIONS[uiHand]; + handOrientation = + Quat.multiply(MyAvatar.orientation, MyAvatar.getAbsoluteJointRotationInObjectFrame(handJointIndex(uiHand))); + defaultOrientation = Quat.multiply(handOrientation, defaultLocalRotation); + faceNormal = Vec3.multiplyQbyV(defaultOrientation, Vec3.UNIT_Z); + + if (Math.abs(Vec3.dot(faceNormal, Vec3.UNIT_Y)) > COS_5_DEGREES) { + // Don't rotate mini tablet if almost flat in the x-z plane. + return; + } else { + // Rotate the tablet so that its base is parallel with the x-z plane. + desiredOrientation = Quat.lookAt(Vec3.ZERO, Vec3.multiplyQbyV(defaultOrientation, Vec3.UNIT_Z), Vec3.UNIT_Y); + defaultYAxis = Vec3.multiplyQbyV(defaultOrientation, Vec3.UNIT_Y); + desiredYAxis = Vec3.multiplyQbyV(desiredOrientation, Vec3.UNIT_Y); + cross = Vec3.cross(defaultYAxis, desiredYAxis); + dot = Vec3.dot(defaultYAxis, desiredYAxis); + deltaAngle = Math.atan2(Vec3.length(cross), dot) * RADIANS_TO_DEGREES; + if (Vec3.dot(cross, Vec3.multiplyQbyV(desiredOrientation, Vec3.UNIT_Z)) > 0) { + deltaAngle = -deltaAngle; + } + deltaRotation = Quat.angleAxis(deltaAngle, Vec3.multiplyQbyV(defaultLocalRotation, Vec3.UNIT_Z)); + localRotation = Quat.multiply(deltaRotation, defaultLocalRotation); + } + Overlays.editOverlay(miniOverlay, { + localRotation: localRotation + }); + } + function hide() { Overlays.editOverlay(miniOverlay, { parentID: Uuid.NULL, // Release hold so that hand can grab tablet proper. @@ -446,6 +492,7 @@ size: size, startExpandingTablet: startExpandingTablet, sizeAboutHandles: sizeAboutHandles, + updateRotation: updateRotation, hide: hide, destroy: destroy }; @@ -478,7 +525,7 @@ miniState = MINI_DISABLED, miniHand, updateTimer = null, - UPDATE_INTERVAL = 300, + UPDATE_INTERVAL = 25, // Mini tablet scaling. MINI_SCALE_DURATION = 250, @@ -702,6 +749,11 @@ } else { setState(MINI_HIDING); } + + // If state hasn't changed, update mini tablet rotation. + if (miniState === MINI_VISIBLE) { + ui.updateRotation(); + } } function updateMiniGrabbed() {