~starkingdoms/starkingdoms

63a231d794983e89781b9c91c28aacae2190cbf4 — c0repwn3r 2 years ago 4d0f675
terrible orbit code
4 files changed, 20 insertions(+), 24 deletions(-)

M server/src/orbit/constants.rs
M server/src/orbit/orbit.rs
M server/src/planet.rs
M server/src/timer.rs
M server/src/orbit/constants.rs => server/src/orbit/constants.rs +3 -3
@@ 1,7 1,7 @@
pub const GAME_SCALE_DISTANCE: f64 = 0.0001567865; // 1000 / EARTH_RADIUS_RL
pub const GAME_SCALE_MASS: f64 = 0.0000000000000000000006697923643670463; // 4000 / EARTH_MASS_RL
pub const GAME_SCALE_TIME: f64 = 0.00051440329218107; // 1200 / MOON_ORBIT_TIME_RL
pub const GAME_ORBITS_ENABLED: bool = true;
pub const GAME_ORBITS_ENABLED: bool = false;

pub const EARTH_RADIUS_BIAS: f64 = 1.0;
pub const EARTH_MASS_BIAS: f64 = 1.0;


@@ 15,7 15,7 @@ pub const MOON_RADIUS_BIAS: f64 = 1.0;
pub const MOON_MASS_BIAS: f64 = 10.0;
pub const MOON_PERIAPSIS_BIAS: f64 = 0.1;
pub const MOON_APOAPSIS_BIAS: f64 = 0.1;
pub const MOON_ORBIT_TIME_BIAS: f64 = 0.5;
pub const MOON_ORBIT_TIME_BIAS: f64 = 1.0;

pub const MOON_RADIUS_RL: f64 = 1_737_400.0;
pub const MOON_RADIUS: f64 = MOON_RADIUS_RL * GAME_SCALE_DISTANCE * MOON_RADIUS_BIAS;


@@ 24,4 24,4 @@ pub const MOON_MASS: f64 = MOON_MASS_RL * GAME_SCALE_MASS * MOON_MASS_BIAS;
pub const MOON_PERIAPSIS: f64 = 363228900.0 * GAME_SCALE_DISTANCE * MOON_PERIAPSIS_BIAS;
pub const MOON_APOAPSIS: f64 = 405400000.0  * GAME_SCALE_DISTANCE * MOON_APOAPSIS_BIAS;
pub const MOON_ORBIT_TIME_RL: f64 = 2332800.0;
pub const MOON_ORBIT_TIME: f64 = MOON_ORBIT_TIME_RL * GAME_SCALE_TIME * MOON_ORBIT_TIME_BIAS;
\ No newline at end of file
pub const MOON_ORBIT_TIME: f64 = 1.0; //MOON_ORBIT_TIME_RL * GAME_SCALE_TIME * MOON_ORBIT_TIME_BIAS;
\ No newline at end of file

M server/src/orbit/orbit.rs => server/src/orbit/orbit.rs +13 -19
@@ 7,33 7,27 @@ use crate::orbit::newtonian::solve_kepler_with_newtonian;
use crate::orbit::vis_viva::vis_viva;
use crate::planet::GRAVITY;

pub fn calculate_vector_of_orbit(periapsis: f64, apoapsis: f64, t: f64, mass_of_bigger: f64, current_x_vel: f64, current_y_vel: f64, delta_t: f64, mass_of_orbiter: f64) -> Vector2<f64> {
pub fn calculate_vector_of_orbit(periapsis: f64, apoapsis: f64, t: f64, current_x: f64, current_y: f64, orbiting_x: f64, orbiting_y: f64, mass: f64, step: f64) -> Vector2<f64> {
    let semi_major_length = (apoapsis + periapsis) / 2.0;
    let linear_eccentricity = semi_major_length - periapsis; // distance between center and focus

    let distances = calculate_point_on_orbit(periapsis, apoapsis, t);
    let distance_x = distances[0];
    let distance_y = distances[1];
    let target = calculate_world_position_of_orbit(calculate_point_on_orbit(periapsis, apoapsis, t), vector![orbiting_x, orbiting_y]);
    let target_x = target[0];
    let target_y = target[1];

    let distance = (distance_x * distance_x + distance_y * distance_y).sqrt();
    let delta_x = target_x - current_x;
    let delta_y = target_y - current_y;

    let velocity = vis_viva(distance, semi_major_length, GRAVITY, mass_of_bigger);
    let velocity_x = delta_x / step;
    let velocity_y = delta_y / step;

    let ellipse_center_x = -linear_eccentricity;
    let ellipse_center_y = apoapsis - semi_major_length;

    let theta = (ellipse_center_x / ellipse_center_y).atan() - std::f64::consts::PI / 2.0;

    let x_vel = velocity * theta.sin();
    let y_vel = velocity * theta.cos();

    let x_accel = current_x_vel - x_vel / delta_t;
    let y_accel = current_y_vel - y_vel / delta_t;
    let accel_x = velocity_x / step;
    let accel_y = velocity_y / step;

    let x_force = mass_of_orbiter * x_accel;
    let y_force = mass_of_orbiter * y_accel;
    let fx = accel_x * mass;
    let fy = accel_y * mass;

    vector![x_force, y_force]
    vector![fx, fy]
}

pub fn calculate_point_on_orbit(periapsis: f64, apoapsis: f64, t: f64) -> Vector2<f64> {

M server/src/planet.rs => server/src/planet.rs +1 -0
@@ 48,6 48,7 @@ impl Planets {
            .build();
        let body = RigidBodyBuilder::dynamic()
            .translation(vector![position.0 / SCALE, position.1 / SCALE])
            .dominance_group(127)
            .additional_mass(0.0);
        let body_handle = rigid_body_set.insert(body);


M server/src/timer.rs => server/src/timer.rs +3 -2
@@ 1,5 1,5 @@
use std::{time::Duration, sync::Arc};
use log::{warn};
use log::{debug, warn};
use nalgebra::{vector, point};
use rapier2d_f64::prelude::{PhysicsPipeline};
use async_std::sync::RwLock;


@@ 36,9 36,10 @@ pub async fn timer_main(mgr: ClientManager, physics_data: Arc<RwLock<PhysicsData
            // update moon
            let new_moon_position = calculate_world_position_of_orbit(calculate_point_on_orbit(MOON_PERIAPSIS, MOON_APOAPSIS, time / MOON_ORBIT_TIME), new_earth_position);
            let moon_body = physics_data.rigid_body_set.get_mut(planets.get_planet_mut("moon").unwrap().body_handle).unwrap();
            let moon_force = calculate_vector_of_orbit(MOON_PERIAPSIS, MOON_APOAPSIS, time / MOON_ORBIT_TIME, EARTH_MASS, moon_body.linvel().x, moon_body.linvel().y, 1f64/20f64, MOON_MASS);
            let moon_force = calculate_vector_of_orbit(MOON_PERIAPSIS, MOON_APOAPSIS, time / MOON_ORBIT_TIME, moon_body.translation().x, moon_body.translation().y, new_earth_position[0], new_earth_position[1],MOON_MASS, 1f64 / 20f64);
            moon_body.reset_forces(true);
            moon_body.add_force(moon_force, true);
            //moon_body.set_linvel(moon_force, true);
            planets.get_planet_mut("moon").unwrap().position = (moon_body.translation()[0] / SCALE, moon_body.translation()[1] / SCALE);
        }