From 12c5ac14501786d1beef66354d98f90805c2bf66 Mon Sep 17 00:00:00 2001 From: "Theunis J. Botha" Date: Tue, 3 Jan 2017 18:15:03 +0100 Subject: [PATCH] drive input component - spline updates --- src/game-lib-d3-api-mesh.js | 4 +-- src/game-lib-d3-api-path-following.js | 6 ----- src/game-lib-d3-input-drive.js | 18 +++++++++---- src/game-lib-d3-mesh.js | 7 +++++ src/game-lib-d3-path-following.js | 39 ++++++++++++++++++++++++--- src/game-lib-d3-raycaster.js | 32 +++++++++++++++++++++- 6 files changed, 89 insertions(+), 17 deletions(-) diff --git a/src/game-lib-d3-api-mesh.js b/src/game-lib-d3-api-mesh.js index bcd2884..edf8ae5 100644 --- a/src/game-lib-d3-api-mesh.js +++ b/src/game-lib-d3-api-mesh.js @@ -73,12 +73,12 @@ GameLib.D3.API.Mesh = function( this.name = name; if (GameLib.Utils.UndefinedOrNull(vertices)) { - throw new Error('Cannot create a mesh with no vertices'); + vertices = []; } this.vertices = vertices; if (GameLib.Utils.UndefinedOrNull(faces)) { - throw new Error('Cannot create a mesh with no faces'); + faces = []; } this.faces = faces; diff --git a/src/game-lib-d3-api-path-following.js b/src/game-lib-d3-api-path-following.js index c99b41e..e4a9a6f 100644 --- a/src/game-lib-d3-api-path-following.js +++ b/src/game-lib-d3-api-path-following.js @@ -165,12 +165,6 @@ GameLib.D3.API.PathFollowing = function ( rotationVector = new GameLib.API.Quaternion(); } this.rotationVector = rotationVector; - - this.mx = new GameLib.Utils.MovingAverage(10); - - this.my = new GameLib.Utils.MovingAverage(10); - - this.mz = new GameLib.Utils.MovingAverage(10); }; GameLib.D3.API.PathFollowing.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-input-drive.js b/src/game-lib-d3-input-drive.js index 2b5e9ff..847e8a5 100644 --- a/src/game-lib-d3-input-drive.js +++ b/src/game-lib-d3-input-drive.js @@ -32,6 +32,8 @@ GameLib.D3.Input.Drive = function RuntimeInputDrive( this.keyRight = false; + this.distance = 0; + this.instance = this.createInstance(); }; @@ -114,15 +116,21 @@ GameLib.D3.Input.Drive.FromObjectComponent = function(graphics, objectComponent) GameLib.D3.Input.Drive.prototype.update = function(deltaTime) { if (this.pathFollowingComponent) { + + this.pathFollowingComponent.mesh.localPosition.x = this.pathFollowingComponent.rotationMatrix.up.x; + this.pathFollowingComponent.mesh.localPosition.y = this.pathFollowingComponent.rotationMatrix.up.y; + this.pathFollowingComponent.mesh.localPosition.z = this.pathFollowingComponent.rotationMatrix.up.z; + if (this.keyLeft) { - - - - this.pathFollowingComponent.mesh.localPosition + this.distance -= 0.05; } if (this.keyRight) { - console.log('keyRight'); + this.distance += 0.05; } + + this.pathFollowingComponent.mesh.localPosition.x += (this.distance * this.pathFollowingComponent.rotationMatrix.left.x); + this.pathFollowingComponent.mesh.localPosition.y += (this.distance * this.pathFollowingComponent.rotationMatrix.left.y); + this.pathFollowingComponent.mesh.localPosition.z += (this.distance * this.pathFollowingComponent.rotationMatrix.left.z); } }; \ No newline at end of file diff --git a/src/game-lib-d3-mesh.js b/src/game-lib-d3-mesh.js index 9d555e6..ef5e663 100644 --- a/src/game-lib-d3-mesh.js +++ b/src/game-lib-d3-mesh.js @@ -92,6 +92,13 @@ GameLib.D3.Mesh = function RuntimeMesh( this.computeNormals = computeNormals; this.instance = this.createInstance(false); + + this.instance.geometry.computeBoundingBox(); + + this.width = this.instance.geometry.boundingBox.max.x - this.instance.geometry.boundingBox.min.x; + this.height = this.instance.geometry.boundingBox.max.y - this.instance.geometry.boundingBox.min.y; + this.depth = this.instance.geometry.boundingBox.max.z - this.instance.geometry.boundingBox.min.z; + }; GameLib.D3.Mesh.prototype = Object.create(GameLib.D3.API.Mesh.prototype); diff --git a/src/game-lib-d3-path-following.js b/src/game-lib-d3-path-following.js index aaf297e..fd8aca9 100644 --- a/src/game-lib-d3-path-following.js +++ b/src/game-lib-d3-path-following.js @@ -105,6 +105,13 @@ GameLib.D3.PathFollowing = function RuntimePathFollowing( this.rotationVector ); + + this.mx = new GameLib.Utils.MovingAverage(10); + + this.my = new GameLib.Utils.MovingAverage(10); + + this.mz = new GameLib.Utils.MovingAverage(10); + }; GameLib.D3.PathFollowing.prototype = Object.create(GameLib.D3.API.PathFollowing.prototype); @@ -238,7 +245,11 @@ GameLib.D3.PathFollowing.prototype.update = function(deltaTime) { } this.grain = (this.currentSpeed / 100.0); - this.currentPosition = this.spline.getPointAt(this.currentPathValue); + var currentPosition = this.spline.getPointAt(this.currentPathValue); + + this.currentPosition.x = currentPosition.x; + this.currentPosition.y = currentPosition.y; + this.currentPosition.z = currentPosition.z; this.currentPathValue += this.grain; @@ -250,12 +261,30 @@ GameLib.D3.PathFollowing.prototype.update = function(deltaTime) { this.currentPathValue = 0.0; } - this.futurePosition = this.spline.getPointAt(this.currentPathValue); + var futurePosition = this.spline.getPointAt(this.currentPathValue); + + this.futurePosition.x = futurePosition.x; + this.futurePosition.y = futurePosition.y; + this.futurePosition.z = futurePosition.z; this.raycaster.setPosition( this.futurePosition ); + var direction = new GameLib.Vector3( + this.graphics, + this, + new GameLib.API.Vector3( + this.up.x * -1, + this.up.y * -1, + this.up.z * -1 + ) + ); + + this.raycaster.setDirection( + direction + ); + var normal = this.raycaster.getFaceNormal(this.raytraceMesh); if (normal) { @@ -277,8 +306,12 @@ GameLib.D3.PathFollowing.prototype.update = function(deltaTime) { /** * Update Position */ + // this.mesh.position.x = this.futurePosition.x + (this.rotationMatrix.up.x * (this.mesh.height - (2 * this.rotationMatrix.up.x)) / 2);// + transformedOffset.x; + // this.mesh.position.y = this.futurePosition.y + (this.rotationMatrix.up.y * (this.mesh.height - (2 * this.rotationMatrix.up.y)) / 2); + // this.mesh.position.z = this.futurePosition.z + (this.rotationMatrix.up.z * (this.mesh.height - (2 * this.rotationMatrix.up.z)) / 2);// + transformedOffset.z; + this.mesh.position.x = this.futurePosition.x;// + transformedOffset.x; - this.mesh.position.y = this.futurePosition.y;// + transformedOffset.y; + this.mesh.position.y = this.futurePosition.y; this.mesh.position.z = this.futurePosition.z;// + transformedOffset.z; /** diff --git a/src/game-lib-d3-raycaster.js b/src/game-lib-d3-raycaster.js index 2702f33..2f4c969 100644 --- a/src/game-lib-d3-raycaster.js +++ b/src/game-lib-d3-raycaster.js @@ -111,7 +111,9 @@ GameLib.D3.Raycaster.prototype.setDirection = function( GameLib.D3.Raycaster.prototype.setPosition = function( position ) { - this.position = position; + this.position.x = position.x; + this.position.y = position.y; + this.position.z = position.z; this.position.updateInstance(); @@ -144,4 +146,32 @@ GameLib.D3.Raycaster.prototype.getFaceNormal = function(mesh) { } return normal; +}; + +/** + * Returns the face normal (if any) of an intersection between current ray position, direction and a provided mesh + * @param mesh GameLib.D3.Mesh + * @returns {null | GameLib.Vector3} + */ +GameLib.D3.Raycaster.prototype.getIntersectPoint = function(mesh) { + + var point = null; + + var intersect = this.instance.intersectObject( + mesh.instance + ); + + if (intersect && intersect.length > 0) { + point = new GameLib.Vector3( + this.graphics, + this, + new GameLib.API.Vector3( + intersect[0].point.x, + intersect[0].point.y, + intersect[0].point.z + ) + ); + } + + return point; }; \ No newline at end of file