From 9a8097857b3fda16c6db453a1dde9105f4532cc6 Mon Sep 17 00:00:00 2001
From: netpro2k <netpro2k@gmail.com>
Date: Mon, 9 Jul 2018 18:39:25 -0700
Subject: [PATCH] Destroy interactables if they "fall out of the world"

---
 .../destroy-at-extreme-distances.js           | 21 +++++++++++++++++++
 src/hub.html                                  |  2 ++
 src/hub.js                                    |  1 +
 3 files changed, 24 insertions(+)
 create mode 100644 src/components/destroy-at-extreme-distances.js

diff --git a/src/components/destroy-at-extreme-distances.js b/src/components/destroy-at-extreme-distances.js
new file mode 100644
index 000000000..2660c3ce9
--- /dev/null
+++ b/src/components/destroy-at-extreme-distances.js
@@ -0,0 +1,21 @@
+AFRAME.registerComponent("destroy-at-extreme-distances", {
+  schema: {
+    xMin: { default: -1000 },
+    xMax: { default: 1000 },
+    yMin: { default: -1000 },
+    yMax: { default: 1000 },
+    zMin: { default: -1000 },
+    zMax: { default: 1000 }
+  },
+
+  tick: (function() {
+    const pos = new THREE.Vector3();
+    return function() {
+      const { xMin, xMax, yMin, yMax, zMin, zMax } = this.data;
+      this.el.object3D.getWorldPosition(pos);
+      if (pos.x < xMin || pos.x > xMax || pos.y < yMin || pos.y > yMax || pos.z < zMin || pos.z > zMax) {
+        this.el.parentNode.removeChild(this.el);
+      }
+    };
+  })()
+});
diff --git a/src/hub.html b/src/hub.html
index 3438ab9a0..3a649ad3c 100644
--- a/src/hub.html
+++ b/src/hub.html
@@ -187,6 +187,7 @@
                     auto-box-collider
                     position-at-box-shape-border="target:.delete-button"
                     auto-scale-cannon-physics-body
+                    destroy-at-extreme-distances
                 >
                     <a-entity class="delete-button" visible-while-frozen scale="0.08 0.08 0.08">
                         <a-entity mixin="rounded-text-button" remove-object-button position="0 0 0"> </a-entity>
@@ -208,6 +209,7 @@
                     image-plus
                     sticky-object="autoLockOnLoad: true; autoLockOnRelease: true;"
                     position-at-box-shape-border="target:.delete-button;dirs:forward,back"
+                    destroy-at-extreme-distances
                 >
                     <a-entity class="delete-button" visible-while-frozen>
                         <a-entity mixin="rounded-text-button" remove-networked-object-button position="0 0 0"> </a-entity>
diff --git a/src/hub.js b/src/hub.js
index fa2114911..123af93bf 100644
--- a/src/hub.js
+++ b/src/hub.js
@@ -73,6 +73,7 @@ import "./components/sticky-object";
 import "./components/auto-scale-cannon-physics-body";
 import "./components/position-at-box-shape-border";
 import "./components/remove-networked-object-button";
+import "./components/destroy-at-extreme-distances";
 
 import ReactDOM from "react-dom";
 import React from "react";
-- 
GitLab