Skip to content

Commit

Permalink
Merge pull request #1 from BabylonJS/master
Browse files Browse the repository at this point in the history
test sync
  • Loading branch information
vousk committed Mar 22, 2014
2 parents 527aacb + f705f94 commit fed793e
Show file tree
Hide file tree
Showing 43 changed files with 2,542 additions and 150 deletions.
24 changes: 24 additions & 0 deletions Babylon/Cameras/Controllers/babylon.globalAxisFactorsFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

var BABYLON = BABYLON || {};

(function () {
BABYLON.GlobalAxisFactorsFilter = function (scene, target, xFactor, yFactor, zFactor) {
BABYLON.inputFilter.call(this, scene,target);
this.xFactor = xFactor;
this.yFactor = yFactor;
this.zFactor = zFactor;

this._globalMovement = new BABYLON.Vector3(0, 0, 0);
};
BABYLON.GlobalAxisFactorsFilter.prototype = Object.create(BABYLON.inputFilter.prototype);
BABYLON.GlobalAxisFactorsFilter.prototype.moveRelative = function (relativeMovement) {
var orientation = this.getOrientation();
BABYLON.Vector3.TransformNormalToRef(relativeMovement, this.getOrientationMatrix(), this._globalMovement);
this._globalMovement.x *= this.xFactor;
this._globalMovement.y *= this.yFactor;
this._globalMovement.z *= this.zFactor;
BABYLON.Vector3.TransformNormalToRef(this._globalMovement, this.getInvertOrientationMatrix(), relativeMovement);
this.target.moveRelative(relativeMovement);
};
})();
20 changes: 20 additions & 0 deletions Babylon/Cameras/Controllers/babylon.gravityInputController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

var BABYLON = BABYLON || {};

(function () {
BABYLON.GravityInputController = function (scene, target) {
BABYLON.InputController.call(this, scene, target);
this._moveVectorGlobal = new BABYLON.Vector3(0, 0, 0);
this._moveVectorLocal = new BABYLON.Vector3(0, 0, 0);
this._fallSpeed = .6;
};
BABYLON.GravityInputController.prototype = Object.create(BABYLON.InputController.prototype);
BABYLON.GravityInputController.prototype.update = function () {
this._moveVectorGlobal.x = 0;
this._moveVectorGlobal.y = -this._fallSpeed * BABYLON.Tools.GetDeltaTime() / 1000.0;
this._moveVectorGlobal.z = 0;
BABYLON.Vector3.TransformNormalToRef(this._moveVectorGlobal, this.target.getInvertOrientationMatrix(), this._moveVectorLocal);
this.target.moveRelative(this._moveVectorLocal);
};
})();
41 changes: 41 additions & 0 deletions Babylon/Cameras/Controllers/babylon.inputCollisionFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";

var BABYLON = BABYLON || {};

(function () {
BABYLON.InputCollisionFilter = function (scene, target, ellipsoid) {
BABYLON.inputFilter.call(this, scene, target);
this._transformedDirection = new BABYLON.Vector3();
this._tempNewPosition = new BABYLON.Vector3();
this._tempNewPosition2 = new BABYLON.Vector3();
this._ellipsoid = ellipsoid || new BABYLON.Vector3(.5,.5,.5);
this._collider = new BABYLON.Collider();
this._collidedPosition = new BABYLON.Vector3(0, 0, 0);
this._cameraHeight = 1.7;
this._positionBottom = new BABYLON.Vector3(0, 0, 0);
};
BABYLON.InputCollisionFilter.prototype = Object.create(BABYLON.inputFilter.prototype);
BABYLON.InputCollisionFilter.prototype.moveRelative = function (relativeMovement) {
var rotation = this.getOrientation();
BABYLON.Vector3.TransformNormalToRef(relativeMovement, this.getOrientationMatrix(), this._transformedDirection);
this.getPosition().addToRef(this._transformedDirection, this._tempNewPosition);
//this._tempNewPosition.y -= this._ellipsoid.y;
this._collider.radius = this._ellipsoid;
var p = this.getPosition();
this._positionBottom.x = p.x;
this._positionBottom.y = p.y;
this._positionBottom.z = p.z;
this._positionBottom.y += this._ellipsoid.y - this._cameraHeight;

this.scene._getNewPosition(this._positionBottom, this._transformedDirection, this._collider, 3, this._collidedPosition);


this._collidedPosition.subtractToRef(this._positionBottom, this._tempNewPosition2);
if (this._tempNewPosition2.length() > BABYLON.Engine.collisionsEpsilon * 5) {

BABYLON.Vector3.TransformNormalToRef(this._tempNewPosition2, this.getInvertOrientationMatrix(), this._tempNewPosition);
this.target.moveRelative(this._tempNewPosition);
}

};
})();
119 changes: 119 additions & 0 deletions Babylon/Cameras/Controllers/babylon.inputController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
"use strict";

