Enhanced collisions system

Players can't enter in trees or go through signs anymore. We're now using two rays instead of one, and reduced the threshold distance
This commit is contained in:
2024-01-15 20:40:31 +01:00
parent 8d948d0874
commit df7df3e6b7

View File

@@ -52,21 +52,38 @@ export function handleKeyDown(event) {
Takes player, camera and scene*/ Takes player, camera and scene*/
//COLLIDERS //COLLIDERS
const raycaster = new THREE.Raycaster();//Creating a raycaster from the player
const rayDirection = new THREE.Vector3(playerDirection.x, playerDirection.y, 0).normalize(); const rayDirection = new THREE.Vector3(playerDirection.x, playerDirection.y, 0).normalize();
raycaster.set(player.scene.position, rayDirection);
const intersects = raycaster.intersectObjects(scene.children, true);//Gettings objects on the path of this raycaster //Two rays are created : one for legs, one for torso/head
const raycaster = new THREE.Raycaster();
const rayPosition = player.scene.position.clone();
rayPosition.z = 0.2;//Adjusted by hand, is enough to collide with nearly everything
raycaster.set(rayPosition, rayDirection);
const raycaster2 = new THREE.Raycaster();
const rayPosition2 = player.scene.position.clone();
rayPosition2.z = 0.9;
raycaster2.set(rayPosition2, rayDirection);
const intersects = [...raycaster.intersectObjects(scene.children, true), ...raycaster2.intersectObjects(scene.children, true)];//Merging the two lists together, so we get all objects collided by ray1 or 2
;
const collidingDistance = 0.5; const collidingDistance = 0.5;
const collisionThreshold = 0.2; const collisionThreshold = 0.01;
for(let i=0; i<intersects.length; i++){
//if(intersects[i].distance<2 && intersects[i].distance>collisionThreshold)console.log(intersects[i]); for (let i = 0; i < intersects.length; i++) {
if(intersects[i].distance>collisionThreshold && intersects[i].distance<collidingDistance){ const collidedObject = intersects[i].object;
//Valid colliding object, not too close ( player leg / arm ), but not too far
playerDirection.set(0, 0, 0); // Check if the collided object is not the player itself
break; if (!player.scene.children.includes(collidedObject)) {
} if (intersects[i].distance > collisionThreshold && intersects[i].distance < collidingDistance) {
// Valid colliding object, not too close (player leg/arm), but not too far
/*console.log(collidedObject);
console.log(intersects[i].distance);*/
playerDirection.set(0, 0, 0);
break;
}
}
} }
//MOVING //MOVING