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);

12
prototype/js/main.js vendored
View File

@ -16,6 +16,9 @@ init();
animate();
function init() {
// Collidable objects to prevent camera from traversing objects
var collidableObjects = new Array();
// Add the listener on the button
document.getElementById('reset').onclick = function() { cameras.mainCamera().reset(); };
var fullarrow = document.getElementById('fullarrow');
@ -91,6 +94,7 @@ function init() {
function ( object ) {
object.up = new THREE.Vector3(0,0,1);
scene.add(object);
collidableObjects.push(object);
object.traverse(function (object) {
if (object instanceof THREE.Mesh) {
object.geometry.mergeVertices();
@ -115,6 +119,7 @@ function init() {
object.up = new THREE.Vector3(0,0,1);
scene.add(object);
collidableObjects.push(object);
object.traverse(function (object) {
if (object instanceof THREE.Mesh) {
object.material.side = THREE.DoubleSide;
@ -184,6 +189,8 @@ function init() {
container.addEventListener('mousedown', click, false);
camera1.collidableObjects = collidableObjects;
// Load the scene
loadScene();
@ -214,6 +221,7 @@ function animate() {
requestAnimationFrame(animate);
stats.begin();
cameras.updateMainCamera();
cameras.update(cameras.mainCamera());
cameras.look();
@ -277,7 +285,7 @@ function click(event) {
if (cameras.getById(intersects[bestIndex].object.parent.id) !== undefined) {
var new_camera = cameras.getById(intersects[bestIndex].object.parent.id);
// hide(new_camera);
cameras.get(0).move(new_camera);
cameras.mainCamera().move(new_camera);
}
}
@ -285,7 +293,7 @@ function click(event) {
for (o in objects) {
if ( intersects[bestIndex].object.id == objects[o].id && cameras.get(objects[o].seen_by[0]) !== undefined) {
var new_camera = cameras.get(objects[o].seen_by[0]);
cameras.get(0).move(new_camera);
cameras.mainCamera().move(new_camera);
break;
}
}