var BABYLON = BABYLON || {};

(function () {

BABYLON.InputControllerTarget = function () {
this._position = new BABYLON.Vector3(0, 0, 0);
this._orientation = { yaw: 0.0, pitch: 0.0, roll: 0.0 };
};

BABYLON.InputControllerTarget.prototype.getPosition = function () {
return this._position;
};
BABYLON.InputControllerTarget.prototype.getOrientation = function () {
return this._orientation;
};
BABYLON.InputControllerTarget.prototype.moveRelative = function (movementVector) {

};

BABYLON.InputControllerTarget.prototype.rotateRelative = function (relativeOrientation) {

};
BABYLON.InputControllerTarget.prototype.getOrientationMatrix = function () { return new BABYLON.Matrix(); };
BABYLON.InputControllerTarget.prototype.getInvertOrientationMatrix = function () { return new BABYLON.Matrix(); };

BABYLON.InputControllerMultiTarget = function (targets) {
this.targets = targets;
var mainTarget = this.targets[0];
if (!mainTarget.controllers) {
mainTarget.controllers = [this];
} else {
mainTarget.controllers.push(this);
}
};

BABYLON.InputControllerMultiTarget.prototype.getPosition = function () {
return this.targets[0].getPosition();
};
BABYLON.InputControllerMultiTarget.prototype.getOrientation = function () {
return this.targets[0].getOrientation();
};


BABYLON.InputControllerMultiTarget.prototype.getOrientationMatrix = function () { return this.targets[0].getOrientationMatrix(); };
BABYLON.InputControllerMultiTarget.prototype.getInvertOrientationMatrix = function () { return this.targets[0].getInvertOrientationMatrix(); };

BABYLON.InputControllerMultiTarget.prototype.moveRelative = function (movementVector) {
for (var i = 0; i < this.targets.length; ++i) {
this.targets[i].moveRelative(movementVector);
}
};

BABYLON.InputControllerMultiTarget.prototype.rotateRelative = function (relativeOrientation) {
for (var i = 0; i < this.targets.length; ++i) {
this.targets[i].rotateRelative(relativeOrientation);
}
};

BABYLON.InputControllerMultiTarget.prototype.update = function () {
if (this.controllers) {
for (var i = 0; i < this.controllers.length; ++i) {
this.controllers[i].update();
}
}
};

BABYLON.InputController = function (scene, target) {
this.scene = scene;
this.target = target;
if (!this.target.controllers) {
this.target.controllers = [this];
} else {
this.target.controllers.push(this);
}
};
BABYLON.InputController.prototype.attachToCanvas = function (canvas) {

};
BABYLON.InputController.prototype.detachFromCanvas = function (canvas) {

};
BABYLON.InputController.prototype.update = function () {

};

BABYLON.InputController.prototype.dispose = function () {

};

BABYLON.inputFilter = function (scene, target) {
BABYLON.InputController.call(this, scene, target);
};
BABYLON.inputFilter.prototype = Object.create(BABYLON.InputController.prototype);
BABYLON.inputFilter.prototype.update = function () {
if (this.controllers) {
for (var i = 0; i < this.controllers.length; ++i) {
this.controllers[i].update();
}
}
};

BABYLON.inputFilter.prototype.getPosition = function () {
return this.target.getPosition();
};
BABYLON.inputFilter.prototype.getOrientation = function () {
return this.target.getOrientation();
};
BABYLON.inputFilter.prototype.getOrientationMatrix = function () { return this.target.getOrientationMatrix(); };
BABYLON.inputFilter.prototype.getInvertOrientationMatrix = function () { return this.target.getInvertOrientationMatrix(); };
BABYLON.inputFilter.prototype.moveRelative = function (movementVector) {
this.target.moveRelative(movementVector);
};

BABYLON.inputFilter.prototype.rotateRelative = function (relativeOrientation) {
this.target.rotateRelative(relativeOrientation);
};
})();
133 changes: 133 additions & 0 deletions Babylon/Cameras/Controllers/babylon.keyboardMoveController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
"use strict";

var BABYLON = BABYLON || {};

