path controls component. main.js cleanup

beta.r3js.org
polygonboutique 2016-11-23 10:23:24 +01:00
parent 105e931035
commit 6baed2483f
2 changed files with 170 additions and 19 deletions

View File

@ -0,0 +1,130 @@
/**
*
* @param id
* @param name
* @constructor
*/
GameLib.D3.ComponentPathControls = function ComponentPathFollowing(
id,
name
) {
this.id = id || GameLib.D3.Tools.RandomId();
if (typeof name == 'undefined') {
name = this.constructor.name;
}
this.name = name;
this.parentEntity = null;
// runtime
this.pathFollowingComponent = null;
this.keyLeftPressed = false;
this.keyRightPressed = false;
this.keyForwardPressed = false;
this.keyBackPressed = false;
this.keyBreakPressed = false;
GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathControls, GameLib.D3.ComponentInterface);
};
///////////////////////// Methods to override //////////////////////////
GameLib.D3.ComponentPathControls.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
if (this.keyForwardPressed) { // Forward [i]
this.pathFollowingComponent.direction = 1;
} else if (this.keyBackPressed){
this.pathFollowingComponent.direction = -1;
} else {
this.pathFollowingComponent.direction = 0;
}
// left right
if (this.keyLeftPressed) { // Left [j]
this.pathFollowingComponent.offset.x = 0;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = 1;
// also: set offset scale
} else if (this.keyRightPressed) { // Right [l]
this.pathFollowingComponent.offset.x = 0;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = -1;
// also: set offset scale
}
};
GameLib.D3.ComponentPathControls.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
this.pathFollowingComponent = parentEntity.getComponent(GameLib.D3.ComponentPathFollowing);
if(!this.pathFollowingComponent) {
console.error("ComponentPathControls. NO PATH FOLLOWING COMPONENT");
}
var component = this;
document.addEventListener('keydown', function(event) {
if (event.keyCode == 73) { // Forward [i]
component.keyForwardPressed = true;
} else if (event.keyCode == 75) { // Back [k]
component.keyBackPressed = true;
}
if (event.keyCode == 74) { // Left [j]
component.keyLeftPressed = true;
} else if (event.keyCode == 76) { // Right [l]
component.keyRightPressed = true;
}
if (event.keyCode == 66) {
component.keyBreakPressed = true;
}
}, false);
document.addEventListener('keyup', function(event) {
if (event.keyCode == 73) { // Forward [i]
component.keyForwardPressed = false;
} else if (event.keyCode == 75) { // Back [k]
component.keyBackPressed = false;
}
if (event.keyCode == 74) { // Left [j]
component.keyLeftPressed = false;
} else if (event.keyCode == 76) { // Right [l]
component.keyRightPressed = false;
}
if (event.keyCode == 66) {
component.keyBreakPressed = false;
}
}, false);
};

View File

@ -3,12 +3,18 @@
* @param id
* @param name
* @param splineCurve3
* @param accel
* @param maxSpeed
* @param baseOffset
* @constructor
*/
GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
id,
name,
splineCurve3
splineCurve3,
accel,
maxSpeed,
baseOffset
) {
this.id = id || GameLib.D3.Tools.RandomId();
@ -19,8 +25,17 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
this.name = name;
this.parentEntity = null;
this.splineCurve3 = splineCurve3;
this.maxSpeed = maxSpeed || 10.0;
this.accel = accel || 2.0;
this.baseOffset = baseOffset || new GameLib.D3.Vector3();
// runtime code
this.currentPathValue = 0.0;
this.offset = new GameLib.D3.Vector3();
this.currentSpeed = 0.0;
this.direction = 0;
GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathFollowing, GameLib.D3.ComponentInterface);
};
@ -33,47 +48,53 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
if(this.splineCurve3) {
if(this.currentPathValue >= 1) {
if(this.currentPathValue >= 1 || this.currentPathValue < 0) {
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
var position = this.splineCurve3.getPointAt(this.currentPathValue); //getPointAt?????
var position = this.splineCurve3.getPointAt(this.currentPathValue);
var rotation = this.splineCurve3.getTangentAt(this.currentPathValue).normalize();
var up = new THREE.Vector3( 0, 1, 0 );
var up = new THREE.Vector3(-1, 0, 0);
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 );
/* var quaternion = new THREE.Quaternion().setFromEuler(
new THREE.Euler(
rotation.x,
rotation.y,
rotation.z
)
);*/
// 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;
var offset = new THREE.Vector3(1, 0, 0);
offset = offset.applyQuaternion(quaternion).multiplyScalar(10.0);
console.log("offset x", offset);
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);
// apply to parent rigidbody instead of direclty to the mesh.
parentEntity.position.x = position.x + offset.x;
parentEntity.position.y = position.y + offset.y;
parentEntity.position.z = position.z + offset.z;
parentEntity.position.x = position.x + transformedOffset.x;
parentEntity.position.y = position.y + transformedOffset.y;
parentEntity.position.z = position.z + transformedOffset.z;
parentEntity.quaternion.x = quaternion.x;
parentEntity.quaternion.y = quaternion.y;
parentEntity.quaternion.z = quaternion.z;
parentEntity.quaternion.w = quaternion.w;
this.currentPathValue += (0.5 * deltaTime);
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;
}
}
};