starting on mp

beta.r3js.org
polygonboutique 2016-12-06 11:42:41 +01:00
parent 490001d316
commit 7001449c56
1 changed files with 51 additions and 59 deletions

View File

@ -50,6 +50,7 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
this.currentPathValue = 0.0; this.currentPathValue = 0.0;
this.currentSpeed = 0.0; this.currentSpeed = 0.0;
this.direction = 0; this.direction = 0;
this.pastNormal = new THREE.Vector3(0,0,0);
//#endif //#endif
// extend // extend
@ -68,41 +69,28 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
if(this.splineCurve3 && this.trackThreeMeshArray) { if(this.splineCurve3 && this.trackThreeMeshArray) {
if(this.currentPathValue >= 1 || this.currentPathValue < 0) { this.grain = deltaTime / 100;
this.currentPathValue = 0;
var currentPosition = this.splineCurve3.getPoint(this.currentPathValue);
this.currentPathValue += this.grain;
var futurePosition = this.splineCurve3.getPoint(this.currentPathValue);
if (this.currentPathValue >= 1) {
this.currentPathValue = this.currentPathValue - 1;
} }
//To maintain a constant speed, you use .getPointAt( t ) instead of .getPoint( t ). if (this.currentPathValue < 0) {
//http://stackoverflow.com/questions/18400667/three-js-object-following-a-spline-path-rotation-tanget-issues-constant-sp this.currentPathValue = 0.0;
var position = this.splineCurve3.getPointAt(this.currentPathValue);
// Update the current thing
var t = deltaTime * this.accel;
t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
this.currentSpeed = this.currentSpeed + (this.maxSpeed * this.direction - this.currentSpeed) * t;
// update current path value & check bounds
this.currentPathValue = this.currentPathValue + ( (this.currentPathValue + this.currentSpeed) - this.currentPathValue ) * t;
if(this.currentSpeed >= this.maxSpeed) {
this.currentSpeed = this.maxSpeed;
} else if (this.currentSpeed <= 0) {
this.currentSpeed = 0.0;
} }
var nextIndex = this.currentPathValue;
if(nextIndex > 1.0) {
nextIndex = 0;
}
var nextPoint = this.splineCurve3.getPointAt(nextIndex);
// - - - - - - - - - - - - - // - - - - - - - - - - - - -
// Ray trace from the current position. // Ray trace from the future position.
// - - - - - - - - -- - - - - // - - - - - - - - -- - - - -
ComponentPathFollowing_Three_Raycaster.set( ComponentPathFollowing_Three_Raycaster.set(
position, futurePosition,
new THREE.Vector3( new THREE.Vector3(
0, 0,
-1, -1,
@ -110,55 +98,59 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
) )
); );
var normal = new THREE.Vector3(0, 1, 0); var futureNormal = new THREE.Vector3(0, 1, 0);
for(var m = 0, ml = this.trackThreeMeshArray.length; m < ml; ++m) { for(var m = 0, ml = this.trackThreeMeshArray.length; m < ml; ++m) {
var intersect = ComponentPathFollowing_Three_Raycaster.intersectObject( var intersect = ComponentPathFollowing_Three_Raycaster.intersectObject(
this.trackThreeMeshArray[m] this.trackThreeMeshArray[m]
); );
if(intersect) { if(intersect && intersect.length > 0) {
normal = intersect[0].face.normal; futureNormal = intersect[0].face.normal;
break; break;
} }
} }
var matrix = new THREE.Matrix4().lookAt(position, nextPoint, normal); // - - - - - - - - - - - - -
var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix); // Ray trace from the current position.
// - - - - - - - - -- - - - -
// update position ComponentPathFollowing_Three_Raycaster.set(
futurePosition,
t = deltaTime * ((this.currentSpeed / this.maxSpeed) * this.steeringSpeed); // put this into another variable new THREE.Vector3(
t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); 0,
-1,
var targetVector = new THREE.Vector3( 0
this.maxOffset.x * this.offset.x, )
this.maxOffset.y * this.offset.y,
this.maxOffset.z * this.offset.z
); );
var lerpedOffset = new THREE.Vector3( var currentNormal = new THREE.Vector3(0, 1, 0);
this.currentOffset.x, for(var m = 0, ml = this.trackThreeMeshArray.length; m < ml; ++m) {
this.currentOffset.y, var intersect = ComponentPathFollowing_Three_Raycaster.intersectObject(
this.currentOffset.z this.trackThreeMeshArray[m]
).lerp(targetVector, t); );
this.currentOffset.x = lerpedOffset.x; if(intersect && intersect.length > 0) {
this.currentOffset.y = lerpedOffset.y; currentNormal = intersect[0].face.normal;
this.currentOffset.z = lerpedOffset.z; break;
}
}
var transformedOffset = new THREE.Vector3( var avgNormal = currentNormal.add(this.pastNormal).add(futureNormal).normalize();
this.baseOffset.x + lerpedOffset.x, this.pastNormal = futureNormal;
this.baseOffset.y + lerpedOffset.y,
this.baseOffset.z + lerpedOffset.z var matrix = new THREE.Matrix4().lookAt(
).applyQuaternion(quaternion); currentPosition,
futurePosition,
avgNormal
);
var quaternion = new THREE.Quaternion().setFromRotationMatrix(matrix);
// apply to parent rigidbody instead of direclty to the mesh. // apply to parent rigidbody instead of direclty to the mesh.
parentEntity.position.x = position.x + transformedOffset.x; parentEntity.position.x = futurePosition.x;
parentEntity.position.y = position.y + transformedOffset.y; parentEntity.position.y = futurePosition.y;
parentEntity.position.z = position.z + transformedOffset.z; parentEntity.position.z = futurePosition.z;
// update rotation // update rotation
parentEntity.quaternion.x = quaternion.x; parentEntity.quaternion.x = quaternion.x;