From 256654502efbde2b0aa3b0f9310773f9c9f08c67 Mon Sep 17 00:00:00 2001 From: LJ5O <75009579+LJ5O@users.noreply.github.com> Date: Fri, 17 Nov 2023 10:42:14 +0100 Subject: [PATCH] Added collisions with objects on scene Player will now be stopped by fences Also added the possibility to define the size of ground Also edited speed of animations Also optimized the loading of fences --- src/main.js | 2 +- src/objects/ground.js | 4 +- src/utils/addAssetsOnScene.js | 76 +++++++++++++++++------------------ src/utils/animateMesh.js | 2 +- src/utils/playerMovement.js | 26 +++++++++++- 5 files changed, 64 insertions(+), 46 deletions(-) diff --git a/src/main.js b/src/main.js index 9ba4472..66cf3d6 100644 --- a/src/main.js +++ b/src/main.js @@ -38,7 +38,7 @@ Animations.addAnimationMixerOnMesh(assets.player, ["Walk"]); function animate() { requestAnimationFrame( animate ); - PlayerMovement.updatePlayerPositionAnimation(assets.player, camera);//Updating player position and rotation + PlayerMovement.updatePlayerPositionAnimation(assets.player, camera, scene);//Updating player position and rotation Animations.updateAnimations(Clock.getDelta());//Playing animations renderer.render( scene, camera );//Calculating and redering new frame diff --git a/src/objects/ground.js b/src/objects/ground.js index b5287f8..112a279 100644 --- a/src/objects/ground.js +++ b/src/objects/ground.js @@ -1,8 +1,8 @@ //ground import * as THREE from 'three'; -export function createGround() { - const geometry = new THREE.PlaneGeometry(10, 10); +export function createGround(height, width) { + const geometry = new THREE.PlaneGeometry(height, width); const material = new THREE.MeshBasicMaterial({ color: 0xa3c973 }); const plane = new THREE.Mesh(geometry, material); diff --git a/src/utils/addAssetsOnScene.js b/src/utils/addAssetsOnScene.js index 6a5dec1..19f51ea 100644 --- a/src/utils/addAssetsOnScene.js +++ b/src/utils/addAssetsOnScene.js @@ -26,65 +26,60 @@ export async function addAssetsOnScene(scene){ scene.add(new THREE.AmbientLight(0x404040, 50));//Light, required to see - const ground = createGround(); - scene.add(ground); - /* -------------------- - ADDING FENCES + ADDING GROUND AND FENCES -------------------- */ - const groundHitBox = new THREE.Box3().setFromObject(ground); // Calculating ground size - const groundXSize = groundHitBox.max.x - groundHitBox.min.x; - const groundySize = groundHitBox.max.y - groundHitBox.min.y; - let fences = [];//List of created fences const fenceModel = await loadModel('src/objects/models/fence.gltf'); //Getting width of one fence fenceModel.scene.scale.set(0.05, 0.05, 0.05); - fenceModel.scene.rotateOnWorldAxis(new THREE.Vector3(1, 0, 0), MathUtils.degToRad(90)); + fenceModel.scene.rotateOnWorldAxis(new THREE.Vector3(1, 0, 0), MathUtils.degToRad(90));//Must be standing up const fenceHitBox = new THREE.Box3().setFromObject(fenceModel.scene); const fenceWidth = fenceHitBox.max.y - fenceHitBox.min.y; + //Creating the ground + const ground = createGround(6*fenceWidth, 6*fenceWidth); + scene.add(ground); + const groundHitBox = new THREE.Box3().setFromObject(ground); // Calculating ground size + const groundXSize = groundHitBox.max.x - groundHitBox.min.x; + const groundySize = groundHitBox.max.y - groundHitBox.min.y; + for(let i = 0; icollisionThreshold && intersects[i].distance 0) {//If a movement key is pressed ( or something in playerDirection ) if(!player.animationActions.Walk.isRunning()) player.animationActions.Walk.play();//Playing walk animation