use crate::constants::{AIR_DENSITY_SEA_LEVEL, SPEED_OF_SOUND_MPS};
#[derive(Debug, Clone, Copy)]
pub struct AerodynamicJumpComponents {
pub vertical_jump_moa: f64, pub horizontal_jump_moa: f64, pub jump_angle_rad: f64, pub magnus_component_moa: f64, pub yaw_component_moa: f64, pub stabilization_factor: f64, }
pub fn calculate_aerodynamic_jump(
muzzle_velocity_mps: f64,
spin_rate_rad_s: f64,
crosswind_mps: f64,
caliber_m: f64,
mass_kg: f64,
barrel_length_m: f64,
twist_rate_calibers: f64,
is_right_twist: bool,
initial_yaw_rad: f64,
air_density_kg_m3: f64,
) -> AerodynamicJumpComponents {
if muzzle_velocity_mps <= 0.0 || caliber_m <= 0.0 {
return AerodynamicJumpComponents {
vertical_jump_moa: 0.0,
horizontal_jump_moa: 0.0,
jump_angle_rad: 0.0,
magnus_component_moa: 0.0,
yaw_component_moa: 0.0,
stabilization_factor: 0.0,
};
}
let mach = muzzle_velocity_mps / SPEED_OF_SOUND_MPS;
let magnus_coeff = if mach < 0.8 {
0.25
} else if mach < 1.2 {
0.15 } else {
0.20
};
let spin_param = (spin_rate_rad_s * caliber_m / 2.0) / muzzle_velocity_mps;
let crosswind_yaw = if crosswind_mps != 0.0 {
(crosswind_mps / muzzle_velocity_mps).atan()
} else {
0.0
};
let total_yaw_rad = crosswind_yaw + initial_yaw_rad;
let area = std::f64::consts::PI * (caliber_m / 2.0).powi(2);
let magnus_force = 0.5
* air_density_kg_m3
* muzzle_velocity_mps.powi(2)
* area
* magnus_coeff
* spin_param
* total_yaw_rad.sin();
let exit_time = 2.0 * barrel_length_m / muzzle_velocity_mps;
let stabilization_calibers = 20.0 / (twist_rate_calibers / 10.0).sqrt();
let stabilization_distance = stabilization_calibers * caliber_m;
let stabilization_time = stabilization_distance / muzzle_velocity_mps;
let effective_time = exit_time + stabilization_time;
let vertical_sign = if is_right_twist {
crosswind_mps.signum()
} else {
-crosswind_mps.signum()
};
let magnus_accel = magnus_force / mass_kg;
let lever_factor = (barrel_length_m / caliber_m) * 0.1;
let magnus_enhancement = 50.0;
let mut vertical_jump_m = magnus_enhancement
* lever_factor
* vertical_sign
* magnus_accel.abs()
* effective_time.powi(2);
if total_yaw_rad != 0.0 {
let yaw_contribution = total_yaw_rad.abs() * barrel_length_m * 0.5;
vertical_jump_m += vertical_sign * yaw_contribution;
}
let horizontal_jump_m = 0.25 * vertical_jump_m * (2.0 * total_yaw_rad).sin();
const YARDS_TO_M: f64 = 0.9144;
const MOA_PER_RADIAN: f64 = 3437.7467707849;
let range_100y = 100.0 * YARDS_TO_M;
let vertical_angle_rad = vertical_jump_m / range_100y;
let horizontal_angle_rad = horizontal_jump_m / range_100y;
let vertical_jump_moa = vertical_angle_rad * MOA_PER_RADIAN;
let horizontal_jump_moa = horizontal_angle_rad * MOA_PER_RADIAN;
let total_jump_rad = (vertical_angle_rad.powi(2) + horizontal_angle_rad.powi(2)).sqrt();
let magnus_component_moa = vertical_jump_moa.abs() * 0.8;
let yaw_component_moa = vertical_jump_moa.abs() * 0.2;
let caliber_in = caliber_m / 0.0254;
let sg_approx = 30.0 * mass_kg * 15.432 / (twist_rate_calibers.powi(2) * caliber_in.powi(3));
let stabilization_factor = (sg_approx / 1.5).min(1.0);
AerodynamicJumpComponents {
vertical_jump_moa,
horizontal_jump_moa,
jump_angle_rad: total_jump_rad,
magnus_component_moa,
yaw_component_moa,
stabilization_factor,
}
}
pub fn calculate_sight_correction_for_jump(
jump_components: &AerodynamicJumpComponents,
zero_range_m: f64,
sight_height_m: f64,
) -> (f64, f64) {
let range_factor = 91.44 / zero_range_m;
let mut vertical_correction = -jump_components.vertical_jump_moa * range_factor;
let horizontal_correction = -jump_components.horizontal_jump_moa * range_factor;
let sight_factor = 1.0 + (sight_height_m / 0.05);
vertical_correction *= sight_factor;
(vertical_correction, horizontal_correction)
}
pub fn calculate_crosswind_jump_sensitivity(
muzzle_velocity_mps: f64,
spin_rate_rad_s: f64,
caliber_m: f64,
mass_kg: f64,
twist_rate_calibers: f64,
is_right_twist: bool,
) -> f64 {
const MPH_TO_MPS: f64 = 0.44704;
let crosswind_1mph = MPH_TO_MPS;
let jump = calculate_aerodynamic_jump(
muzzle_velocity_mps,
spin_rate_rad_s,
crosswind_1mph,
caliber_m,
mass_kg,
0.6, twist_rate_calibers,
is_right_twist,
0.0, AIR_DENSITY_SEA_LEVEL,
);
jump.vertical_jump_moa.abs()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_aerodynamic_jump_zero_conditions() {
let jump = calculate_aerodynamic_jump(
800.0, 1000.0, 0.0, 0.00762, 0.01134, 0.6, 32.47, true, 0.0, 1.225, );
assert_eq!(jump.vertical_jump_moa, 0.0);
assert!(jump.horizontal_jump_moa.abs() < 0.001);
}
#[test]
fn test_aerodynamic_jump_with_crosswind() {
let jump = calculate_aerodynamic_jump(
800.0, 17593.0, 4.4704, 0.00782, 0.01134, 0.6096, 32.47, true, 0.0, 1.225, );
assert!(jump.vertical_jump_moa > 0.0);
assert!(jump.stabilization_factor > 0.0);
}
#[test]
fn test_opposite_twist_direction() {
let crosswind = 4.4704;
let jump_right = calculate_aerodynamic_jump(
800.0, 17593.0, crosswind, 0.00782, 0.01134, 0.6096, 32.47, true, 0.0, 1.225,
);
let jump_left = calculate_aerodynamic_jump(
800.0, 17593.0, crosswind, 0.00782, 0.01134, 0.6096, 32.47, false, 0.0, 1.225,
);
assert!((jump_right.vertical_jump_moa + jump_left.vertical_jump_moa).abs() < 0.001);
}
#[test]
fn test_sight_correction() {
let jump = AerodynamicJumpComponents {
vertical_jump_moa: 0.5,
horizontal_jump_moa: 0.1,
jump_angle_rad: 0.0001,
magnus_component_moa: 0.4,
yaw_component_moa: 0.1,
stabilization_factor: 0.9,
};
let (vert, horiz) = calculate_sight_correction_for_jump(
&jump, 274.32, 0.05, );
assert!(vert < 0.0);
assert!(horiz < 0.0);
}
#[test]
fn test_crosswind_sensitivity() {
let sensitivity = calculate_crosswind_jump_sensitivity(
800.0, 17593.0, 0.00782, 0.01134, 32.47, true, );
assert!(sensitivity > 0.0);
assert!(sensitivity < 0.5);
}
}