visible method
Performs a line of sight test in order to determine if the given point in 3D space is visible for the game entity.
Implementation
bool visible(Vector3 point ) {
final owner = this.owner;
final obstacles = this.obstacles;
owner?.getWorldPosition( worldPosition );
// check if point lies within the game entity's visual range
toPoint.subVectors( point, worldPosition );
final distanceToPoint = toPoint.length;
if ( distanceToPoint > range ) return false;
// next, check if the point lies within the game entity's field of view
owner?.getWorldDirection( direction );
final angle = direction.angleTo( toPoint );
if ( angle > ( fieldOfView * 0.5 ) ) return false;
// the point lies within the game entity's visual range and field
// of view. now check if obstacles block the game entity's view to the given point.
ray.origin.copy( worldPosition );
ray.direction.copy( toPoint ).divideScalar(distanceToPoint != 0?distanceToPoint: 1); // normalize
for ( int i = 0, l = obstacles.length; i < l; i ++ ) {
final obstacle = obstacles[ i ];
final intersection = obstacle.lineOfSightTest( ray, intersectionPoint );
if ( intersection != null ) {
// if an intersection point is closer to the game entity than the given point,
// something is blocking the game entity's view
final squaredDistanceToIntersectionPoint = intersectionPoint.squaredDistanceTo( worldPosition );
if ( squaredDistanceToIntersectionPoint <= ( distanceToPoint * distanceToPoint ) ) return false;
}
}
return true;
}