From bee5d1842f7127989e72d72acd4748b0d2e170b5 Mon Sep 17 00:00:00 2001
From: Brian Peiris <brianpeiris@gmail.com>
Date: Fri, 5 Oct 2018 18:32:58 -0700
Subject: [PATCH] Project position and snap to closest nav mesh poly

For https://github.com/MozillaReality/Spoke/issues/302
---
 package-lock.json                      |  2 +-
 src/components/character-controller.js | 23 ++++++++++++-----------
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 2cb1a811f..f5c74cb53 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -12632,7 +12632,7 @@
       }
     },
     "three-pathfinding": {
-      "version": "github:mozillareality/three-pathfinding#8ea5fed7418a1e93a7ba8917a622f871477169d9",
+      "version": "github:mozillareality/three-pathfinding#a52f437eaaf1b608c5f7fed046846bdbd79c75e7",
       "from": "github:mozillareality/three-pathfinding#hubs/master"
     },
     "three-to-cannon": {
diff --git a/src/components/character-controller.js b/src/components/character-controller.js
index 36c7c641f..28e48e6c2 100644
--- a/src/components/character-controller.js
+++ b/src/components/character-controller.js
@@ -171,29 +171,30 @@ AFRAME.registerComponent("character-controller", {
     console.warn("character-controller", msg);
   },
 
-  _updateNavState: function(pos, forceUpdate) {
+  _setNavNode: function(pos) {
+    if (this.navNode !== null) return;
     const { pathfinder } = this.el.sceneEl.systems.nav;
-    if (this.navGroup === null || forceUpdate) {
-      this.navGroup = pathfinder.getGroup(this.navZone, pos, true);
-    }
-    if (this.navNode === null || forceUpdate) {
-      this.navNode =
-        pathfinder.getClosestNode(pos, this.navZone, this.navGroup, true) ||
-        pathfinder.getClosestNode(pos, this.navZone, this.navGroup);
-    }
+    this.navNode =
+      pathfinder.getClosestNode(pos, this.navZone, this.navGroup, true) ||
+      pathfinder.getClosestNode(pos, this.navZone, this.navGroup);
   },
 
   setPositionOnNavMesh: function(start, end, object3D) {
     const { pathfinder } = this.el.sceneEl.systems.nav;
     if (!(this.navZone in pathfinder.zones)) return;
-    this._updateNavState(end);
+    if (this.navGroup === null) {
+      this.navGroup = pathfinder.getGroup(this.navZone, end, true, true);
+    }
+    this._setNavNode(end);
     this.navNode = pathfinder.clampStep(start, end, this.navNode, this.navZone, this.navGroup, object3D.position);
   },
 
   resetPositionOnNavMesh: function(position, navPosition, object3D) {
     const { pathfinder } = this.el.sceneEl.systems.nav;
     if (!(this.navZone in pathfinder.zones)) return;
-    this._updateNavState(navPosition, true);
+    this.navGroup = pathfinder.getGroup(this.navZone, navPosition, true);
+    this.navNode = null;
+    this._setNavNode(navPosition);
     object3D.position.copy(navPosition);
   },
 
-- 
GitLab