diff --git a/src/game-lib-component-path-following.js b/src/game-lib-component-path-following.js index 6833059..dfaaeb1 100644 --- a/src/game-lib-component-path-following.js +++ b/src/game-lib-component-path-following.js @@ -14,6 +14,7 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( id, name, splineCurve3, + normalSplineCurve3, accel, maxSpeed, baseOffset, @@ -30,6 +31,7 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( this.parentEntity = null; this.splineCurve3 = splineCurve3; + this.normalSplineCurve3 = normalSplineCurve3; this.maxSpeed = maxSpeed || 10.0; this.accel = accel || 2.0; @@ -57,7 +59,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( parentEntity ) { - if(this.splineCurve3) { + if(this.splineCurve3 && this.normalSplineCurve3) { if(this.currentPathValue >= 1 || this.currentPathValue < 0) { this.currentPathValue = 0; @@ -67,19 +69,16 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( //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 nextIndex = this.currentPathValue + 0.005; + if(nextIndex > 1.0) { + nextIndex = 0; + } -// var nextPosition = this.splineCurve3.getPointAt(this.currentPathValue + 0.01); + var nextPoint = this.splineCurve3.getPointAt(nextIndex); + var normal = this.normalSplineCurve3.getPointAt(this.currentPathValue).normalize(); - var rotation = this.splineCurve3.getTangentAt(this.currentPathValue).normalize(); - //var tangent = this.splineCurve3.getTangentAt(this.currentPathValue).normalize(); - //var rotation = position.clone().normalize().cross( tangent ); - - // extract rotation - var forward = new THREE.Vector3(-1, 0, 0); - var axis = new THREE.Vector3(); - axis.crossVectors(forward, rotation).normalize(); - var radians = Math.acos(forward.dot(rotation)); - var quaternion = new THREE.Quaternion().setFromAxisAngle(axis, radians); + var matrix = new THREE.Matrix4().lookAt(position, nextPoint, normal); + var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix); { // update current speed var t = deltaTime * this.accel;