Absolutely, it can be done.
To start, you need to check if a point or circle is positioned behind the player. This involves calculating the dot product between the unit vector representing the direction the player is facing and the unit vector pointing to the circle. If the dot product value is less than or equal to 0, then the circle is considered to be behind the player.
(A unit vector is a vector with a magnitude of one, ensuring its x/y/z components do not exceed 1)
Here's a code example:
// Assume this code is inside a loop
const directionVect = circle.position.clone().sub(player.position).normalize();
const playerFacing = player.getWorldDirection(new THREE.Vector3());
if (playerFacing.dot(directionVect) <= 0) {
// Circle is behind the player
// ...continue handling logic here...
} else {
return;
}
Once you have identified the circles behind the player, you can further determine which circles are within a specified cone angle. This involves calculating the angle between the player's position and the circle's position, then checking if it meets certain criteria (e.g., angle less than 45 degrees from the player's back).
// ...
if (playerFacing.dot(directionVect) <= 0) {
// Circle is behind the player
const angle = player.position.angleTo(circle.position);
if (angle < Math.PI * 0.25) {
// Perform actions with circle
}
} else {
return;
}