From ec15a39683bc0c6eeb22a70e54c34a838729062c Mon Sep 17 00:00:00 2001
From: netpro2k <netpro2k@gmail.com>
Date: Tue, 17 Jul 2018 15:49:04 -0700
Subject: [PATCH] Fix issue with objects spawning in the wrong position

---
 src/hub.js               | 21 +++++++++++++++++++--
 src/utils/media-utils.js |  6 ------
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/src/hub.js b/src/hub.js
index 65f541357..e9dcad31d 100644
--- a/src/hub.js
+++ b/src/hub.js
@@ -304,8 +304,25 @@ const onReady = async () => {
       NAF.connection.entities.completeSync(ev.detail.clientId);
     });
 
+    const offset = { x: 0, y: 0, z: -1.5 };
+    const playerCamera = document.querySelector("#player-camera").object3D;
+    const spawnPos = new THREE.Vector3();
+    const spawnRot = new THREE.Quaternion();
+    const spawnMediaInfrontOfPlayer = url => {
+      const entity = addMedia(url, true);
+      entity.addEventListener("loaded", () => {
+        console.log("body loaded");
+        spawnPos.copy(offset);
+        playerCamera.localToWorld(spawnPos);
+        playerCamera.getWorldQuaternion(spawnRot);
+        console.log(spawnPos, spawnRot);
+        entity.body.position.copy(spawnPos);
+        entity.body.quaternion.copy(spawnRot);
+      });
+    };
+
     scene.addEventListener("add_media", e => {
-      addMedia(e.detail);
+      spawnMediaInfrontOfPlayer(e.detail);
     });
 
     if (qsTruthy("mediaTools")) {
@@ -314,7 +331,7 @@ const onReady = async () => {
 
         const imgUrl = e.clipboardData.getData("text");
         console.log("Pasted: ", imgUrl, e);
-        addMedia(imgUrl);
+        spawnMediaInfrontOfPlayer(imgUrl);
       });
 
       document.addEventListener("dragover", e => {
diff --git a/src/utils/media-utils.js b/src/utils/media-utils.js
index 5c0efb2ba..93ad430f7 100644
--- a/src/utils/media-utils.js
+++ b/src/utils/media-utils.js
@@ -18,7 +18,6 @@ export const resolveFarsparkUrl = async url => {
 };
 
 let interactableId = 0;
-const offset = { x: 0, y: 0, z: -1.5 };
 export const addMedia = src => {
   const scene = AFRAME.scenes[0];
 
@@ -26,11 +25,6 @@ export const addMedia = src => {
   entity.id = "interactable-media-" + interactableId++;
   entity.setAttribute("networked", { template: "#interactable-media" });
   entity.setAttribute("media-loader", { src });
-  entity.setAttribute("offset-relative-to", {
-    target: "#player-camera",
-    offset: offset,
-    selfDestruct: true
-  });
   scene.appendChild(entity);
   return entity;
 };
-- 
GitLab