From 105e931035aa462e219e1be02cad1a44d3054e1a Mon Sep 17 00:00:00 2001 From: polygonboutique Date: Tue, 22 Nov 2016 17:07:02 +0100 Subject: [PATCH] follow path component --- src/game-lib-component-path-following.js | 88 +++++++++++++++++++ ...game-lib-component-vehicle-ai-path-boid.js | 59 ------------- 2 files changed, 88 insertions(+), 59 deletions(-) create mode 100644 src/game-lib-component-path-following.js delete mode 100644 src/game-lib-component-vehicle-ai-path-boid.js diff --git a/src/game-lib-component-path-following.js b/src/game-lib-component-path-following.js new file mode 100644 index 0000000..f8b577a --- /dev/null +++ b/src/game-lib-component-path-following.js @@ -0,0 +1,88 @@ +/** + * + * @param id + * @param name + * @param splineCurve3 + * @constructor + */ +GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( + id, + name, + splineCurve3 +) { + this.id = id || GameLib.D3.Tools.RandomId(); + + if (typeof name == 'undefined') { + name = this.constructor.name; + } + + this.name = name; + this.parentEntity = null; + this.splineCurve3 = splineCurve3; + + this.currentPathValue = 0.0; + + GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathFollowing, GameLib.D3.ComponentInterface); +}; + +///////////////////////// Methods to override ////////////////////////// +GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( + deltaTime, + parentEntity +) { + + if(this.splineCurve3) { + + if(this.currentPathValue >= 1) { + this.currentPathValue = 0; + } + + //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); //getPointAt????? + var rotation = this.splineCurve3.getTangentAt(this.currentPathValue).normalize(); + + var up = new THREE.Vector3( 0, 1, 0 ); + + var axis = new THREE.Vector3(); + axis.crossVectors(up, rotation).normalize(); + var radians = Math.acos(up.dot(rotation)); + var quaternion = new THREE.Quaternion().setFromAxisAngle( axis, radians ); + + /* var quaternion = new THREE.Quaternion().setFromEuler( + new THREE.Euler( + rotation.x, + rotation.y, + rotation.z + ) + );*/ + + var offset = new THREE.Vector3(1, 0, 0); + offset = offset.applyQuaternion(quaternion).multiplyScalar(10.0); + + console.log("offset x", offset); + + // apply to parent rigidbody instead of direclty to the mesh. + parentEntity.position.x = position.x + offset.x; + parentEntity.position.y = position.y + offset.y; + parentEntity.position.z = position.z + offset.z; + + parentEntity.quaternion.x = quaternion.x; + parentEntity.quaternion.y = quaternion.y; + parentEntity.quaternion.z = quaternion.z; + parentEntity.quaternion.w = quaternion.w; + + this.currentPathValue += (0.5 * deltaTime); + + } +}; + +GameLib.D3.ComponentPathFollowing.prototype.onSetParentEntity = function( + parentScene, + parentEntity +) { + if(!this.splineCurve3) { + console.error("NO PATH GIVEN"); + } +}; \ No newline at end of file diff --git a/src/game-lib-component-vehicle-ai-path-boid.js b/src/game-lib-component-vehicle-ai-path-boid.js deleted file mode 100644 index 0c213cb..0000000 --- a/src/game-lib-component-vehicle-ai-path-boid.js +++ /dev/null @@ -1,59 +0,0 @@ -/** - * - * @param id - * @param name - * @param targetEntity GameLib.D3.Entity - * @param nodePath - * @constructor - */ -GameLib.D3.ComponentVehicleAIPathBoid = function( - id, - name, - targetEntity, - nodePath -) { - this.id = id|| GameLib.D3.Tools.RandomId(); - if (typeof name == 'undefined') { - name = this.constructor.name; - } - this.name = name; - - this.parentEntity = null; - GameLib.D3.Utils.Extend(GameLib.D3.ComponentVehicleAIPathBoid, GameLib.D3.ComponentInterface); - - this.targetEntity = targetEntity; - this.raycastVehicleComponent = null; - this.nodePath = nodePath; - this.debugArrows = {}; -}; - -///////////////////////////////////////////////////////////////////////// -///////////////////////// Methods to override /////////////////////////// -///////////////////////////////////////////////////////////////////////// -GameLib.D3.ComponentVehicleAIPathBoid.prototype.onSetParentEntity = function( - parentScene, - parentEntity -) { - this.parentEntity = parentEntity; - this.raycastVehicleComponent = parentEntity.getComponent(GameLib.D3.RaycastVehicle); - - if(!this.raycastVehicleComponent) { - console.warn("NO RAYCAST VEHICLE FOUND!"); - } -}; - -GameLib.D3.ComponentVehicleAIPathBoid.prototype.onUpdate = function( - deltaTime, - parentEntity -) { - if(this.targetEntity && this.raycastVehicleComponent && this.nodePath) { - - // boid code. - - var currentNodeIndex = this.nodePath.indexOf(this.targetEntity); - var distanceToCurrentNode = this.parentEntity.position.distanceTo(this.targetEntity.position); - - - - } -}; \ No newline at end of file