From d3bf9db542697b5321cde4f334d786a66d71eaef Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Wed, 22 Apr 2015 10:17:54 +0200 Subject: [PATCH] Collisions --- js/CameraContainer.js | 14 ++++++++++---- js/FixedCamera.js | 5 +++-- js/PointerCamera.js | 36 +++++++++++++++++++++++++++++++----- prototype/js/main.js | 12 ++++++++++-- 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/js/CameraContainer.js b/js/CameraContainer.js index c6f2fb8..c5534b0 100644 --- a/js/CameraContainer.js +++ b/js/CameraContainer.js @@ -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) { diff --git a/js/FixedCamera.js b/js/FixedCamera.js index b00b65c..0ccf87e 100644 --- a/js/FixedCamera.js +++ b/js/FixedCamera.js @@ -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), diff --git a/js/PointerCamera.js b/js/PointerCamera.js index ae52a94..1fc92f4 100644 --- a/js/PointerCamera.js +++ b/js/PointerCamera.js @@ -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); diff --git a/prototype/js/main.js b/prototype/js/main.js index 14b3efb..c590972 100644 --- a/prototype/js/main.js +++ b/prototype/js/main.js @@ -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; } }