base the maximum targeting angle on the angular diameter of the object

This commit is contained in:
yuni 2024-04-05 20:28:28 +02:00
parent c8340c4890
commit 9694ebfecd

View file

@ -346,8 +346,6 @@ pub fn find_closest_target<TargetSpecifier>(
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<TargetSpecifier>(
.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);