From a5750adc4cc3c679bc7fbd28dc6eb654a7fca2fd Mon Sep 17 00:00:00 2001
From: Greg Fodor <gfodor@gmail.com>
Date: Sat, 3 Nov 2018 18:57:25 +0000
Subject: [PATCH] Separate right and left hand offsets, switch hand collider to
 be a box, align oculus hands better

---
 src/components/controls-shape-offset.js | 14 ++++++----
 src/components/hand-controls2.js        | 37 +++++++++++++++++++++----
 src/components/ik-controller.js         |  8 +++---
 3 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/src/components/controls-shape-offset.js b/src/components/controls-shape-offset.js
index d234b971a..49bfaf4ce 100644
--- a/src/components/controls-shape-offset.js
+++ b/src/components/controls-shape-offset.js
@@ -1,4 +1,4 @@
-import { CONTROLLER_OFFSETS } from "./hand-controls2.js";
+import { LEFT_CONTROLLER_OFFSETS, RIGHT_CONTROLLER_OFFSETS } from "./hand-controls2.js";
 
 /**
  * Sets the offset of the aframe-physics shape on this entity based on the current VR controller type
@@ -7,11 +7,12 @@ import { CONTROLLER_OFFSETS } from "./hand-controls2.js";
  */
 AFRAME.registerComponent("controls-shape-offset", {
   schema: {
-    additionalOffset: { type: "vec3", default: { x: 0, y: -0.03, z: -0.04 } }
+    additionalOffset: { type: "vec3", default: { x: 0, y: 0, z: -0.04 } }
   },
   init: function() {
     this.controller = null;
     this.shapeAdded = false;
+    this.isLeft = this.el.getAttribute("hand-controls2") === "left";
 
     this._handleControllerConnected = this._handleControllerConnected.bind(this);
     this.el.addEventListener("controllerconnected", this._handleControllerConnected);
@@ -24,8 +25,9 @@ AFRAME.registerComponent("controls-shape-offset", {
   tick: function() {
     if (!this.shapeAdded && this.controller) {
       this.shapeAdded = true;
-      const hasOffset = CONTROLLER_OFFSETS.hasOwnProperty(this.controller);
-      const offset = hasOffset ? CONTROLLER_OFFSETS[this.controller] : CONTROLLER_OFFSETS.default;
+      const offsets = this.isLeft ? LEFT_CONTROLLER_OFFSETS : RIGHT_CONTROLLER_OFFSETS;
+      const hasOffset = offsets.hasOwnProperty(this.controller);
+      const offset = hasOffset ? offsets[this.controller] : offsets.default;
       const position = new THREE.Vector3();
       const quaternion = new THREE.Quaternion();
       const scale = new THREE.Vector3();
@@ -34,8 +36,8 @@ AFRAME.registerComponent("controls-shape-offset", {
       quaternion.conjugate();
 
       const shape = {
-        shape: "sphere",
-        radius: "0.02",
+        shape: "box",
+        halfExtents: { x: 0.03, y: 0.04, z: 0.05 },
         orientation: quaternion,
         offset: position
       };
diff --git a/src/components/hand-controls2.js b/src/components/hand-controls2.js
index 3a97888b1..0e50a1658 100644
--- a/src/components/hand-controls2.js
+++ b/src/components/hand-controls2.js
@@ -14,9 +14,27 @@ const POSES = {
 
 // TODO: If the hands or controllers are mispositioned, then rightHand.controllerPose and rightHand.pose
 //       should be bound differently.
-export const CONTROLLER_OFFSETS = {
+export const LEFT_CONTROLLER_OFFSETS = {
   default: new THREE.Matrix4(),
-  "oculus-touch-controls": new THREE.Matrix4().makeTranslation(0, -0.015, 0.04),
+  "oculus-touch-controls": new THREE.Matrix4().makeTranslation(-0.025, -0.03, 0.1),
+  "oculus-go-controls": new THREE.Matrix4(),
+  "vive-controls": new THREE.Matrix4().compose(
+    new THREE.Vector3(0, -0.017, 0.13),
+    new THREE.Quaternion().setFromEuler(new THREE.Euler(-40 * THREE.Math.DEG2RAD, 0, 0)),
+    new THREE.Vector3(1, 1, 1)
+  ),
+  "windows-motion-controls": new THREE.Matrix4().compose(
+    new THREE.Vector3(0, -0.017, 0.13),
+    new THREE.Quaternion().setFromEuler(new THREE.Euler(-40 * THREE.Math.DEG2RAD, 0, 0)),
+    new THREE.Vector3(1, 1, 1)
+  ),
+  "daydream-controls": new THREE.Matrix4().makeTranslation(0, 0, -0.04),
+  "gearvr-controls": new THREE.Matrix4()
+};
+
+export const RIGHT_CONTROLLER_OFFSETS = {
+  default: new THREE.Matrix4(),
+  "oculus-touch-controls": new THREE.Matrix4().makeTranslation(0.025, -0.03, 0.1),
   "oculus-go-controls": new THREE.Matrix4(),
   "vive-controls": new THREE.Matrix4().compose(
     new THREE.Vector3(0, -0.017, 0.13),
@@ -40,11 +58,18 @@ export const CONTROLLER_OFFSETS = {
 AFRAME.registerComponent("hand-controls2", {
   schema: { default: "left" },
 
-  getControllerOffset() {
-    if (CONTROLLER_OFFSETS[this.connectedController] === undefined) {
-      return CONTROLLER_OFFSETS.default;
+  getLeftControllerOffset() {
+    if (LEFT_CONTROLLER_OFFSETS[this.connectedController] === undefined) {
+      return LEFT_CONTROLLER_OFFSETS.default;
+    }
+    return LEFT_CONTROLLER_OFFSETS[this.connectedController];
+  },
+
+  getRightControllerOffset() {
+    if (RIGHT_CONTROLLER_OFFSETS[this.connectedController] === undefined) {
+      return RIGHT_CONTROLLER_OFFSETS.default;
     }
-    return CONTROLLER_OFFSETS[this.connectedController];
+    return RIGHT_CONTROLLER_OFFSETS[this.connectedController];
   },
 
   init() {
diff --git a/src/components/ik-controller.js b/src/components/ik-controller.js
index e7c9260f8..be7b8a93d 100644
--- a/src/components/ik-controller.js
+++ b/src/components/ik-controller.js
@@ -180,11 +180,11 @@ AFRAME.registerComponent("ik-controller", {
     rootToChest.multiplyMatrices(hips.matrix, chest.matrix);
     invRootToChest.getInverse(rootToChest);
 
-    this.updateHand(this.hands.left, leftHand, leftController);
-    this.updateHand(this.hands.right, rightHand, rightController);
+    this.updateHand(this.hands.left, leftHand, leftController, true);
+    this.updateHand(this.hands.right, rightHand, rightController, false);
   },
 
-  updateHand(handState, handObject3D, controller) {
+  updateHand(handState, handObject3D, controller, isLeft) {
     const hand = handObject3D.el;
     const handMatrix = handObject3D.matrix;
     const controllerObject3D = controller.object3D;
@@ -201,7 +201,7 @@ AFRAME.registerComponent("ik-controller", {
       const handControls = controller.components["hand-controls2"];
 
       if (handControls) {
-        handMatrix.multiply(handControls.getControllerOffset());
+        handMatrix.multiply(isLeft ? handControls.getLeftControllerOffset() : handControls.getRightControllerOffset());
       }
 
       handMatrix.multiply(handState.rotation);
-- 
GitLab