From 7367e8859597972368a7da8f6d051e421a2b86ab Mon Sep 17 00:00:00 2001
From: Greg Fodor <gfodor@gmail.com>
Date: Wed, 14 Nov 2018 06:24:54 +0000
Subject: [PATCH] Quaternion math kinda working

---
 src/components/follow-entity.js      | 41 ++++++++++++++++++++++------
 src/react-components/chat-message.js | 23 ++++++++--------
 2 files changed, 44 insertions(+), 20 deletions(-)

diff --git a/src/components/follow-entity.js b/src/components/follow-entity.js
index 6eccb76d2..4f9efef0f 100644
--- a/src/components/follow-entity.js
+++ b/src/components/follow-entity.js
@@ -2,7 +2,9 @@ AFRAME.registerComponent("follow-entity", {
   schema: {
     target: { type: "selector" },
     offset: { type: "vec3" },
-    fixedY: { type: "number" }
+    worldYOffset: { type: "number", default: 0.35 },
+    speed: { type: "number", default: 0.005 },
+    ySnapInterval: { type: "number", default: 0.75 }
   },
 
   init() {
@@ -15,25 +17,48 @@ AFRAME.registerComponent("follow-entity", {
     const obj = this.el.object3D;
     const target = this.data.target.object3D;
 
+    const snappedRot = new THREE.Euler(
+      Math.floor((target.rotation.x + Math.PI / 6) / (Math.PI / 4)) * (Math.PI / 4),
+      target.rotation.y,
+      target.rotation.z,
+      target.rotation.order
+    );
+
+    const snappedQ = new THREE.Quaternion();
+    snappedQ.setFromEuler(snappedRot);
+    //snappedQ.copy(target.quaternion);
+    const snappedXForm = new THREE.Matrix4();
+    const snappedXFormWorld = new THREE.Matrix4();
+    snappedXForm.compose(
+      target.position,
+      snappedQ,
+      target.scale
+    );
+    snappedXFormWorld.multiplyMatrices(target.parent.matrixWorld, snappedXForm);
+    //snappedXForm.copy(target.matrixWorld);
+    //snappedXForm.makeRotationFromEuler(snappedRot);
+
     this.targetPos.copy(this.offset);
-    target.localToWorld(this.targetPos);
+    this.targetPos.applyMatrix4(snappedXFormWorld);
+    //target.localToWorld(this.targetPos);
 
     if (obj.parent) {
       obj.parent.worldToLocal(this.targetPos);
     }
 
-    if (this.data.fixedY) {
-      this.targetPos.y = this.data.fixedY;
-    }
+    //this.targetPos.y = this.data.worldYOffset;
+    /*this.targetPos.y =
+      Math.floor((this.targetPos.y - this.data.ySnapInterval / 2) / this.data.ySnapInterval) * this.data.ySnapInterval +
+      this.data.ySnapInterval / 2;*/
 
     if (!this.started) {
       obj.position.copy(this.targetPos);
       this.started = true;
     } else {
       obj.position.set(
-        obj.position.x + (this.targetPos.x - obj.position.x) * 0.0005 * dt,
-        obj.position.y + (this.targetPos.y - obj.position.y) * 0.0005 * dt,
-        obj.position.z + (this.targetPos.z - obj.position.z) * 0.0005 * dt
+        obj.position.x + (this.targetPos.x - obj.position.x) * this.data.speed * dt,
+        obj.position.y + (this.targetPos.y - obj.position.y) * this.data.speed * dt,
+        obj.position.z + (this.targetPos.z - obj.position.z) * this.data.speed * dt
       );
     }
 
diff --git a/src/react-components/chat-message.js b/src/react-components/chat-message.js
index 1ff291fd6..ce4a8a40b 100644
--- a/src/react-components/chat-message.js
+++ b/src/react-components/chat-message.js
@@ -135,8 +135,7 @@ export async function createInWorldChatMessage(body, lowResolution) {
   entity.appendChild(meshEntity);
   entity.setAttribute("follow-entity", {
     target: "#player-camera",
-    offset: { x: 0, y: 0, z: -1 },
-    fixedY: 0.7
+    offset: { x: 0, y: 0.0, z: -0.8 }
   });
 
   await nextTick();
@@ -150,19 +149,19 @@ export async function createInWorldChatMessage(body, lowResolution) {
     property: "position",
     dur: 15000,
     from: { x: 0, y: 0, z: 0 },
-    to: { x: 0, y: 0.4, z: -0.4 },
+    to: { x: 0, y: 0, z: -0.05 },
     easing: "easeOutQuad"
   });
 
-  meshEntity.setAttribute("animation__opacity", {
-    property: "meshMaterial.opacity",
-    isRawProperty: true,
-    delay: 3000,
-    dur: 13000,
-    from: 1.0,
-    to: 0.0,
-    easing: "easeInQuad"
-  });
+  //meshEntity.setAttribute("animation__opacity", {
+  //  property: "meshMaterial.opacity",
+  //  isRawProperty: true,
+  //  delay: 3000,
+  //  dur: 13000,
+  //  from: 1.0,
+  //  to: 0.0,
+  //  easing: "easeInQuad"
+  //});
 
   meshEntity.addEventListener("animationcomplete__opacity", () => {
     entity.parentNode.removeChild(entity);
-- 
GitLab