From 24a17da5d4f770f1ea86328ffdfe7aaa7712173c Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Wed, 7 Dec 2016 09:52:23 +0100 Subject: [PATCH] start update path following --- src/game-lib-component-path-following.js | 22 +++++++++++----------- src/game-lib-utils.js | 18 +++++++++++++++++- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/game-lib-component-path-following.js b/src/game-lib-component-path-following.js index 8416989..613d924 100644 --- a/src/game-lib-component-path-following.js +++ b/src/game-lib-component-path-following.js @@ -2,8 +2,8 @@ * * @param id * @param name - * @param splineCurve3 - * @param trackThreeMeshArray + * @param spline GameLib.D3.Spline + * @param meshes[] GameLib.D3.Mesh * @param accel * @param maxSpeed * @param baseOffset @@ -14,8 +14,8 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( id, name, - splineCurve3, - trackThreeMeshArray, + spline, + meshes, accel, maxSpeed, baseOffset, @@ -31,8 +31,8 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( this.name = name; this.parentEntity = null; - this.splineCurve3 = splineCurve3; - this.trackThreeMeshArray = trackThreeMeshArray; + this.spline = spline; + this.trackThreeMeshArray = mesh; this.maxSpeed = maxSpeed || 10.0; this.accel = accel || 2.0; @@ -66,7 +66,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( parentEntity ) { - if(this.splineCurve3 && this.trackThreeMeshArray) { + if(this.spline && this.trackThreeMeshArray) { if(this.currentPathValue >= 1 || this.currentPathValue < 0) { this.currentPathValue = 0; @@ -74,7 +74,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( //To maintain a constant speed, you use .getPointAt( t ) instead of .getPoint( t ). //http://stackoverflow.com/questions/18400667/three-js-object-following-a-spline-path-rotation-tanget-issues-constant-sp - var position = this.splineCurve3.getPointAt(this.currentPathValue); + var position = this.spline.instance.getPointAt(this.currentPathValue); // Update the current thing @@ -96,7 +96,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( nextIndex = 0; } - var nextPoint = this.splineCurve3.getPointAt(nextIndex); + var nextPoint = this.spline.instance.getPointAt(nextIndex); // - - - - - - - - - - - - - // Ray trace from the current position. @@ -117,7 +117,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( this.trackThreeMeshArray[m] ); - if(intersect) { + if (intersect) { normal = intersect[0].face.normal; break; } @@ -174,7 +174,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onSetParentEntity = function( parentScene, parentEntity ) { - if(!this.splineCurve3) { + if (!this.spline) { console.error("NO PATH GIVEN"); } }; \ No newline at end of file diff --git a/src/game-lib-utils.js b/src/game-lib-utils.js index bbeb5b4..e0e6957 100644 --- a/src/game-lib-utils.js +++ b/src/game-lib-utils.js @@ -329,4 +329,20 @@ GameLib.D3.Utils.FixPolyZPlane = function(verticesFlat, grain) { } return vertices; -}; \ No newline at end of file +}; + +GameLib.D3.Utils.MovingAverage = function(period) { + var nums = []; + return function(num) { + nums.push(num); + if (nums.length > period) + nums.splice(0,1); // remove the first element of the array + var sum = 0; + for (var i in nums) + sum += nums[i]; + var n = period; + if (nums.length < period) + n = nums.length; + return(sum/n); + } +};