From b16e99223d82974b54235e870ad079e4ca74e7a4 Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Wed, 12 Apr 2023 22:43:29 -0500 Subject: [PATCH] gravity added, client still broken --- server/src/planet.rs | 28 +++++++++++- server/src/timer.rs | 41 ++++++++++-------- .../__pycache__/ninja_syntax.cpython-310.pyc | Bin 5942 -> 5958 bytes 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/server/src/planet.rs b/server/src/planet.rs index fb1ac78cd34c406520a538809a0ffe19dbd580ad..188bfd0f932e1ab11c7f739146d3932a10a69ae3 100644 --- a/server/src/planet.rs +++ b/server/src/planet.rs @@ -1,8 +1,11 @@ +use nalgebra::{Vector2, vector}; use rapier2d_f64::prelude::{RigidBodyHandle, RigidBodySet, ColliderBuilder, RigidBodyBuilder, ColliderSet}; use starkingdoms_protocol::{PlanetType, ProtocolPlanet}; use crate::{SCALE, manager::ClientHandlerMessage}; +const GRAVITY: f64 = 0.001; + #[derive(Clone)] pub struct Planet { pub planet_type: PlanetType, @@ -12,6 +15,16 @@ pub struct Planet { pub mass: f64 } +impl Planet { + pub fn gravity(&self, position: (f64, f64), mass: f64) -> (f64, f64) { + let distance = ((position.0 - self.position.0).powi(2) + (position.1 - self.position.1).powi(2)).sqrt(); + let force = GRAVITY * ((self.mass * mass) / distance * distance); + let mut direction = Vector2::new(self.position.0 - position.0, self.position.1 - position.1); + direction.set_magnitude(force); + (direction.x, direction.y) + } +} + #[derive(Default, Clone)] pub struct Planets { pub planets: Vec, @@ -19,9 +32,10 @@ pub struct Planets { impl Planets { pub fn make_planet(planets: &mut Vec, planet_type: PlanetType, mass: f64, radius: f64, position: (f64, f64), rigid_body_set: &mut RigidBodySet, collider_set: &mut ColliderSet) { - let collider = ColliderBuilder::ball(1000.0 / SCALE) + let collider = ColliderBuilder::ball(radius / SCALE) .build(); let body = RigidBodyBuilder::fixed() + .translation(vector![position.0, position.1]) .additional_mass(0.0); let body_handle = rigid_body_set.insert(body); @@ -60,7 +74,7 @@ impl Planets { planet_type: planet.planet_type, x: planet.position.0 * SCALE, y: planet.position.1 * SCALE, - radius: planet.radius, + radius: planet.radius * SCALE, }); } @@ -68,4 +82,14 @@ impl Planets { planets } } + + pub fn gravity(&self, position: (f64, f64), mass: f64) -> (f64, f64) { + let mut direction = Vector2::zeros(); + for planet in self.planets.clone() { + let planet_grav = planet.gravity(position, mass); + direction.x = planet_grav.0; + direction.y = planet_grav.1; + } + (direction.x, direction.y) + } } diff --git a/server/src/timer.rs b/server/src/timer.rs index 37dc74d4b7cfca5a7874a0fadff68f362e95fc3e..eb6df7c18bdf831763b99e5c68f1d31c74b37955 100644 --- a/server/src/timer.rs +++ b/server/src/timer.rs @@ -2,6 +2,7 @@ use std::{time::Duration, sync::Arc}; use log::{error}; +use nalgebra::vector; use rapier2d_f64::prelude::{PhysicsPipeline, RigidBodyHandle}; use starkingdoms_protocol::{ProtocolPlanet, PlanetType, ProtocolPlayer}; use tokio::{time::sleep, sync::RwLock}; @@ -14,29 +15,33 @@ pub async fn timer_main(mgr: ClientManager, physics_data: Arc