Skip to content
Snippets Groups Projects
Commit a28da1b5 authored by Cedric.dosreis's avatar Cedric.dosreis
Browse files

collision detection

parent 18058a13
Branches
No related tags found
No related merge requests found
......@@ -2,12 +2,11 @@ class Enemy {
constructor(obj) {
this.object = obj;
this.direction = DIRECTION.NONE;
this.newDirection = DIRECTION.NONE;
}
move(path){
let { x, y } = MOVEMENT[this.direction];
//move
this.object.position.x += x;
this.object.position.y += y;
......@@ -29,24 +28,23 @@ class Enemy {
& (Math.abs(posY) % 1 < 0.05 | Math.abs(posY) % 1 > 0.95)) {
// postion to coordinates
let x = Math.round(posX + SIZE/2 - 0.5);
let y = (Math.round(posY - SIZE/2 - 0.5) * -1) -1;
let [x,y] = this.getCoordinates();
var directions = this.getPossibleDirections(x,y,path);
// 10% chance to change direction if 2 direction available
// 10% chance to change direction if 2 directions available
if (directions.length == 2 & Math.floor(Math.random()*10) == 0){
this.direction = directions[ Math.floor(Math.random()*directions.length) ];
this.changeDirection(directions, false);
}
// 25% chance to change direction if 3 directions available
if (directions.length == 3 & Math.floor(Math.random()*3) == 0){
this.direction = directions[ Math.floor(Math.random()*directions.length) ];
this.changeDirection(directions, false);
}
// 50% chance to change direction if 3 directions available
// 50% chance to change direction if 4 directions available
if (directions.length == 4 & Math.floor(Math.random()) == 0){
this.direction = directions[ Math.floor(Math.random()*directions.length) ];
this.changeDirection(directions,false);
}
......@@ -69,11 +67,9 @@ class Enemy {
nextBox = 0;
break;
}
if (nextBox == 0){
//change direction
this.direction = DIRECTION.NONE;
this.direction = directions[ Math.floor(Math.random()*directions.length) ];
if (nextBox == 0){
// a collision occured with a wall-> change direction (it can be opposite direction)
this.changeDirection(directions, true);
}
}
}
......@@ -94,11 +90,29 @@ class Enemy {
possibleDirections.push(DIRECTION.RIGHT);
}
// choose random direction
//this.direction = possibleDirection[ Math.floor(Math.random()*possibleDirection.length) ];
return possibleDirections;
}
getCoordinates(){
var posX = this.object.position.x;
var posY = this.object.position.y;
let x = Math.round(posX + SIZE/2 - 0.5);
let y = (Math.round(posY - SIZE/2 - 0.5) * -1) -1;
return [x,y];
}
changeDirection(directions, allowOpposite){
var newDirection ;
if (allowOpposite == false){ // the new direction must not be the opposite direction of the actual direction
do{
newDirection = directions[ Math.floor(Math.random()*directions.length) ];
}while( newDirection == OPPOSITE_DIRECTION[this.direction]);
}else{
newDirection = directions[ Math.floor(Math.random()*directions.length) ];
}
this.direction = newDirection;
}
getDirection(){
return this.direction;
}
......
......@@ -7,6 +7,14 @@ const DIRECTION = {
NONE: 'NONE'
};
const OPPOSITE_DIRECTION = {
UP: DIRECTION.DOWN,
DOWN: DIRECTION.UP,
RIGHT: DIRECTION.LEFT,
LEFT: DIRECTION.RIGHT,
NONE: DIRECTION.NONE
};
const SPEED = 0.05;
const MOVEMENT = {
LEFT: { x: -SPEED, y: 0 },
......@@ -42,8 +50,6 @@ const SIZE = 21
function main() {
mapGenerator = new MapGenerator();
//mapGenerator.generateMap("maps/map1.png");
// init scene
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x3f3f3f );
......@@ -55,8 +61,6 @@ function main() {
camera = new THREE.PerspectiveCamera(85, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 12;
camera.position.x = 0;
/*camera.position.y = -10;
camera.rotation.x = 0.5;*/
scene.add(camera);
// Modal management
......@@ -73,12 +77,11 @@ function main() {
}*/
//create objects
// laod map from image
[plane, walls, foods, playerObj, path, enemies_spawns] = mapGenerator.generateMap("map1", scene);
player = new Player(playerObj);
// spawn enemey
// spawn enemies
for (let i = 0; i < MAX_ENEMIES; i++) {
spawnEnemy();
}
......@@ -87,14 +90,14 @@ function main() {
//getListofMaps();
function render(){
//checkCollision();
player.move(path);
enemies.forEach(enemy => {
enemy.move(path);
});
//checkCollision();
checkCollision();
checkCollisionFood();
requestAnimationFrame(render);
renderer.render(scene, camera);
......@@ -114,46 +117,20 @@ function spawnEnemy(){
enemies.push(new Enemy(enemy));
}
// check player collision with food/specail food and enemies
function checkCollision() {
let[playerX, playerY] = player.getCoordinates();
enemies.forEach(enemy => {
let[enemyX, enemyY] = enemy.getCoordinates();
var playerObj = scene.getObjectByName("Player");
var originPoint = playerObj.position.clone();
var originPointFastForward = originPoint;
let {x,y} = MOVEMENT[player.getDirection()];
originPointFastForward.x += x;
originPointFastForward.y += y;
for (var vertexIndex = 0; vertexIndex < playerObj.geometry.vertices.length; vertexIndex++) {
var localVertex = playerObj.geometry.vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4(playerObj.matrix);
var directionVector = globalVertex.sub(playerObj.position);
// check collision with walls
/*var ray = new THREE.Raycaster(originPointFastForward, directionVector.clone().normalize());
var collisionResults = ray.intersectObjects(walls);
if (collisionResults.length > 0 && collisionResults[0].distance < directionVector.length()) {
player.setDirection(DIRECTION.NONE);
}*/
ray = new THREE.Raycaster(originPoint, directionVector.clone().normalize());
collisionResults = ray.intersectObjects(enemies.getObject());
if (collisionResults.length > 0 && collisionResults[0].distance < directionVector.length()) {
alert('Game Over');
player.setDirection(DIRECTION.NONE);
/*collisionResults[0].object.material.transparent = true;
collisionResults[0].object.material.opacity = 0.4;*/
//console.log(playerX + ' ' + playerY + ' ' + enemyX + ' ' + enemyY);
if (enemyX == playerX & enemyY == playerY){
alert("GameOver");
}
collisionResults = ray.intersectObjects(foods);
if (collisionResults.length > 0 && collisionResults[0].distance < directionVector.length()) {
console.log("Miam");
score++;
}
}
});
}
window.addEventListener("keydown", function (event) {
//change player direction
......@@ -170,6 +147,49 @@ window.addEventListener("keydown", function (event) {
});
// check player collision with food/special food and enemies
function checkCollisionFood() {
var playerObj = player.getObject();
var originPoint = playerObj.position.clone();
/*var originPointFastForward = originPoint;
let {x,y} = MOVEMENT[player.getDirection()];
originPointFastForward.x += x;
originPointFastForward.y += y; */
for (var vertexIndex = 0; vertexIndex < playerObj.geometry.vertices.length; vertexIndex++) {
var localVertex = playerObj.geometry.vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4(playerObj.matrix);
var directionVector = globalVertex.sub(playerObj.position);
var ray = new THREE.Raycaster(originPoint, directionVector.clone().normalize());
collisionResults = ray.intersectObjects(foods);
if (collisionResults.length > 0 && collisionResults[0].distance < directionVector.length()) {
console.log("Miam " + collisionResults[0].object.name);
score++;
}
/*check collision with enemies
ray = new THREE.Raycaster(originPoint, directionVector.clone().normalize());
//collisionResults = ray.intersectObjects(enemies.map(o => o.getObject()));
//console.log(enemies.map(o => o.getObject()));
if (collisionResults.length > 0 && collisionResults[0].distance < directionVector.length()) {
console.log('Game Over ' + collisionResults.length + ' ' );
//player.setDirection(DIRECTION.NONE);
}*/
/*// check collision with walls
var ray = new THREE.Raycaster(originPointFastForward, directionVector.clone().normalize());
var collisionResults = ray.intersectObjects(walls);
if (collisionResults.length > 0 && collisionResults[0].distance < directionVector.length()) {
player.setDirection(DIRECTION.NONE);
}*/
}
}
// ask for all maps image filename et absolute path of the file
/*function getListofMaps(){
// send synchronous request
var request = new XMLHttpRequest();
......
......@@ -13,7 +13,7 @@ class MapGenerator {
var walls = [];
var foods = [];
var path = [];
var enemies_spawn = [];
var enemies_spawn_zones = [];
var player;
var map_content = [];
......@@ -89,6 +89,7 @@ class MapGenerator {
var geometry = new THREE.SphereGeometry(0.1);
var material = new THREE.MeshBasicMaterial( {color: 0xffffff} );
var food = new THREE.Mesh( geometry, material );
food.name = "food" + foods.length;
food.position.x = -x + (SIZE - 1)/2;
food.position.y = -y + (SIZE - 1)/2;
......@@ -103,6 +104,7 @@ class MapGenerator {
var geometry = new THREE.SphereGeometry(0.2);
var material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
var food = new THREE.Mesh( geometry, material );
food.name = "s_food" + foods.length;
food.position.x = -x + (SIZE - 1)/2;
food.position.y = -y + (SIZE - 1)/2;
scene.add(food);
......@@ -117,7 +119,7 @@ class MapGenerator {
case "SPAWN_E":
var spawnX = (-x + (SIZE - 1)/2);
var spawnY = (-y + (SIZE - 1)/2);
enemies_spawn.push({"x":spawnX, "y": spawnY});
enemies_spawn_zones.push({"x":spawnX, "y": spawnY});
path[y].push(1);
break;
......@@ -142,6 +144,6 @@ class MapGenerator {
}
}
return [plane, walls, foods, player, path, enemies_spawn];
return [plane, walls, foods, player, path, enemies_spawn_zones];
}
}
\ No newline at end of file
......@@ -33,12 +33,11 @@ class Player {
// postion to coordinates
let x = Math.round(posX + SIZE/2 - 0.5);
let y = (Math.round(posY - SIZE/2 - 0.5) * -1) -1;
let [x,y ] = this.getCoordinates();
// check if direction can be changed according to path
if (this.newDirection != this.direction){
var nextBox;
var nextBox = -1;
// check if a path is available (not a wall) in the new direction
switch(this.newDirection){
case DIRECTION.DOWN:
......@@ -51,7 +50,10 @@ class Player {
nextBox = path[y][(x-1)%SIZE];
break;
case DIRECTION.RIGHT:
nextBox = path[y][(x+1)%SIZE]
nextBox = path[y][(x+1)%SIZE];
break;
case DIRECTION.NONE:
this.direction = this.newDirection;
break;
}
......@@ -81,15 +83,7 @@ class Player {
//change direction
this.direction = DIRECTION.NONE;
}
}
//console.log (this.object.position.x + " " + this.object.position.y);
}
setDirection(dir){
......@@ -98,8 +92,21 @@ class Player {
//this.direction = dir;
}
getObject(){
return this.object;
}
getDirection(){
return this.direction;
}
// position to coordinate in map array
getCoordinates(){
var posX = this.object.position.x;
var posY = this.object.position.y;
let x = Math.round(posX + SIZE/2 - 0.5);
let y = (Math.round(posY - SIZE/2 - 0.5) * -1) -1;
return [x,y];
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment