From 252067c4c8b23a929ea76eb6351f8fc911140cb7 Mon Sep 17 00:00:00 2001 From: polygonboutique Date: Tue, 6 Dec 2016 16:43:03 +0100 Subject: [PATCH] server comm --- src/game-lib-component-path-following.js | 66 ++++++++++++++++++++---- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/src/game-lib-component-path-following.js b/src/game-lib-component-path-following.js index 9ee3f96..2dcd720 100644 --- a/src/game-lib-component-path-following.js +++ b/src/game-lib-component-path-following.js @@ -69,14 +69,23 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( if(this.splineCurve3 && this.trackThreeMeshArray) { - this.grain = deltaTime / 100; + // current speed - var currentPosition = this.splineCurve3.getPoint(this.currentPathValue); + this.maxSpeed = 0.03; + this.accel = 0.1; + + this.currentSpeed += this.accel * deltaTime * this.direction; + if(this.currentSpeed > this.maxSpeed) { + this.currentSpeed = this.maxSpeed; + } + this.grain = (this.currentSpeed / 100.0); + + //this.grain = (deltaTime / 100.0); + + var currentPosition = this.splineCurve3.getPointAt(this.currentPathValue); this.currentPathValue += this.grain; - var futurePosition = this.splineCurve3.getPoint(this.currentPathValue); - if (this.currentPathValue >= 1) { this.currentPathValue = this.currentPathValue - 1; } @@ -85,6 +94,8 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( this.currentPathValue = 0.0; } + var futurePosition = this.splineCurve3.getPointAt(this.currentPathValue); + // - - - - - - - - - - - - - // Ray trace from the future position. // - - - - - - - - -- - - - - @@ -99,6 +110,11 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( ); var futureNormal = new THREE.Vector3(0, 1, 0); + var futurePositionRayCasted = new THREE.Vector3( + futurePosition.x, + futurePosition.y, + futurePosition.z + ); for(var m = 0, ml = this.trackThreeMeshArray.length; m < ml; ++m) { var intersect = ComponentPathFollowing_Three_Raycaster.intersectObject( this.trackThreeMeshArray[m] @@ -106,6 +122,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( if(intersect && intersect.length > 0) { futureNormal = intersect[0].face.normal; + futurePositionRayCasted = intersect[0].point; break; } } @@ -116,7 +133,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( // - - - - - - - - -- - - - - ComponentPathFollowing_Three_Raycaster.set( - futurePosition, + currentPosition, new THREE.Vector3( 0, -1, @@ -136,7 +153,8 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( } } - var avgNormal = currentNormal.add(this.pastNormal).add(futureNormal).normalize(); + //var avgNormal = currentNormal.add(this.pastNormal).add(futureNormal).normalize(); + var avgNormal = (this.pastNormal).add(futureNormal).normalize(); this.pastNormal = futureNormal; var matrix = new THREE.Matrix4().lookAt( @@ -144,13 +162,43 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( futurePosition, avgNormal ); + var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix); + var targetVector = new THREE.Vector3( + this.maxOffset.x * this.offset.x, + this.maxOffset.y * this.offset.y, + this.maxOffset.z * this.offset.z + ); + + var lerpedOffset = new THREE.Vector3( + this.currentOffset.x, + this.currentOffset.y, + this.currentOffset.z + ).lerp( + targetVector, + (this.grain * this.steeringSpeed) + ); + + this.currentOffset.x = lerpedOffset.x; + this.currentOffset.y = lerpedOffset.y; + this.currentOffset.z = lerpedOffset.z; + + var transformedOffset = new THREE.Vector3( + this.baseOffset.x + lerpedOffset.x, + this.baseOffset.y + lerpedOffset.y, + this.baseOffset.z + lerpedOffset.z + ).applyQuaternion(quaternion); // apply to parent rigidbody instead of direclty to the mesh. - parentEntity.position.x = futurePosition.x; - parentEntity.position.y = futurePosition.y; - parentEntity.position.z = futurePosition.z; + /* parentEntity.position.x = futurePositionRayCasted.x + transformedOffset.x; + parentEntity.position.y = futurePositionRayCasted.y + transformedOffset.y + this.parentEntity.mesh.geometry.boundingBox.y; + parentEntity.position.z = futurePositionRayCasted.z + transformedOffset.z; +*/ + + parentEntity.position.x = futurePosition.x + transformedOffset.x; + parentEntity.position.y = futurePosition.y + transformedOffset.y; + parentEntity.position.z = futurePosition.z + transformedOffset.z; // update rotation parentEntity.quaternion.x = quaternion.x;