diff --git a/src/game-lib-component-path-following.js b/src/game-lib-component-path-following.js index eeb8b72..9ee3f96 100644 --- a/src/game-lib-component-path-following.js +++ b/src/game-lib-component-path-following.js @@ -50,6 +50,7 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( this.currentPathValue = 0.0; this.currentSpeed = 0.0; this.direction = 0; + this.pastNormal = new THREE.Vector3(0,0,0); //#endif // extend @@ -68,41 +69,28 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( if(this.splineCurve3 && this.trackThreeMeshArray) { - if(this.currentPathValue >= 1 || this.currentPathValue < 0) { - this.currentPathValue = 0; + this.grain = deltaTime / 100; + + var currentPosition = this.splineCurve3.getPoint(this.currentPathValue); + + this.currentPathValue += this.grain; + + var futurePosition = this.splineCurve3.getPoint(this.currentPathValue); + + if (this.currentPathValue >= 1) { + this.currentPathValue = this.currentPathValue - 1; } - //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); - - - // Update the current thing - var t = deltaTime * this.accel; - t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); - this.currentSpeed = this.currentSpeed + (this.maxSpeed * this.direction - this.currentSpeed) * t; - - // update current path value & check bounds - this.currentPathValue = this.currentPathValue + ( (this.currentPathValue + this.currentSpeed) - this.currentPathValue ) * t; - if(this.currentSpeed >= this.maxSpeed) { - this.currentSpeed = this.maxSpeed; - } else if (this.currentSpeed <= 0) { - this.currentSpeed = 0.0; + if (this.currentPathValue < 0) { + this.currentPathValue = 0.0; } - - var nextIndex = this.currentPathValue; - if(nextIndex > 1.0) { - nextIndex = 0; - } - - var nextPoint = this.splineCurve3.getPointAt(nextIndex); - // - - - - - - - - - - - - - - // Ray trace from the current position. + // Ray trace from the future position. // - - - - - - - - -- - - - - + ComponentPathFollowing_Three_Raycaster.set( - position, + futurePosition, new THREE.Vector3( 0, -1, @@ -110,55 +98,59 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( ) ); - var normal = new THREE.Vector3(0, 1, 0); - + var futureNormal = new THREE.Vector3(0, 1, 0); for(var m = 0, ml = this.trackThreeMeshArray.length; m < ml; ++m) { var intersect = ComponentPathFollowing_Three_Raycaster.intersectObject( this.trackThreeMeshArray[m] ); - if(intersect) { - normal = intersect[0].face.normal; + if(intersect && intersect.length > 0) { + futureNormal = intersect[0].face.normal; break; } } - var matrix = new THREE.Matrix4().lookAt(position, nextPoint, normal); - var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix); + // - - - - - - - - - - - - - + // Ray trace from the current position. + // - - - - - - - - -- - - - - - // update position - - t = deltaTime * ((this.currentSpeed / this.maxSpeed) * this.steeringSpeed); // put this into another variable - t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); - - var targetVector = new THREE.Vector3( - this.maxOffset.x * this.offset.x, - this.maxOffset.y * this.offset.y, - this.maxOffset.z * this.offset.z + ComponentPathFollowing_Three_Raycaster.set( + futurePosition, + new THREE.Vector3( + 0, + -1, + 0 + ) ); - var lerpedOffset = new THREE.Vector3( - this.currentOffset.x, - this.currentOffset.y, - this.currentOffset.z - ).lerp(targetVector, t); + var currentNormal = new THREE.Vector3(0, 1, 0); + for(var m = 0, ml = this.trackThreeMeshArray.length; m < ml; ++m) { + var intersect = ComponentPathFollowing_Three_Raycaster.intersectObject( + this.trackThreeMeshArray[m] + ); - this.currentOffset.x = lerpedOffset.x; - this.currentOffset.y = lerpedOffset.y; - this.currentOffset.z = lerpedOffset.z; + if(intersect && intersect.length > 0) { + currentNormal = intersect[0].face.normal; + break; + } + } - var transformedOffset = new THREE.Vector3( - this.baseOffset.x + lerpedOffset.x, - this.baseOffset.y + lerpedOffset.y, - this.baseOffset.z + lerpedOffset.z - ).applyQuaternion(quaternion); + var avgNormal = currentNormal.add(this.pastNormal).add(futureNormal).normalize(); + this.pastNormal = futureNormal; + + var matrix = new THREE.Matrix4().lookAt( + currentPosition, + futurePosition, + avgNormal + ); + var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix); // apply to parent rigidbody instead of direclty to the mesh. - parentEntity.position.x = position.x + transformedOffset.x; - parentEntity.position.y = position.y + transformedOffset.y; - parentEntity.position.z = position.z + transformedOffset.z; + parentEntity.position.x = futurePosition.x; + parentEntity.position.y = futurePosition.y; + parentEntity.position.z = futurePosition.z; // update rotation parentEntity.quaternion.x = quaternion.x;