med-material / Whack_A_Mole_VR

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

Constraint: Applies rotation after position (Bug) #186

Closed bastianilso closed 2 years ago

bastianilso commented 2 years ago

Our calibration scripts apply rotation after reading the player position. In some cases (e.g. AVALAB) this causes offset of the player position from the center.

From 4ecaadddc9cd9d887eb1b3c151aa5648fb9de5a4 Mon Sep 17 00:00:00 2001
From: ha <mathias.halilovic94@gmail.com>
Date: Mon, 27 Jun 2022 15:43:44 +0200
Subject: [PATCH] Constraint: Read Position after Rotation

When Position is set prior to rotation,
the position might not be accurately
representing the camera.
---
 Assets/Scripts/Constraint.cs | 43 +++++++++++++++++++-----------------
 1 file changed, 23 insertions(+), 20 deletions(-)

diff --git a/Assets/Scripts/Constraint.cs b/Assets/Scripts/Constraint.cs
index d3e353b..482ce08 100644
--- a/Assets/Scripts/Constraint.cs
+++ b/Assets/Scripts/Constraint.cs
@@ -27,33 +27,36 @@ public class Constraint : MonoBehaviour
             /*ROTATION*/
             // Get current head heading in scene (y-only, to avoid tilting the floor)
             float offsetAngle = steamCamera.rotation.eulerAngles.y;
-            float offsetX = steamCamera.transform.position.x;
-            float offsetZ = steamCamera.transform.position.z;

             // Now rotate CameraRig in opposite direction to compensate
-            cameraRig.Rotate(0f, -offsetAngle, 0f);
-
-            /*POSITION*/
-            // calculate how much to add or subtract from the height, to arrive at y 1.6
-            float headHeight = steamCamera.transform.localPosition.y;
+            cameraRig.Rotate(0f, -offsetAngle, 0f);
+
+            float offsetX = steamCamera.transform.position.x;
+            float offsetZ = steamCamera.transform.position.z;
+             /*POSITION*/
+             // calculate how much to add or subtract from the height, to arrive at y 1.6
+             float headHeight = steamCamera.transform.localPosition.y;
             //float heightChange = desiredHeight - headHeight;
             float xChange = transform.position.x - offsetX;
-            float zChange = transform.position.z - offsetZ;
-
-            /*FIT HEIGHT TO MOTORSPACE*/
-            // instead of moving the motorspace, we should move players
-            // such that they have the correct height for using the motorspace.
-
-            // A: determine current player controller height
+            float zChange = transform.position.z - offsetZ;
+
+            Debug.Log("transform.position.x: " + transform.position.x.ToString());
+            Debug.Log("offsetX: " + offsetX.ToString());
+
+            /*FIT HEIGHT TO MOTORSPACE*/
+            // instead of moving the motorspace, we should move players
+            // such that they have the correct height for using the motorspace.
+
+            // A: determine current player controller height
             float controllerHeight = steamController.position.y;
             Debug.Log("SteamController Height: " + steamController.position.y.ToString());
             // B: determine motorspace (bottom) height
-            float heightChange = desiredHeight - controllerHeight;
-
-            // C: offset player upwards, so the controller aligns with bottom of the motorspace.
-            
-            transform.position = new Vector3(xChange, heightChange, zChange);
-            Debug.Log("Seat recentered!");
+            float heightChange = desiredHeight - controllerHeight;
+
+             // C: offset player upwards, so the controller aligns with bottom of the motorspace.
+
+             transform.position = new Vector3(xChange, heightChange, zChange);
+              Debug.Log("Seat recentered!");
             calibrated = true;
             }
         }
-- 
2.29.2.windows.2
bastianilso commented 2 years ago

fixed with 34c0e604b1ff3d41364a4a1f9d486bfa3203bdbd