From 948fe6fc10bf2e0d7eae13f66c99f1f256db9ef0 Mon Sep 17 00:00:00 2001
From: Kevin Lee <kevin@infinite-lee.com>
Date: Wed, 6 Dec 2017 14:41:07 -0800
Subject: [PATCH] users will now randomly spawn around the table facing towards
 the center (may vary depending on headset configuration)

---
 src/index.js | 11 ++++++++++-
 src/utils.js | 21 +++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/src/index.js b/src/index.js
index 918898be4..64feef0d6 100644
--- a/src/index.js
+++ b/src/index.js
@@ -27,7 +27,7 @@ import "./systems/personal-space-bubble";
 
 import registerNetworkScheams from "./network-schemas";
 import registerInputMappings from "./input-mappings";
-import { promptForName, getCookie, parseJwt } from "./utils";
+import { promptForName, getCookie, parseJwt, getSpawnPositionInCircle, getRotationToTarget } from "./utils";
 import Config from "./config";
 
 registerNetworkScheams();
@@ -94,6 +94,15 @@ window.App = {
     const myNametag = document.querySelector("#player-rig .nametag");
     myNametag.setAttribute("text", "value", username);
 
+    //change spawn position & rotation
+    const myHead = document.querySelector("#player-rig #head");
+    let rotation = Math.random() * 2 * Math.PI;
+    let position = myHead.getAttribute("position");
+    let newPosition = getSpawnPositionInCircle(0, 0, 4, rotation);
+    newPosition.y = position.y;
+    myHead.setAttribute("position", newPosition);
+    myHead.setAttribute("rotation", getRotationToTarget(newPosition, {x:0, y:0, z:0}));
+
     scene.addEventListener("action_share_screen", shareScreen);
 
     const mediaStream = await navigator.mediaDevices.getUserMedia({
diff --git a/src/utils.js b/src/utils.js
index b37764120..a939eb6b0 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -187,3 +187,24 @@ export function parseJwt (token) {
   var base64 = base64Url.replace('-', '+').replace('_', '/');
   return JSON.parse(window.atob(base64));
 }
+
+export function getSpawnPositionInCircle(originX, originZ, radius, rotation) {
+  let x = originX + radius * Math.cos(rotation);
+  let z = originZ + radius * Math.sin(rotation);
+
+  return {x: x, z: z};
+}
+
+export function getRotationToTarget(eye, target) {
+  let m4 = new THREE.Matrix4();
+  eye = new THREE.Vector3(eye.x, eye.y, eye.z);
+  target = new THREE.Vector3(target.x, target.y, target.z);
+  m4.lookAt(eye, target, new THREE.Vector3(0,1,0));
+  let euler = new THREE.Euler();
+  euler.setFromRotationMatrix(m4, "YXZ");
+  return {x: 0, y:(toDegrees(euler.y) + 360) % 360, z:0};
+}
+
+export function toDegrees(angle) {
+  return angle * (180 / Math.PI);
+}
-- 
GitLab