diff --git a/src/camera.rs b/src/camera.rs index ae48570..2d5c2dc 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -346,8 +346,6 @@ pub fn find_closest_target( let mut closest_distance: f32 = f32::MAX; let target_vector = (camera_transform.rotation * Vec3::new(0.0, 0.0, -1.0)) .normalize_or_zero(); - let field_of_view = 20.0f32.to_radians(); - let max_angle = field_of_view / 2.0; for (entity, trans) in objects { // Use Transform instead of Position because we're basing this // not on the player mesh but on the camera, which doesn't have a position. @@ -355,10 +353,18 @@ pub fn find_closest_target( .normalize_or_zero(); let cosine_of_angle = target_vector.dot(pos_vector); let angle = cosine_of_angle.acos(); - if angle <= max_angle { + let distance = trans.translation.distance(camera_transform.translation); + let leeway = 1.3; + let angular_diameter = if distance > 0.0 { + // Angular Diameter + leeway * (trans.scale[0] / distance).asin() + } + else { + 0.0 + }; + if angle <= angular_diameter.clamp(0.001, PI) { // It's in the field of view! //commands.entity(entity).insert(IsTargeted); - let distance = trans.translation.distance(camera_transform.translation); if distance < closest_distance { closest_distance = distance; closest_entity = Some(entity);