Game
- Starting solo game with physics
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/28 15:58:03 by edbernar #+# #+# */
|
||||
/* Updated: 2024/08/29 00:44:26 by edbernar ### ########.fr */
|
||||
/* Updated: 2024/08/29 13:45:02 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,12 +23,10 @@ class Ball
|
||||
{
|
||||
static create(scene)
|
||||
{
|
||||
|
||||
ball = createBall();
|
||||
|
||||
scene.add(ball);
|
||||
ball.rotateY(0.8);
|
||||
|
||||
ball.rotateY(2.39);
|
||||
}
|
||||
|
||||
static dispose()
|
||||
@ -36,36 +34,10 @@ class Ball
|
||||
ball = null;
|
||||
}
|
||||
|
||||
static moveBall()
|
||||
static update()
|
||||
{
|
||||
createInterval();
|
||||
}
|
||||
|
||||
|
||||
static stopBall()
|
||||
{
|
||||
if (interval)
|
||||
clearInterval(interval);
|
||||
}
|
||||
}
|
||||
|
||||
function createInterval()
|
||||
{
|
||||
interval = setInterval(() => {
|
||||
console.log(ball.position);
|
||||
moveForward();
|
||||
bounceWallTop();
|
||||
bounceWallTBottom();
|
||||
bouncePlayer1();
|
||||
}, 16);
|
||||
}
|
||||
|
||||
function moveForward()
|
||||
{
|
||||
const direction = new THREE.Vector3(0, 0, dir);
|
||||
direction.applyQuaternion(ball.quaternion);
|
||||
|
||||
ball.position.add(direction.multiplyScalar(speed));
|
||||
}
|
||||
|
||||
function createBall()
|
||||
@ -81,55 +53,4 @@ function createBall()
|
||||
return (mesh);
|
||||
}
|
||||
|
||||
function bounceWallTop()
|
||||
{
|
||||
const origin = new THREE.Vector3(ball.position.x, ball.position.y, ball.position.z);
|
||||
const direction = new THREE.Vector3(ball.position.x, ball.position.y, ball.position.z - 1);
|
||||
|
||||
direction.normalize();
|
||||
const raycaster = new THREE.Raycaster(origin, direction);
|
||||
const objects = [ wallTop ];
|
||||
const intersects = raycaster.intersectObjects(objects);
|
||||
|
||||
if (intersects.length > 0)
|
||||
{
|
||||
if (intersects[0].distance <= 0.5)
|
||||
ball.rotation.y = Math.PI - ball.rotation.y
|
||||
}
|
||||
}
|
||||
|
||||
function bounceWallTBottom()
|
||||
{
|
||||
const origin = new THREE.Vector3(ball.position.x, ball.position.y, ball.position.z);
|
||||
const direction = new THREE.Vector3(ball.position.x, ball.position.y, ball.position.z + 1);
|
||||
|
||||
direction.normalize();
|
||||
const raycaster = new THREE.Raycaster(origin, direction);
|
||||
const objects = [ wallBottom ];
|
||||
const intersects = raycaster.intersectObjects(objects);
|
||||
|
||||
if (intersects.length > 0)
|
||||
{
|
||||
if (intersects[0].distance <= 0.4)
|
||||
ball.rotation.y = Math.PI - ball.rotation.y;
|
||||
}
|
||||
}
|
||||
|
||||
function bouncePlayer1()
|
||||
{
|
||||
const origin = new THREE.Vector3(ball.position.x, ball.position.y, ball.position.z);
|
||||
const direction = new THREE.Vector3(ball.position.x - 1, ball.position.y, ball.position.z);
|
||||
|
||||
direction.normalize();
|
||||
const raycaster = new THREE.Raycaster(origin, direction);
|
||||
const objects = [ player1 ];
|
||||
const intersects = raycaster.intersectObjects(objects);
|
||||
|
||||
if (intersects.length > 0)
|
||||
{
|
||||
if (intersects[0].distance <= 0.4)
|
||||
ball.rotation.y = Math.PI - ball.rotation.y;
|
||||
}
|
||||
}
|
||||
|
||||
export { Ball };
|
||||
export { Ball, ball };
|
@ -6,23 +6,35 @@
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/28 12:23:48 by edbernar #+# #+# */
|
||||
/* Updated: 2024/08/28 17:01:17 by edbernar ### ########.fr */
|
||||
/* Updated: 2024/08/29 18:42:55 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
import * as CANNON from 'cannon-es';
|
||||
import * as THREE from 'three';
|
||||
import { ball } from './Ball.js'
|
||||
|
||||
const width = 25;
|
||||
const height = 12.5;
|
||||
let spotLight = null;
|
||||
let wallTop = null;
|
||||
let wallBottom = null;
|
||||
const width = 25;
|
||||
const height = 12.5;
|
||||
let ground = null;
|
||||
let spotLight = null;
|
||||
let wallTop = null;
|
||||
let wallBottom = null;
|
||||
let world = null;
|
||||
const timeStep = 1 / 60;
|
||||
let groundBody = null;
|
||||
let wallTopBody = null;
|
||||
let wallBottomBody = null;
|
||||
let ballBody = null;
|
||||
|
||||
class Map
|
||||
{
|
||||
static create(scene)
|
||||
{
|
||||
createGround(scene);
|
||||
world = new CANNON.World({
|
||||
gravity: new CANNON.Vec3(0, -9.81, 0),
|
||||
});
|
||||
ground = createGround(scene);
|
||||
wallBottom = createWall(false);
|
||||
wallTop = createWall(true);
|
||||
spotLight = new THREE.SpotLight({color: 0xffffff});
|
||||
@ -33,6 +45,33 @@ class Map
|
||||
scene.add(wallTop);
|
||||
scene.add(wallBottom);
|
||||
scene.add(new THREE.AmbientLight(0xffffff, 0.5));
|
||||
wallTopBody = new CANNON.Body({
|
||||
shape: new CANNON.Box(new CANNON.Vec3(width, 0.7, 0.2)),
|
||||
type: CANNON.Body.STATIC,
|
||||
})
|
||||
wallTopBody.position.z = -6.15;
|
||||
wallTopBody.position.y += 0.35;
|
||||
world.addBody(wallTopBody);
|
||||
wallBottomBody = new CANNON.Body({
|
||||
shape: new CANNON.Box(new CANNON.Vec3(width, 0.7, 0.2)),
|
||||
type: CANNON.Body.STATIC,
|
||||
})
|
||||
wallBottomBody.position.z = 6.15;
|
||||
wallBottomBody.position.y += 0.35;
|
||||
world.addBody(wallBottomBody);
|
||||
groundBody = new CANNON.Body({
|
||||
shape: new CANNON.Plane(),
|
||||
type: CANNON.Body.STATIC,
|
||||
});
|
||||
groundBody.quaternion.setFromEuler(-Math.PI / 2, 0, 0);
|
||||
world.addBody(groundBody);
|
||||
ballBody = new CANNON.Body({
|
||||
shape: new CANNON.Sphere(0.3),
|
||||
mass: 1
|
||||
});
|
||||
ballBody.position.set(0, 2, 0);
|
||||
ballBody.velocity.set(0, 0, 0);
|
||||
world.addBody(ballBody);
|
||||
}
|
||||
|
||||
static dispose()
|
||||
@ -41,6 +80,19 @@ class Map
|
||||
spotLight.dispose();
|
||||
spotLight = null;
|
||||
}
|
||||
|
||||
static update()
|
||||
{
|
||||
world.step(timeStep);
|
||||
ground.position.copy(groundBody.position);
|
||||
ground.quaternion.copy(groundBody.quaternion);
|
||||
ball.position.copy(ballBody.position);
|
||||
ball.quaternion.copy(ballBody.quaternion);
|
||||
wallBottom.position.copy(wallBottomBody.position);
|
||||
wallBottom.quaternion.copy(wallBottomBody.quaternion);
|
||||
wallTop.position.copy(wallTopBody.position);
|
||||
wallTop.quaternion.copy(wallTopBody.quaternion);
|
||||
}
|
||||
}
|
||||
|
||||
function createGround(scene)
|
||||
@ -52,6 +104,7 @@ function createGround(scene)
|
||||
mesh.rotateX(-Math.PI / 2);
|
||||
mesh.position.set(0, 0, 0);
|
||||
scene.add(mesh);
|
||||
return (mesh);
|
||||
}
|
||||
|
||||
function createWall(onTop)
|
||||
@ -60,11 +113,11 @@ function createWall(onTop)
|
||||
const material = new THREE.MeshPhysicalMaterial({color: 0x333333});
|
||||
const mesh = new THREE.Mesh(geometry, material);
|
||||
|
||||
if (onTop)
|
||||
mesh.position.z = -6.15;
|
||||
else
|
||||
mesh.position.z = 6.15;
|
||||
mesh.position.y += 0.35;
|
||||
// if (onTop)
|
||||
// mesh.position.z = -6.15;
|
||||
// else
|
||||
// mesh.position.z = 6.15;
|
||||
// mesh.position.y += 0.35;
|
||||
return (mesh);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user