2016-11-22 17:07:02 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* @param name
|
|
|
|
* @param splineCurve3
|
2016-11-23 10:23:24 +01:00
|
|
|
* @param accel
|
|
|
|
* @param maxSpeed
|
|
|
|
* @param baseOffset
|
2016-11-23 11:06:08 +01:00
|
|
|
* @param maxOffset
|
2016-11-22 17:07:02 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
|
|
|
|
id,
|
|
|
|
name,
|
2016-11-23 10:23:24 +01:00
|
|
|
splineCurve3,
|
|
|
|
accel,
|
|
|
|
maxSpeed,
|
2016-11-23 11:06:08 +01:00
|
|
|
baseOffset,
|
|
|
|
maxOffset
|
2016-11-22 17:07:02 +01:00
|
|
|
) {
|
|
|
|
this.id = id || GameLib.D3.Tools.RandomId();
|
|
|
|
|
|
|
|
if (typeof name == 'undefined') {
|
|
|
|
name = this.constructor.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.name = name;
|
|
|
|
this.parentEntity = null;
|
|
|
|
this.splineCurve3 = splineCurve3;
|
2016-11-23 10:23:24 +01:00
|
|
|
this.maxSpeed = maxSpeed || 10.0;
|
|
|
|
this.accel = accel || 2.0;
|
|
|
|
this.baseOffset = baseOffset || new GameLib.D3.Vector3();
|
2016-11-23 11:06:08 +01:00
|
|
|
this.maxOffset = maxOffset || new GameLib.D3.Vector3();
|
2016-11-22 17:07:02 +01:00
|
|
|
|
2016-11-23 10:23:24 +01:00
|
|
|
|
|
|
|
// runtime code
|
2016-11-23 11:06:08 +01:00
|
|
|
this.offset = new GameLib.D3.Vector3(); // this one is our destination offset
|
|
|
|
|
2016-11-22 17:07:02 +01:00
|
|
|
this.currentPathValue = 0.0;
|
2016-11-23 10:23:24 +01:00
|
|
|
this.currentSpeed = 0.0;
|
|
|
|
this.direction = 0;
|
|
|
|
|
2016-11-22 17:07:02 +01:00
|
|
|
GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathFollowing, GameLib.D3.ComponentInterface);
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////// Methods to override //////////////////////////
|
|
|
|
GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
|
|
|
|
deltaTime,
|
|
|
|
parentEntity
|
|
|
|
) {
|
|
|
|
|
|
|
|
if(this.splineCurve3) {
|
|
|
|
|
2016-11-23 10:23:24 +01:00
|
|
|
if(this.currentPathValue >= 1 || this.currentPathValue < 0) {
|
2016-11-22 17:07:02 +01:00
|
|
|
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
|
|
|
|
|
2016-11-23 10:23:24 +01:00
|
|
|
var position = this.splineCurve3.getPointAt(this.currentPathValue);
|
2016-11-22 17:07:02 +01:00
|
|
|
var rotation = this.splineCurve3.getTangentAt(this.currentPathValue).normalize();
|
|
|
|
|
2016-11-23 10:23:24 +01:00
|
|
|
var up = new THREE.Vector3(-1, 0, 0);
|
2016-11-22 17:07:02 +01:00
|
|
|
|
|
|
|
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 );
|
|
|
|
|
2016-11-23 10:23:24 +01:00
|
|
|
// move the entity
|
|
|
|
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;
|
2016-11-22 17:07:02 +01:00
|
|
|
|
2016-11-23 10:23:24 +01:00
|
|
|
var transformedOffset = new THREE.Vector3(
|
|
|
|
this.baseOffset.x + this.offset.x,
|
|
|
|
this.baseOffset.y + this.offset.y,
|
|
|
|
this.baseOffset.z + this.offset.z
|
|
|
|
).applyQuaternion(quaternion);
|
2016-11-22 17:07:02 +01:00
|
|
|
|
|
|
|
// apply to parent rigidbody instead of direclty to the mesh.
|
2016-11-23 10:23:24 +01:00
|
|
|
parentEntity.position.x = position.x + transformedOffset.x;
|
|
|
|
parentEntity.position.y = position.y + transformedOffset.y;
|
|
|
|
parentEntity.position.z = position.z + transformedOffset.z;
|
2016-11-22 17:07:02 +01:00
|
|
|
|
|
|
|
parentEntity.quaternion.x = quaternion.x;
|
|
|
|
parentEntity.quaternion.y = quaternion.y;
|
|
|
|
parentEntity.quaternion.z = quaternion.z;
|
|
|
|
parentEntity.quaternion.w = quaternion.w;
|
|
|
|
|
2016-11-23 10:23:24 +01:00
|
|
|
console.log("this.currentSpeed", this.currentSpeed);
|
|
|
|
|
|
|
|
this.currentPathValue += (this.currentSpeed);
|
|
|
|
|
|
|
|
if(this.currentSpeed >= this.maxSpeed) {
|
|
|
|
this.currentSpeed = this.maxSpeed;
|
|
|
|
} else if (this.currentSpeed <= 0) {
|
|
|
|
this.currentSpeed = 0.0;
|
|
|
|
}
|
2016-11-22 17:07:02 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.ComponentPathFollowing.prototype.onSetParentEntity = function(
|
|
|
|
parentScene,
|
|
|
|
parentEntity
|
|
|
|
) {
|
|
|
|
if(!this.splineCurve3) {
|
|
|
|
console.error("NO PATH GIVEN");
|
|
|
|
}
|
|
|
|
};
|