Game - Push

This commit is contained in:
hubourge
2024-08-30 16:51:28 +02:00
parent 820715f920
commit 69272ae91c
3 changed files with 57 additions and 15 deletions

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* SoloGame.js :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/28 12:07:39 by edbernar #+# #+# */
/* Updated: 2024/08/29 13:58:06 by edbernar ### ########.fr */
/* Updated: 2024/08/30 15:52:41 by hubourge ### ########.fr */
/* */
/* ************************************************************************** */
@ -42,7 +42,7 @@ class SoloGame
Players.create(scene);
controls = new OrbitControls(camera, renderer.domElement);
camera.position.set(0, 20, 0);
camera.position.set(0, 30, 0);
// camera.position.set(20, 5, 20);
renderer.setAnimationLoop(loop);
}
@ -82,4 +82,4 @@ function loop()
stats.end();
}
export { SoloGame };
export { SoloGame };

View File

@ -3,16 +3,15 @@
/* ::: :::::::: */
/* Ball.js :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/28 15:58:03 by edbernar #+# #+# */
/* Updated: 2024/08/29 13:45:02 by edbernar ### ########.fr */
/* Updated: 2024/08/30 16:01:52 by hubourge ### ########.fr */
/* */
/* ************************************************************************** */
import * as THREE from 'three';
import { wallTop, wallBottom } from './Map.js';
import { player1, player2 } from './Players.js';
let ball = null;
let speed = 0.15;
@ -26,7 +25,7 @@ class Ball
ball = createBall();
scene.add(ball);
ball.rotateY(2.39);
ball.rotateY(2.39);
}
static dispose()
@ -53,4 +52,4 @@ function createBall()
return (mesh);
}
export { Ball, ball };
export { Ball, ball };

View File

@ -3,16 +3,17 @@
/* ::: :::::::: */
/* Map.js :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/28 12:23:48 by edbernar #+# #+# */
/* Updated: 2024/08/29 18:42:55 by edbernar ### ########.fr */
/* Updated: 2024/08/30 16:49:53 by hubourge ### ########.fr */
/* */
/* ************************************************************************** */
import * as CANNON from 'cannon-es';
import * as THREE from 'three';
import { ball } from './Ball.js'
import { player1, player2 } from './Players.js';
const width = 25;
const height = 12.5;
@ -26,9 +27,14 @@ let groundBody = null;
let wallTopBody = null;
let wallBottomBody = null;
let ballBody = null;
let vec3 = {x:0.1, y:0, z:3};
let player1Body = null;
let player2Body = null;
let wallBottomId = -1;
let wallTopId = -1;
class Map
{
static create(scene)
{
world = new CANNON.World({
@ -52,6 +58,8 @@ class Map
wallTopBody.position.z = -6.15;
wallTopBody.position.y += 0.35;
world.addBody(wallTopBody);
wallTopId = wallTopBody.id;
wallBottomBody = new CANNON.Body({
shape: new CANNON.Box(new CANNON.Vec3(width, 0.7, 0.2)),
type: CANNON.Body.STATIC,
@ -59,19 +67,47 @@ class Map
wallBottomBody.position.z = 6.15;
wallBottomBody.position.y += 0.35;
world.addBody(wallBottomBody);
wallBottomId = wallBottomBody.id;
groundBody = new CANNON.Body({
shape: new CANNON.Plane(),
type: CANNON.Body.STATIC,
});
groundBody.quaternion.setFromEuler(-Math.PI / 2, 0, 0);
world.addBody(groundBody);
player1Body = new CANNON.Body({
shape: new CANNON.Box(new CANNON.Vec3(0.3, 0.4, 2.5)),
type: CANNON.Body.STATIC,
});
player1Body.position.set(-12, 0.4, 0);
world.addBody(player1Body);
player2Body = new CANNON.Body({
shape: new CANNON.Box(new CANNON.Vec3(0.3, 0.4, 2.5)),
type: CANNON.Body.STATIC,
});
player2Body.position.set(12, 0.4, 0);
world.addBody(player2Body);
ballBody = new CANNON.Body({
shape: new CANNON.Sphere(0.3),
mass: 1
});
ballBody.position.set(0, 2, 0);
ballBody.velocity.set(0, 0, 0);
ballBody.position.set(0, 0.15, 0);
ballBody.velocity.set(vec3.x, vec3.y, vec3.z);
world.addBody(ballBody);
ballBody.addEventListener("collide",function(e){
var relativeVelocity = e.contact.getImpactVelocityAlongNormal();
console.log("e.contact : ", e.contact);
console.log("id: ", e.contact.id );
console.log("player1Body.id: ", player1Body.id );
if (e.contact.id == player1Body.id || e.contact.id == player2Body.id)
vec3.x = -vec3.x;
if (e.contact.id == wallBottomId || e.contact.id == wallTopId)
vec3.z = -vec3.z;
});
}
static dispose()
@ -84,6 +120,7 @@ class Map
static update()
{
world.step(timeStep);
// Deplacer les update des objet fixe dans le constructeur pour ne pas update a chaque frame
ground.position.copy(groundBody.position);
ground.quaternion.copy(groundBody.quaternion);
ball.position.copy(ballBody.position);
@ -92,6 +129,12 @@ class Map
wallBottom.quaternion.copy(wallBottomBody.quaternion);
wallTop.position.copy(wallTopBody.position);
wallTop.quaternion.copy(wallTopBody.quaternion);
////////
ballBody.velocity.set(vec3.x, vec3.y, vec3.z);
player1.position.copy(player1Body.position);
player1.quaternion.copy(player1Body.quaternion);
player2.position.copy(player2Body.position);
player2.quaternion.copy(player2Body.quaternion);
}
}
@ -121,4 +164,4 @@ function createWall(onTop)
return (mesh);
}
export { Map, wallBottom, wallTop };
export { Map, wallBottom, wallTop };