(function () {
BABYLON.KeyboardMoveController = function (scene, target) {
BABYLON.InputController.call(this, scene, target);
this._keys = [];
this.keysUp = [38];
this.keysDown = [40];
this.keysLeft = [37];
this.keysRight = [39];
this._currentSpeed = new BABYLON.Vector3(0, 0, 0);
this._lastFrameSpeed = new BABYLON.Vector3(0, 0, 0);
this._currentAcceleration = new BABYLON.Vector3(0, 0, 0);
this._tempSpeed = new BABYLON.Vector3(0, 0, 0);
this._tempSpeed2 = new BABYLON.Vector3(0, 0, 0);
this.maxAbsoluteSpeed = 2; // 2 meters per second
this.maxAbsoluteAcceleration = 5; // 2 meters per second²
this._targetSpeed = new BABYLON.Vector3(0, 0, 0);
};
BABYLON.KeyboardMoveController.prototype = Object.create(BABYLON.InputController.prototype);
BABYLON.KeyboardMoveController.prototype.attachToCanvas = function (canvas) {
var that = this;
this._canvas = canvas;

this._onKeyDown = function (evt) {
if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
that.keysDown.indexOf(evt.keyCode) !== -1 ||
that.keysLeft.indexOf(evt.keyCode) !== -1 ||
that.keysRight.indexOf(evt.keyCode) !== -1) {
var index = that._keys.indexOf(evt.keyCode);

if (index === -1) {
that._keys.push(evt.keyCode);
}
}
};

this._onKeyUp = function (evt) {
if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
that.keysDown.indexOf(evt.keyCode) !== -1 ||
that.keysLeft.indexOf(evt.keyCode) !== -1 ||
that.keysRight.indexOf(evt.keyCode) !== -1) {
var index = that._keys.indexOf(evt.keyCode);

if (index >= 0) {
that._keys.splice(index, 1);
}
}
};

this._onLostFocus = function () {
that._keys = [];
};

window.addEventListener("keydown", this._onKeyDown, false);
window.addEventListener("keyup", this._onKeyUp, false);
window.addEventListener("blur", this._onLostFocus, false);
};
BABYLON.KeyboardMoveController.prototype.detachFromCanvas = function (canvas) {
window.removeEventListener("keydown", this._onKeyDown, false);
window.removeEventListener("keyup", this._onKeyUp, false);
window.removeEventListener("blur", this._onLostFocus, false);
};
BABYLON.KeyboardMoveController.prototype.updateCurrentSpeed = function () {
this._lastFrameSpeed.x = this._currentSpeed.x;
this._lastFrameSpeed.y = this._currentSpeed.y;
this._lastFrameSpeed.z = this._currentSpeed.z;
if (this._currentSpeed.equals(this._targetSpeed)) {
this._currentAcceleration.x = 0;
this._currentAcceleration.y = 0;
this._currentAcceleration.z = 0;
return;
}
var dt = BABYLON.Tools.GetDeltaTime()/1000.0;

var dv = this._tempSpeed;
this._targetSpeed.subtractToRef(this._lastFrameSpeed, dv);
var absoluteAccToTarget = dv.length() / dt;
if (absoluteAccToTarget < this.maxAbsoluteAcceleration) {
this._currentSpeed.x = this._targetSpeed.x;
this._currentSpeed.y = this._targetSpeed.y;
this._currentSpeed.z = this._targetSpeed.z;
dv.normalize();
dv.scaleToRef(absoluteAccToTarget, this._currentAcceleration);
} else {
dv.normalize();
dv.scaleToRef(this.maxAbsoluteAcceleration, this._currentAcceleration);
dv.scaleInPlace(this.maxAbsoluteAcceleration * dt);

this._currentSpeed.addInPlace(dv);
}
};
BABYLON.KeyboardMoveController.prototype.update = function () {
this._targetSpeed.x = 0;
this._targetSpeed.y = 0;
this._targetSpeed.z = 0;
// update target speed from input
for (var index = 0; index < this._keys.length; index++) {
var keyCode = this._keys[index];
if (this.keysLeft.indexOf(keyCode) !== -1) {
this._targetSpeed.x -= 1;
} else if (this.keysUp.indexOf(keyCode) !== -1) {
this._targetSpeed.z += 1;
} else if (this.keysRight.indexOf(keyCode) !== -1) {
this._targetSpeed.x += 1;
} else if (this.keysDown.indexOf(keyCode) !== -1) {
this._targetSpeed.z -= 1;
}
}
if (this._targetSpeed.x != 0 || this._targetSpeed.z != 0) {
this._targetSpeed.normalize();
this._targetSpeed.scaleInPlace(this.maxAbsoluteSpeed);
}

this.updateCurrentSpeed();

if (this._lastFrameSpeed.x == 0 && this._lastFrameSpeed.z == 0 && this._currentAcceleration.x == 0 && this._currentAcceleration.z == 0) {
return;
}

// dv = (dt * v0) + 1/2 * dt² * a

var dt = BABYLON.Tools.GetDeltaTime() / 1000.0;
this._lastFrameSpeed.scaleToRef(dt, this._tempSpeed);
this._currentAcceleration.scaleToRef(dt * dt * 0.5, this._tempSpeed2);
this._tempSpeed.addInPlace(this._tempSpeed2);
if (this._tempSpeed.x != 0 || this._tempSpeed.z != 0) {
this.target.moveRelative(this._tempSpeed);
}
};
})();
Loading

0 comments on commit fed793e

Please sign in to comment.