Merge branch 'sergej' of github.com:ToywheelDev/game-lib into sergej

beta.r3js.org
Theunis J. Botha 2016-11-24 10:57:53 +01:00
commit bdbe1c7664
4 changed files with 67 additions and 35 deletions

View File

@ -4,7 +4,7 @@
* @param name * @param name
* @constructor * @constructor
*/ */
GameLib.D3.ComponentPathControls = function ComponentPathFollowing( GameLib.D3.ComponentPathControls = function ComponentPathControls(
id, id,
name name
) { ) {
@ -28,6 +28,8 @@ GameLib.D3.ComponentPathControls = function ComponentPathFollowing(
GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathControls, GameLib.D3.ComponentInterface); GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathControls, GameLib.D3.ComponentInterface);
}; };
//#ifdef RUNTIME__
///////////////////////// Methods to override ////////////////////////// ///////////////////////// Methods to override //////////////////////////
GameLib.D3.ComponentPathControls.prototype.onUpdate = function( GameLib.D3.ComponentPathControls.prototype.onUpdate = function(
deltaTime, deltaTime,
@ -126,3 +128,5 @@ GameLib.D3.ComponentPathControls.prototype.onSetParentEntity = function(
}, false); }, false);
}; };
//#endif

View File

@ -7,6 +7,7 @@
* @param maxSpeed * @param maxSpeed
* @param baseOffset * @param baseOffset
* @param maxOffset * @param maxOffset
* @param steeringSpeed
* @constructor * @constructor
*/ */
GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing( GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
@ -16,7 +17,8 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
accel, accel,
maxSpeed, maxSpeed,
baseOffset, baseOffset,
maxOffset maxOffset,
steeringSpeed
) { ) {
this.id = id || GameLib.D3.Tools.RandomId(); this.id = id || GameLib.D3.Tools.RandomId();
@ -26,23 +28,29 @@ GameLib.D3.ComponentPathFollowing = function ComponentPathFollowing(
this.name = name; this.name = name;
this.parentEntity = null; this.parentEntity = null;
this.splineCurve3 = splineCurve3; this.splineCurve3 = splineCurve3;
this.maxSpeed = maxSpeed || 10.0; this.maxSpeed = maxSpeed || 10.0;
this.accel = accel || 2.0; this.accel = accel || 2.0;
this.baseOffset = baseOffset || new GameLib.D3.Vector3(); this.baseOffset = baseOffset || new GameLib.D3.Vector3();
this.maxOffset = maxOffset || new GameLib.D3.Vector3(); this.maxOffset = maxOffset || new GameLib.D3.Vector3(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
this.steeringSpeed = steeringSpeed || 1.0;
//#ifdef RUNTIME__
// runtime code // runtime code
this.offset = new GameLib.D3.Vector3(); // this one is our destination offset this.offset = new GameLib.D3.Vector3(); // this one is our destination offset
this.currentOffset = new GameLib.D3.Vector3();
this.currentPathValue = 0.0; this.currentPathValue = 0.0;
this.currentSpeed = 0.0; this.currentSpeed = 0.0;
this.direction = 0; this.direction = 0;
//#endif
GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathFollowing, GameLib.D3.ComponentInterface); GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathFollowing, GameLib.D3.ComponentInterface);
}; };
//#ifdef RUNTIME__
///////////////////////// Methods to override ////////////////////////// ///////////////////////// Methods to override //////////////////////////
GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function( GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
deltaTime, deltaTime,
@ -57,42 +65,62 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
//To maintain a constant speed, you use .getPointAt( t ) instead of .getPoint( t ). //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 //http://stackoverflow.com/questions/18400667/three-js-object-following-a-spline-path-rotation-tanget-issues-constant-sp
var position = this.splineCurve3.getPointAt(this.currentPathValue); var position = this.splineCurve3.getPointAt(this.currentPathValue);
var rotation = this.splineCurve3.getTangentAt(this.currentPathValue).normalize(); var rotation = this.splineCurve3.getTangentAt(this.currentPathValue).normalize();
var up = new THREE.Vector3(-1, 0, 0); // extract rotation
var forward = new THREE.Vector3(-1, 0, 0);
var axis = new THREE.Vector3(); var axis = new THREE.Vector3();
axis.crossVectors(up, rotation).normalize(); axis.crossVectors(forward, rotation).normalize();
var radians = Math.acos(up.dot(rotation)); var radians = Math.acos(forward.dot(rotation));
var quaternion = new THREE.Quaternion().setFromAxisAngle( axis, radians ); var quaternion = new THREE.Quaternion().setFromAxisAngle(axis, radians);
// move the entity { // update current speed
var t = deltaTime * this.accel; var t = deltaTime * this.accel;
t = t * t * t * (t * (6.0 * t - 15.0) + 10.0); t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
this.currentSpeed = this.currentSpeed + (this.maxSpeed * this.direction - this.currentSpeed) * t; this.currentSpeed = this.currentSpeed + (this.maxSpeed * this.direction - this.currentSpeed) * t;
}
var transformedOffset = new THREE.Vector3( { // update position
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. var t = deltaTime * this.accel;
parentEntity.position.x = position.x + transformedOffset.x; t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
parentEntity.position.y = position.y + transformedOffset.y;
parentEntity.position.z = position.z + transformedOffset.z;
parentEntity.quaternion.x = quaternion.x; var lerpedOffset = new THREE.Vector3(
parentEntity.quaternion.y = quaternion.y; this.currentOffset.x,
parentEntity.quaternion.z = quaternion.z; this.currentOffset.y,
parentEntity.quaternion.w = quaternion.w; this.currentOffset.z
).lerp(new THREE.Vector3(
this.maxOffset.x * this.offset.x,
this.maxOffset.y * this.offset.y,
this.maxOffset.z * this.offset.z
), t);
console.log("this.currentSpeed", this.currentSpeed); this.currentOffset.x = lerpedOffset.x;
this.currentOffset.y = lerpedOffset.y;
this.currentOffset.z = lerpedOffset.z;
var transformedOffset = new THREE.Vector3(
this.baseOffset.x + lerpedOffset.x,
this.baseOffset.y + lerpedOffset.y,
this.baseOffset.z + lerpedOffset.z
).applyQuaternion(quaternion);
// apply to parent rigidbody instead of direclty to the mesh.
parentEntity.position.x = position.x + transformedOffset.x;
parentEntity.position.y = position.y + transformedOffset.y;
parentEntity.position.z = position.z + transformedOffset.z;
}
{ // update rotation
parentEntity.quaternion.x = quaternion.x;
parentEntity.quaternion.y = quaternion.y;
parentEntity.quaternion.z = quaternion.z;
parentEntity.quaternion.w = quaternion.w;
}
// update current path value & check bounds
this.currentPathValue += (this.currentSpeed); this.currentPathValue += (this.currentSpeed);
if(this.currentSpeed >= this.maxSpeed) { if(this.currentSpeed >= this.maxSpeed) {
this.currentSpeed = this.maxSpeed; this.currentSpeed = this.maxSpeed;
} else if (this.currentSpeed <= 0) { } else if (this.currentSpeed <= 0) {
@ -100,7 +128,9 @@ GameLib.D3.ComponentPathFollowing.prototype.onUpdate = function(
} }
} }
}; };
//#endif
GameLib.D3.ComponentPathFollowing.prototype.onSetParentEntity = function( GameLib.D3.ComponentPathFollowing.prototype.onSetParentEntity = function(
parentScene, parentScene,

View File

@ -30,7 +30,7 @@ GameLib.D3.Game.prototype.render = function(
) { ) {
for(var s in this.scenes) { for(var s in this.scenes) {
var scene = this.scenes[s]; var scene = this.scenes[s];
scene.render(dt, renderer, scene.cameras[scene.activeCameraIndex]); scene.render(dt, renderer);
} }
}; };

View File

@ -648,9 +648,7 @@ GameLib.D3.Scene.prototype.render = function(
deltaTime, deltaTime,
renderer renderer
) { ) {
if(this.sceneCamera) { renderer.render(this.instance, this.cameras[this.activeCameraIndex].instance);
renderer.render(this.instance, this.sceneCamera);
}
}; };
/** /**