Collisions

This commit is contained in:
Thomas FORGIONE
2015-04-22 10:17:54 +02:00
parent fd2cc2cddf
commit d3bf9db542
4 changed files with 54 additions and 13 deletions

View File

@@ -1,11 +1,10 @@
var CameraContainer = function () {
this.current_camera = 0;
this.cameras = new Array();
}
CameraContainer.prototype.mainCamera = function(id) {
if (id === undefined) {
return this.cameras[this.current_camera];
return this.pointerCamera;
}
if (id >= cameras.length || id < 0) {
console.log('Warning : this camera does not exist');
@@ -20,7 +19,11 @@ CameraContainer.prototype.forEach = function(callback) {
}
CameraContainer.prototype.look = function() {
this.cameras[this.current_camera].look();
this.mainCamera().look();
}
CameraContainer.prototype.updateMainCamera = function() {
this.pointerCamera.update();
}
CameraContainer.prototype.update = function(position) {
@@ -28,7 +31,10 @@ CameraContainer.prototype.update = function(position) {
}
CameraContainer.prototype.push = function(camera) {
this.cameras.push(camera);
this.pointerCamera = camera;
this.push = function(camera) {
this.cameras.push(camera);
};
}
CameraContainer.prototype.get = function(i) {

View File

@@ -166,8 +166,9 @@ FixedCamera.prototype.regenerateArrow = function(mainCamera) {
up.multiplyScalar(-1);
up.normalize();
var left = Tools.cross(up, deriv); left.normalize(); left.multiplyScalar(0.1);
var other = Tools.cross(deriv, left); other.normalize(); other.multiplyScalar(0.1);
var coeff = 0.1*i;
var left = Tools.cross(up, deriv); left.normalize(); left.multiplyScalar(coeff);
var other = Tools.cross(deriv, left); other.normalize(); other.multiplyScalar(coeff);
vertices.push(
Tools.sum(Tools.sum(point, left), other),

View File

@@ -35,6 +35,9 @@ var PointerCamera = function() {
this.sensitivity = 0.05;
this.speed = 1;
// Raycaster for collisions
this.raycaster = new THREE.Raycaster();
// Set events from the document
var self = this;
var onKeyDown = function(event) {self.onKeyDown(event);};
@@ -124,13 +127,23 @@ PointerCamera.prototype.update = function() {
left.normalize();
left.multiplyScalar(400.0 * delta);
// Move only if no collisions
var speed = this.speed;
if (this.boost) speed *= 10;
if (this.moveForward) this.position.add(Tools.mul(forward, speed));
if (this.moveBackward) this.position.sub(Tools.mul(forward, speed));
if (this.moveLeft) this.position.add(Tools.mul(left, speed));
if (this.moveRight) this.position.sub(Tools.mul(left, speed));
var direction = new THREE.Vector3();
if (this.boost) speed *= 10;
if (this.moveForward) direction.add(Tools.mul(forward, speed));
if (this.moveBackward) direction.sub(Tools.mul(forward, speed));
if (this.moveLeft) direction.add(Tools.mul(left, speed));
if (this.moveRight) direction.sub(Tools.mul(left, speed));
if (!this.isColliding(direction)) {
this.position.add(direction);
} else {
this.position.sub(direction);
}
// Update angle
this.target = this.position.clone();
this.target.add(forward);
}
@@ -176,6 +189,19 @@ PointerCamera.prototype.move = function(otherCamera) {
this.t = 0;
}
PointerCamera.prototype.isColliding = function(direction) {
this.raycaster.set(this.position, direction.clone().normalize());
var intersects = this.raycaster.intersectObjects(this.collidableObjects, true);
for (var i in intersects) {
if (intersects[i].distance < 0.1) {
return true;
}
}
return false;
}
// Look function
PointerCamera.prototype.look = function() {
this.lookAt(this.target);