Update deprecated rand methods (thread_rng→rng, gen_range→random_range, gen_bool→random_bool). Prefix unused struct fields with underscore (scanner_camera_buffer, color). Allow dead_code on App struct for fields only read in wasm cfg blocks.
66 lines
1.6 KiB
Rust
66 lines
1.6 KiB
Rust
use glam::{Vec2, Vec3};
|
|
|
|
use crate::scene::SceneObject;
|
|
|
|
pub struct Scanner {
|
|
pub range: f32,
|
|
}
|
|
|
|
pub struct ScannerBlip {
|
|
pub disc_pos: Vec2,
|
|
pub altitude: f32,
|
|
pub _color: [f32; 3],
|
|
}
|
|
|
|
impl Scanner {
|
|
pub fn new(range: f32) -> Self {
|
|
Self { range }
|
|
}
|
|
|
|
pub fn compute_blips(
|
|
&self,
|
|
camera_pos: Vec3,
|
|
camera_yaw: f32,
|
|
objects: &[SceneObject],
|
|
) -> Vec<ScannerBlip> {
|
|
let mut blips = Vec::new();
|
|
let cos_yaw = (-camera_yaw).cos();
|
|
let sin_yaw = (-camera_yaw).sin();
|
|
let disc_scale = 1.0 / self.range;
|
|
|
|
for obj in objects {
|
|
let rel = obj.position - camera_pos;
|
|
let horiz_dist = (rel.x * rel.x + rel.y * rel.y).sqrt();
|
|
|
|
if horiz_dist > self.range {
|
|
continue;
|
|
}
|
|
|
|
// Rotate by negative yaw so scanner is view-relative,
|
|
// then apply 90° CCW so forward maps to scanner +Y (top of disc)
|
|
let rotated_x = rel.x * cos_yaw - rel.y * sin_yaw;
|
|
let rotated_y = rel.x * sin_yaw + rel.y * cos_yaw;
|
|
let rx = -rotated_y;
|
|
let ry = rotated_x;
|
|
|
|
let disc_x = rx * disc_scale;
|
|
let disc_y = ry * disc_scale;
|
|
|
|
// Skip if outside the disc
|
|
if disc_x * disc_x + disc_y * disc_y > 1.0 {
|
|
continue;
|
|
}
|
|
|
|
let altitude = (rel.z * disc_scale).clamp(-0.6, 0.6);
|
|
|
|
blips.push(ScannerBlip {
|
|
disc_pos: Vec2::new(disc_x, disc_y),
|
|
altitude,
|
|
_color: obj.color,
|
|
});
|
|
}
|
|
|
|
blips
|
|
}
|
|
}
|