From 63a231d794983e89781b9c91c28aacae2190cbf4 Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Sun, 16 Apr 2023 21:50:26 -0400 Subject: [PATCH] terrible orbit code --- server/src/orbit/constants.rs | 6 +++--- server/src/orbit/orbit.rs | 32 +++++++++++++------------------- server/src/planet.rs | 1 + server/src/timer.rs | 5 +++-- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/server/src/orbit/constants.rs b/server/src/orbit/constants.rs index eed1ce60f1b6dd58675dd89d3faee8d36d5448e8..9acda4221f47103463d1e2ce9b686f53fbde72d5 100644 --- a/server/src/orbit/constants.rs +++ b/server/src/orbit/constants.rs @@ -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 diff --git a/server/src/orbit/orbit.rs b/server/src/orbit/orbit.rs index d38caaad1d309208aa974d477397d9052cbed9fe..e43dce2b33db6c55a3d23750937502bc0d8445e1 100644 --- a/server/src/orbit/orbit.rs +++ b/server/src/orbit/orbit.rs @@ -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 { +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 { 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 { diff --git a/server/src/planet.rs b/server/src/planet.rs index 049ecd523a2ee5e9a66009c9d942ef200b088f77..3aaaebb4ac82be815b5264f1e429cf60b2bf1b71 100644 --- a/server/src/planet.rs +++ b/server/src/planet.rs @@ -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); diff --git a/server/src/timer.rs b/server/src/timer.rs index 013602fd00c0b6e08e919920be9f8b1ebc3616a8..c4bd29bce57b4d8034bf22132753c3ce3cf9f17c 100644 --- a/server/src/timer.rs +++ b/server/src/timer.rs @@ -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