r3-legacy/bak/r3-d3-path-following.js

257 lines
6.5 KiB
JavaScript
Raw Permalink Normal View History

2016-12-15 14:53:39 +01:00
/**
* This component makes the parentEntity (ex. car) follow the path provided by the spline
2018-04-09 10:05:13 +02:00
* @param graphics R3.GraphicsRuntime
* @param apiPathFollowing R3.D3.API.PathFollowing
2016-12-15 14:53:39 +01:00
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.PathFollowing = function (
2016-12-15 14:53:39 +01:00
graphics,
apiPathFollowing
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(apiPathFollowing)) {
apiPathFollowing = {};
}
2018-04-09 10:05:13 +02:00
if (apiPathFollowing instanceof R3.D3.PathFollowing) {
return apiPathFollowing;
}
2018-04-09 10:05:13 +02:00
R3.D3.API.PathFollowing.call(
2016-12-15 14:53:39 +01:00
this,
apiPathFollowing.id,
apiPathFollowing.name,
apiPathFollowing.spline,
2016-12-19 17:44:15 +01:00
apiPathFollowing.mesh,
apiPathFollowing.raytraceMesh,
2016-12-15 14:53:39 +01:00
apiPathFollowing.accelleration,
apiPathFollowing.maxSpeed,
apiPathFollowing.baseOffset,
apiPathFollowing.maxOffset,
apiPathFollowing.steeringSpeed,
apiPathFollowing.targetOffset,
apiPathFollowing.currentOffset,
apiPathFollowing.currentPathValue,
apiPathFollowing.currentSpeed,
apiPathFollowing.direction,
apiPathFollowing.raycaster,
apiPathFollowing.currentPosition,
apiPathFollowing.futurePosition,
apiPathFollowing.up,
apiPathFollowing.rotationMatrix,
2017-01-02 17:05:40 +01:00
apiPathFollowing.rotationVector,
apiPathFollowing.parentEntity
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.baseOffset = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.baseOffset,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.maxOffset = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.maxOffset,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.targetOffset = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.targetOffset,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.currentOffset = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.currentOffset,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.raycaster = new R3.D3.Raycaster(
2016-12-15 14:53:39 +01:00
this.graphics,
this.raycaster
);
2018-04-09 10:05:13 +02:00
this.currentPosition = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.currentPosition,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.futurePosition = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.futurePosition,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.up = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.up,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.rotationMatrix = new R3.Matrix4(
2016-12-15 14:53:39 +01:00
this.graphics,
this.rotationMatrix,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.rotationVector = new R3.Quaternion(
2016-12-15 14:53:39 +01:00
this.graphics,
this.rotationVector,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.mx = new R3.Utils.MovingAverage(10);
this.my = new R3.Utils.MovingAverage(10);
this.mz = new R3.Utils.MovingAverage(10);
2017-01-03 18:15:03 +01:00
2018-04-09 10:05:13 +02:00
R3.Component.call(
2017-06-16 15:49:53 +02:00
this,
2018-04-09 10:05:13 +02:00
R3.Component.COMPONENT_PATH_FOLLOWING,
2017-06-16 15:49:53 +02:00
{
2018-04-09 10:05:13 +02:00
'spline': R3.D3.Spline,
'mesh' : R3.D3.Mesh,
'raytraceMesh' : R3.D3.Mesh
2017-06-16 15:49:53 +02:00
}
);
2016-12-15 14:53:39 +01:00
};
2018-04-09 10:05:13 +02:00
R3.D3.PathFollowing.prototype = Object.create(R3.D3.API.PathFollowing.prototype);
R3.D3.PathFollowing.prototype.constructor = R3.D3.PathFollowing;
2018-04-09 10:05:13 +02:00
R3.D3.PathFollowing.prototype.createInstance = function() {
2017-06-16 18:45:25 +02:00
2018-04-09 10:05:13 +02:00
console.log('R3.D3.PathFollowing.prototype.createInstance()');
2017-06-16 18:45:25 +02:00
2018-04-09 10:05:13 +02:00
R3.Component.prototype.createInstance.call(this);
2017-06-16 18:45:25 +02:00
2017-06-16 15:49:53 +02:00
};
2018-04-09 10:05:13 +02:00
R3.D3.PathFollowing.prototype.toApiObject = function() {
2018-04-09 10:05:13 +02:00
var apiPathFollowing = new R3.D3.API.PathFollowing(
this.id,
this.name,
2018-04-09 10:05:13 +02:00
R3.Utils.IdOrNull(this.spline),
R3.Utils.IdOrNull(this.mesh),
R3.Utils.IdOrNull(this.raytraceMesh),
this.accelleration,
this.maxSpeed,
2017-05-16 14:51:57 +02:00
this.baseOffset.toApiObject(),
this.maxOffset.toApiObject(),
this.steeringSpeed,
2017-05-16 14:51:57 +02:00
this.targetOffset.toApiObject(),
this.currentOffset.toApiObject(),
this.currentPathValue,
this.currentSpeed,
this.direction,
2017-05-16 14:51:57 +02:00
this.raycaster.toApiObject(),
this.currentPosition.toApiObject(),
this.futurePosition.toApiObject(),
this.up.toApiObject(),
this.rotationMatrix.toApiObject(),
this.rotationVector.toApiObject(),
2018-04-09 10:05:13 +02:00
R3.Utils.IdOrNull(this.parentEntity)
);
return apiPathFollowing;
};
2017-01-06 16:53:53 +01:00
/**
2018-04-09 10:05:13 +02:00
* Object path following to R3.D3.PathFollowing
2017-01-06 16:53:53 +01:00
* @param graphics
* @param objectComponent
2018-04-09 10:05:13 +02:00
* @returns {R3.D3.PathFollowing}
2017-01-06 16:53:53 +01:00
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.PathFollowing.FromObject = function(graphics, objectComponent) {
2018-04-09 10:05:13 +02:00
var apiPathFollowing = R3.D3.API.PathFollowing.FromObject(objectComponent);
2018-04-09 10:05:13 +02:00
return new R3.D3.PathFollowing(
graphics,
apiPathFollowing
);
2016-12-19 17:44:15 +01:00
};
/**
* Updates the component
* @param deltaTime
*/
2018-04-09 10:05:13 +02:00
R3.D3.PathFollowing.prototype.update = function(deltaTime) {
2016-12-19 17:44:15 +01:00
if (this.spline && this.mesh && this.raytraceMesh) {
this.currentSpeed += this.accelleration * deltaTime * this.direction;
if(this.currentSpeed > this.maxSpeed) {
this.currentSpeed = this.maxSpeed;
}
this.grain = (this.currentSpeed / 100.0);
2017-01-03 18:15:03 +01:00
var currentPosition = this.spline.getPointAt(this.currentPathValue);
this.currentPosition.x = currentPosition.x;
this.currentPosition.y = currentPosition.y;
this.currentPosition.z = currentPosition.z;
2016-12-19 17:44:15 +01:00
this.currentPathValue += this.grain;
if (this.currentPathValue >= 1) {
this.currentPathValue = this.currentPathValue - 1;
}
if (this.currentPathValue < 0) {
this.currentPathValue = 0.0;
}
2017-01-03 18:15:03 +01:00
var futurePosition = this.spline.getPointAt(this.currentPathValue);
this.futurePosition.x = futurePosition.x;
this.futurePosition.y = futurePosition.y;
this.futurePosition.z = futurePosition.z;
2016-12-19 17:44:15 +01:00
this.raycaster.setPosition(
2017-01-04 16:12:30 +01:00
this.currentPosition
2017-01-03 18:15:03 +01:00
);
this.raycaster.setDirection(
2017-01-04 16:12:30 +01:00
{
x : -this.up.x,
y : -this.up.y,
z : -this.up.z
}
2017-01-03 18:15:03 +01:00
);
2016-12-19 17:44:15 +01:00
var normal = this.raycaster.getFaceNormal(this.raytraceMesh);
if (normal) {
this.up.x = this.mx(normal.x);
this.up.y = this.my(normal.y);
this.up.z = this.mz(normal.z);
}
this.rotationMatrix.lookAt(
this.currentPosition,
this.futurePosition,
this.up
);
this.rotationVector.setFromRotationMatrix(this.rotationMatrix);
2017-01-09 15:20:48 +01:00
this.mesh.position.x = this.futurePosition.x;
this.mesh.position.y = this.futurePosition.y;
this.mesh.position.z = this.futurePosition.z;
2016-12-19 17:44:15 +01:00
/**
* Update Rotation
*/
this.mesh.quaternion.x = this.rotationVector.x;
this.mesh.quaternion.y = this.rotationVector.y;
this.mesh.quaternion.z = this.rotationVector.z;
this.mesh.quaternion.w = this.rotationVector.w;
}
};