From 3283eb40ce4d002a8f68ef520456d2295a226fe6 Mon Sep 17 00:00:00 2001 From: polygonboutique Date: Tue, 29 Nov 2016 13:53:12 +0100 Subject: [PATCH] parent entity link component --- src/game-lib-component-entity-parent.js | 41 +++++++---- src/game-lib-component-entity-permutation.js | 17 +++-- src/game-lib-component-path-ai.js | 4 -- src/game-lib-component-path-following.js | 75 ++++++++++---------- 4 files changed, 76 insertions(+), 61 deletions(-) diff --git a/src/game-lib-component-entity-parent.js b/src/game-lib-component-entity-parent.js index 4b7684f..1c40c5f 100644 --- a/src/game-lib-component-entity-parent.js +++ b/src/game-lib-component-entity-parent.js @@ -8,7 +8,8 @@ GameLib.D3.ComponentEntityParent = function ComponentEntityParent( id, name, - parent + parent, + centerToOrigin ) { this.id = id || GameLib.D3.Tools.RandomId(); @@ -19,9 +20,9 @@ GameLib.D3.ComponentEntityParent = function ComponentEntityParent( this.parent = parent; this.parentEntity = null; + this.centerToOrigin = centerToOrigin; this.originPosition = new GameLib.D3.Vector3(0, 0, 0); - // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. GameLib.D3.Utils.Extend(GameLib.D3.ComponentEntityParent, GameLib.D3.ComponentInterface); }; @@ -34,16 +35,24 @@ GameLib.D3.ComponentEntityParent.prototype.onLateUpdate = function( if(parentEntity && this.parent) { - // parentEntity = this component's parent - parentEntity.position.x = this.parent.position.x /*+ this.parentOrigin.x + this.originPosition.x*/; - parentEntity.position.y = this.parent.position.y /*+ this.parentOrigin.y + this.originPosition.y*/; - parentEntity.position.z = this.parent.position.z /*+ this.parentOrigin.z + this.originPosition.z*/; + if(this.centerToOrigin) { + parentEntity.position.x = this.parent.position.x + this.parent.origin.x - this.originPosition.x; + parentEntity.position.y = this.parent.position.y + this.parent.origin.y - this.originPosition.y; + parentEntity.position.z = this.parent.position.z + this.parent.origin.z - this.originPosition.z; + } else { + parentEntity.position.x = this.parent.position.x; + parentEntity.position.y = this.parent.position.y; + parentEntity.position.z = this.parent.position.z; + } parentEntity.quaternion.x = this.parent.quaternion.x; parentEntity.quaternion.y = this.parent.quaternion.y; parentEntity.quaternion.z = this.parent.quaternion.z; parentEntity.quaternion.w = this.parent.quaternion.w; + parentEntity.scale.x = this.parent.scale.x; + parentEntity.scale.y = this.parent.scale.y; + parentEntity.scale.z = this.parent.scale.z; } }; @@ -53,17 +62,21 @@ GameLib.D3.ComponentEntityParent.prototype.onSetParentEntity = function( ) { if(parentEntity && parentEntity.mesh) { - /* var scale = parentEntity.mesh.scale.clone(); - /!* parentEntity.mesh.updateMatrix(); - parentEntity.mesh.geometry.applyMatrix( parentEntity.mesh.matrix ); - parentEntity.mesh.updateMatrix();*!/ - */ + // The entities should already be scaled down and stuff + // The matrix transform should already be applied to the geometry. - /*this.parentOrigin = this.parent.mesh.geometry.center(); - this.originPosition = parentEntity.mesh.geometry.center();*/ + if(this.centerToOrigin) { + if(GameLib.D3.Utils.UndefinedOrNull(this.parent.origin)) { + this.parent.origin = this.parent.mesh.geometry.center(); + parentEntity.position.x = this.parent.position.x - this.parent.origin.x; + parentEntity.position.y = this.parent.position.y - this.parent.origin.y; + parentEntity.position.z = this.parent.position.z - this.parent.origin.z; + } + + this.originPosition = parentEntity.mesh.geometry.center(); + } - // don't forget to set the scale on the entity itself!!! } }; diff --git a/src/game-lib-component-entity-permutation.js b/src/game-lib-component-entity-permutation.js index 7e3149e..4484411 100644 --- a/src/game-lib-component-entity-permutation.js +++ b/src/game-lib-component-entity-permutation.js @@ -23,11 +23,20 @@ GameLib.D3.ComponentEntityPermutation = function ComponentEntityPermutation( this.parentEntity = null; - this.positionOffset = positionOffset || new GameLib.D3.Vector3(0, 0, 0); - this.quaternionOffset = quaternionOffset || new GameLib.D3.Vector4(0, 0, 0, 1); - this.scaleOffset = scaleOffset || new GameLib.D3.Vector3(0, 0, 0); + if(GameLib.D3.Utils.UndefinedOrNull(positionOffset)) { + positionOffset = new GameLib.D3.Vector3(0, 0, 0); + } this.positionOffset = positionOffset; + + + if(GameLib.D3.Utils.UndefinedOrNull(quaternionOffset)) { + quaternionOffset = new GameLib.D3.Vector4(0, 0, 0, 1); + } this.quaternionOffset = quaternionOffset; + + + if(GameLib.D3.Utils.UndefinedOrNull(scaleOffset)) { + scaleOffset = new GameLib.D3.Vector3(0, 0, 0); + } this.scaleOffset = scaleOffset; - // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. GameLib.D3.Utils.Extend(GameLib.D3.ComponentEntityPermutation, GameLib.D3.ComponentInterface); }; diff --git a/src/game-lib-component-path-ai.js b/src/game-lib-component-path-ai.js index 6a85d37..db12295 100644 --- a/src/game-lib-component-path-ai.js +++ b/src/game-lib-component-path-ai.js @@ -65,10 +65,6 @@ GameLib.D3.ComponentPathAI.prototype.onUpdate = function( this.pathFollowingComponent.direction = 1; - - - - // - - -- - - - - -- - - - - - - - - - - - - - - // D E B U G G I N G // - - - - - - - - - - - - - - - - - - diff --git a/src/game-lib-component-path-following.js b/src/game-lib-component-path-following.js index de3852a..eeb8b72 100644 --- a/src/game-lib-component-path-following.js +++ b/src/game-lib-component-path-following.js @@ -42,14 +42,17 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( this.steeringSpeed = steeringSpeed || 1.0; - //#ifdef RUNTIME__ // runtime code + + //#ifdef RUNTIME__ this.offset = new GameLib.D3.Vector3(); // this one is our destination offset this.currentOffset = new GameLib.D3.Vector3(); this.currentPathValue = 0.0; this.currentSpeed = 0.0; this.direction = 0; //#endif + + // extend GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathFollowing, GameLib.D3.ComponentInterface); }; @@ -95,9 +98,9 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( var nextPoint = this.splineCurve3.getPointAt(nextIndex); - // - - - - - - - - - - - - - - // Raytrace from thhe current position. + // Ray trace from the current position. + // - - - - - - - - -- - - - - ComponentPathFollowing_Three_Raycaster.set( position, new THREE.Vector3( @@ -107,7 +110,6 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( ) ); - var normal = new THREE.Vector3(0, 1, 0); for(var m = 0, ml = this.trackThreeMeshArray.length; m < ml; ++m) { @@ -116,9 +118,7 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( ); if(intersect) { - normal = intersect[0].face.normal; - break; } } @@ -127,47 +127,44 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( var matrix = new THREE.Matrix4().lookAt(position, nextPoint, normal); var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix); - { // update 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); + 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 - ); + 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, t); + var lerpedOffset = new THREE.Vector3( + this.currentOffset.x, + this.currentOffset.y, + this.currentOffset.z + ).lerp(targetVector, t); - this.currentOffset.x = lerpedOffset.x; - this.currentOffset.y = lerpedOffset.y; - this.currentOffset.z = lerpedOffset.z; + 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); + 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 = position.x + transformedOffset.x; - parentEntity.position.y = position.y + transformedOffset.y; - parentEntity.position.z = position.z + transformedOffset.z; - } + // 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; - { - // update rotation - parentEntity.quaternion.x = quaternion.x; - parentEntity.quaternion.y = quaternion.y; - parentEntity.quaternion.z = quaternion.z; - parentEntity.quaternion.w = quaternion.w; - } + // update rotation + parentEntity.quaternion.x = quaternion.x; + parentEntity.quaternion.y = quaternion.y; + parentEntity.quaternion.z = quaternion.z; + parentEntity.quaternion.w = quaternion.w; } };