From c572393a543cdcdd88d397aa4d335c304d289249 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Mon, 25 Sep 2017 21:55:39 +0200 Subject: [PATCH] apply axis and angle to vectors --- src/game-lib-a-component-a.js | 11 +++++++- src/game-lib-d3-mesh-0.js | 53 +++++++++++++++++++++++++++++++++-- src/game-lib-vector3.js | 38 +++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) diff --git a/src/game-lib-a-component-a.js b/src/game-lib-a-component-a.js index bea467b..0b90f3b 100644 --- a/src/game-lib-a-component-a.js +++ b/src/game-lib-a-component-a.js @@ -291,7 +291,16 @@ GameLib.Component.prototype.clone = function() { apiObject.id = GameLib.Utils.RandomId(); - apiObject.name = apiObject.name + ' (Clone)'; + var matches = apiObject.name.match(/Clone \((\d+)\)/); + + if (matches && matches.length > 1) { + if (matches.length === 2) { + var number = Number(matches[1]); + apiObject.name = apiObject.name.replace(/Clone \(\d*\)/, 'Clone (' + (number+1) + ')'); + } + } else { + apiObject.name = apiObject.name + ' Clone (1)' + } var runtimeObject = null; diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index 36a521e..ea55321 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -935,6 +935,22 @@ GameLib.D3.Mesh.prototype.centerAroundOrigin = function() { this.position.z = -position.z; this.updateInstancePosition(); + + return position; +}; + +GameLib.D3.Mesh.prototype.applyAxisAngleToPosition = function(axis, angle) { + this.position.applyAxisAngle(this.quaternion.axis, this.quaternion.angle); + this.updateInstancePosition(); +}; + +GameLib.D3.Mesh.prototype.translate = function(vector3) { + + this.position.x += vector3.x; + this.position.y += vector3.y; + this.position.z += vector3.z; + + this.updateInstancePosition(); }; GameLib.D3.Mesh.prototype.getDistanceFromCenter = function() { @@ -958,20 +974,46 @@ GameLib.D3.Mesh.prototype.applyPositionRotationScale = function() { this.instance.geometry.applyMatrix(this.instance.matrix); + /** + * Reset position + * @type {number} + */ this.position.x = 0; this.position.y = 0; this.position.z = 0; this.updateInstancePosition(); + /** + * Reset scale + * @type {number} + */ this.scale.x = 1; this.scale.y = 1; this.scale.z = 1; this.instance.scale.set(1,1,1); + /** + * Reset rotation + * @type {number} + */ + this.quaternion.x = 0; + this.quaternion.y = 0; + this.quaternion.z = 0; + this.quaternion.w = 1; + this.quaternion.axis.x = 0; + this.quaternion.axis.y = 0; + this.quaternion.axis.z = 0; + this.quaternion.angle = 0; this.updateInstanceRotationFromAxisAngle(); + /** + * Update our instance matrix + */ this.instance.updateMatrix(); + /** + * Let this reflect back to our vertices + */ this.updateVerticesFromGeometryInstance(this.instance.geometry); }; @@ -997,10 +1039,15 @@ GameLib.D3.Mesh.prototype.updateInstanceRotationFromAxisAngle = function(axis, a }; GameLib.D3.Mesh.prototype.updateInstancePosition = function() { - this.position.instance.x = this.position.x; - this.position.instance.y = this.position.y; - this.position.instance.z = this.position.z; + + this.position.instance.set( + this.position.x, + this.position.y, + this.position.z + ); + this.instance.position.copy(this.position.instance); + if (this.helper) { this.removeHelper(); this.createHelper(); diff --git a/src/game-lib-vector3.js b/src/game-lib-vector3.js index f1e4b8c..75f3b9f 100644 --- a/src/game-lib-vector3.js +++ b/src/game-lib-vector3.js @@ -126,6 +126,44 @@ GameLib.Vector3.prototype.copy = function() { ) }; +/** + * Create a negative version of this vector + * @returns {GameLib.Vector3} + */ +GameLib.Vector3.prototype.negativeCopy = function() { + return new GameLib.Vector3( + this.implementation, + new GameLib.API.Vector3( + -this.x, + -this.y, + -this.z + ), + this.parentObject, + this.grain + ) +}; + +/** + * Applies rotation specified by axis / angle to this vector - its a wrapper for three.. + * @param axis + * @param angle + */ +GameLib.Vector3.prototype.applyAxisAngle = function(axis, angle) { + + this.instance.applyAxisAngle( + new THREE.Vector3( + axis.x, + axis.y, + axis.z + ), + angle + ); + + this.x = this.instance.x; + this.y = this.instance.y; + this.z = this.instance.z; +}; + GameLib.Vector3.prototype.setFrom = function(vector3) { this.x = vector3.x; this.y = vector3.y;