med-material / Whack_A_Mole_VR

Whack-A-Mole in VR
MIT License
3 stars 16 forks source link

Mirror Effect: Mirrored controller might appear inverse #184

Closed bastianilso closed 2 years ago

bastianilso commented 2 years ago

image

bastianilso commented 2 years ago

I could not replicate the controllers appearing inverse of each other - in fact I had issues making the mirrored controller appear at all. The mirrored controllers seemed to be tracking the wrong objects which has been fixed in 3e0190d5a7bd45d82c44eea35adb1b40294be877 and 456f3c626f906ddf63b135a7690c07f8be2aed9f .

Might be related to this issue, will have to test in AVALAB to verify.

bastianilso commented 2 years ago

Replicated the issue in AVALAB. This might be solved by applying a -180 degree rotation to the model or by applying this rotation to its container on startup.

Should be solvable by applying this.transform.Rotate(new Vector3(0f, -180f, 0f)); after the mirror rotation itself.

Dropping off older some WIP code from the investigation..

        if (prevAngle == Vector3.zero)
        {
            prevAngle = this.transform.eulerAngles;
            // apply correct orientation after sampling current start orientation.
            this.transform.Rotate(new Vector3(0f,-180f,0f));
        }

        newRotEuler = controllerToMirror.transform.eulerAngles;
        angle = new Vector3(newRotEuler.x - prevAngle.x, -(newRotEuler.y - prevAngle.y), -(newRotEuler.z - prevAngle.z));
        // Mirror rotation
        //newRot = controllerToMirror.transform.localRotation;
        Debug.Log(angle);
        this.transform.Rotate(angle);
        prevAngle = newRotEuler;
bastianilso commented 2 years ago

Patch to apply

From 168ff6e7c2602bf92befcc2fe15680440660b9d4 Mon Sep 17 00:00:00 2001
From: ha <mathias.halilovic94@gmail.com>
Date: Mon, 27 Jun 2022 14:55:36 +0200
Subject: [PATCH] ControllerMirror: use Quaternion.Inverse()

ControllerMirror was flipping numbers in
a Quaternion manually and was not preserving
controller orientation. Fix this by using
Quaternion.Inverse() and by applying a
180 degree rotation afterwards.
---
 Assets/Scripts/Pointers/ControllerMirror.cs | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/Assets/Scripts/Pointers/ControllerMirror.cs b/Assets/Scripts/Pointers/ControllerMirror.cs
index ab46967..37a3d6e 100644
--- a/Assets/Scripts/Pointers/ControllerMirror.cs
+++ b/Assets/Scripts/Pointers/ControllerMirror.cs
@@ -77,9 +77,12 @@ public class ControllerMirror : MonoBehaviour
         newPos = controllerToMirror.transform.position;
         newPos = new Vector3 (newPos.x * -1f, newPos.y, newPos.z);

-        // Mirror rotation
-        newRot = controllerToMirror.transform.localRotation;
-        newRot = new Quaternion(newRot.x, newRot.y * -1f, newRot.z * -1f, newRot.w);
+        // mirror the y and z rotational axes, and keep the x axis intact.
+        // We do this by inversing X before we pass it to Quaternion.Inverse()
+        newRot = Quaternion.Euler(new Vector3(-controllerToMirror.transform.eulerAngles.x, controllerToMirror.transform.eulerAngles.y, controllerToMirror.transform.eulerAngles.z));
+        // Multiply the rotation by the base calibration angles before inversion.
+        // This ensures we get the right controller orientation.
+        newRot = Quaternion.Inverse(newRot * Quaternion.Euler(this.transform.root.eulerAngles));

         this.transform.position = newPos;
         this.transform.localRotation = newRot;
-- 
2.29.2.windows.2
bastianilso commented 2 years ago

fixed with 1259430cde431b19819385bef73dc86d0a9e92a4