~starkingdoms/starkingdoms

ref: 5ccd4fb282fe3858f302b284a4d985d590ac79a4 starkingdoms/crates/unified/src/shared/orbit.rs -rw-r--r-- 3.1 KiB
5ccd4fb2ghostly_zsh feat: removed replicon + clientbound messages implemented, but untested 9 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::collections::HashMap;
use std::f64::consts::PI;
use avian2d::math::TAU;
use avian2d::prelude::{LinearVelocity, Mass};
use bevy::log::debug;
use bevy::prelude::{App, Plugin, Transform, Update};
use bevy::time::Time;
use crate::shared::config::planet::{Planet, PlanetSpring};
use crate::prelude::{Query, Res, Without};
use crate::shared::ecs::TimeOffset;
use crate::shared::world_config::WorldConfigResource;

pub struct OrbitPlugin;
impl Plugin for OrbitPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(Update, update_orbits);
    }
}

pub fn update_orbits(
    mut planets: Query<(&Planet, &Transform, &mut LinearVelocity), Without<PlanetSpring>>,
    planets_2: Query<(&Planet, &Transform, &Mass), Without<PlanetSpring>>,
    mut planet_springs: Query<(&PlanetSpring, &mut Transform, &mut LinearVelocity), Without<Planet>>,
    world_config: Res<WorldConfigResource>,
    time: Res<Time>,
    time_offset: Res<TimeOffset>,
) {
    let Some(ref world_config) = world_config.config else {
        return;
    };
    let parent_velocities = planets.iter().map(|u| (u.0.name.clone(), u.2.clone())).collect::<HashMap<String, LinearVelocity>>();
    for (planet, _, _vel) in planets.iter_mut() {
        let Some(orbit_data) = &planet.orbit else { continue; };
        let Some(parent) = planets_2.iter().find(|u| u.0.name == orbit_data.orbiting) else { continue; };

        let (parent_data, parent_transform, parent_mass) = parent;
        let a = (planet.default_position.x as f64 - parent_data.default_position.x as f64) / (1.0 - orbit_data.eccentricity);
        let e = orbit_data.eccentricity;
        let t = 2.0*PI*((a*a*a)/(world_config.world.gravity*(**parent_mass as f64))).sqrt();

        let time = time.elapsed_secs_f64() + time_offset.0;

        let m = (TAU / t) * time;
        let e_k = iterative_kepler(m, e);
        let nu = 2.0_f64 * ((1.0 + e).sqrt() * (e_k / 2.0).sin())
            .atan2((1.0 - e).sqrt() * (e_k / 2.0).cos());
        let r = a * (1.0 - e * e_k.cos());

        let x = r * nu.cos();
        let y = r * nu.sin();

        let Some(mut planet_spring) = planet_springs.iter_mut().find(|u| u.0.name == planet.name) else { continue; };
        planet_spring.1.translation.x = x as f32 + parent_transform.translation.x;
        planet_spring.1.translation.y = y as f32 + parent_transform.translation.y;

        let Some(parent_velocity) = parent_velocities.get(&orbit_data.orbiting) else { continue; };
        let de_dt = (TAU / t) / (1.0 - e * e_k.cos());
        let b_factor = (1.0 - e * e).sqrt();
        let vx = -a * e_k.sin() * de_dt;
        let vy = a * b_factor * e_k.cos() * de_dt;
        planet_spring.2.x = vx + parent_velocity.x;
        planet_spring.2.y = vy + parent_velocity.y;
    }
}

fn iterative_kepler(m: f64, e: f64) -> f64 {
    const MAX_ITERATIONS: u32 = 100;
    const CONVERGENCE_THRESHOLD: f64 = 1e-10;
    let mut output = m;
    for _ in 0..MAX_ITERATIONS {
        let d = (m - output + e * output.sin()) / (1.0 - e * output.cos());
        output += d;
        if d.abs() < CONVERGENCE_THRESHOLD {
            break;
        }
    }
    output
}