From 49175fb533db030ca3d3dee8434ea3aa905b4172 Mon Sep 17 00:00:00 2001 From: core Date: Sun, 19 Apr 2026 18:50:40 -0400 Subject: [PATCH] feat: orbits --- crates/unified/assets/config/planets.pc.toml | 23 +++--- crates/unified/src/client_plugins.rs | 1 + crates/unified/src/config/planet.rs | 26 ++++++- crates/unified/src/config/world.rs | 2 +- crates/unified/src/server/mod.rs | 3 + crates/unified/src/server/orbit/mod.rs | 74 ++++++++++++++++++++ crates/unified/src/server/planets.rs | 59 ++++++++++++---- crates/unified/src/shared_plugins.rs | 4 +- crates/unified/src/world_config.rs | 1 + 9 files changed, 168 insertions(+), 25 deletions(-) create mode 100644 crates/unified/src/server/orbit/mod.rs diff --git a/crates/unified/assets/config/planets.pc.toml b/crates/unified/assets/config/planets.pc.toml index 217a961f8c63a1f4974156de48f74829272a418e..e730ade69317f3ff293e292b36a761a2589b2e37 100644 --- a/crates/unified/assets/config/planets.pc.toml +++ b/crates/unified/assets/config/planets.pc.toml @@ -1,3 +1,6 @@ +[orbit] +planet_spring_compliance = 0 + [[planets]] name = "Sun" sprite = "textures/sun.png" @@ -14,7 +17,7 @@ radius = 666.66 # m mass = 205_000_000.0 # kg default_transform = [116_129.4, 0.0, 0.0] planet_resource = { name = "Composite", color = { LinearRgba = { red = 0.7, green = 0.7, blue = 0.7, alpha = 1.0 } }, mining_speed = 5.0 } -orbit = { orbiting = "Sun", eccentricity = 0.2056 } +orbit = { orbiting = "Sun", eccentricity = 0.2056, period = 299.0 } [[planets]] name = "Venus" @@ -23,7 +26,7 @@ radius = 1899.8 # m mass = 806_166_000.0 # kg default_transform = [216_999.6, 0.0, 0.0] planet_resource = { name = "Sulfur", color = { LinearRgba = { red = 0.7, green = 0.7, blue = 0.7, alpha = 1.0 } }, mining_speed = 5.0 } -orbit = { orbiting = "Sun", eccentricity = 0.0068 } +orbit = { orbiting = "Sun", eccentricity = 0.0068, period = 546.0 } [[planets]] name = "Earth" @@ -33,7 +36,7 @@ radius = 2000.0 # m mass = 16_900_000_000.0 # kg planet_resource = { name = "Carbon", color = { LinearRgba = { red = 1.0, green = 0.7, blue = 0.7, alpha = 1.0 } }, mining_speed = 2.0 } default_transform = [300_000.0, 0.0, 0.0] -orbit = { orbiting = "Sun", eccentricity = 0.0167 } +orbit = { orbiting = "Sun", eccentricity = 0.0167, period = 900.0 } [[planets]] name = "Moon" @@ -43,7 +46,7 @@ radius = 545.4 # m mass = 360_236_000.0 # kg default_transform = [312_700.0, 0.0, 0.0] planet_resource = { name = "Silicon", color = { LinearRgba = { red = 0.7, green = 0.7, blue = 0.7, alpha = 1.0 } }, mining_speed = 5.0 } -orbit = { orbiting = "Earth", eccentricity = 0.0549 } +orbit = { orbiting = "Earth", eccentricity = 0.0549, period = 256.0 } [[planets]] name = "Mars" @@ -53,7 +56,7 @@ radius = 1062.0 # m mass = 525_857_000.0 # kg default_transform = [430_000.0, 0.0, 0.0] planet_resource = { name = "Iron", color = { LinearRgba = { red = 0.7, green = 0.7, blue = 0.7, alpha = 1.0 } }, mining_speed = 5.0 } -orbit = { orbiting = "Sun", eccentricity = 0.0934 } +orbit = { orbiting = "Sun", eccentricity = 0.0934, period = 1745.0 } [[planets]] name = "Jupiter" @@ -62,7 +65,7 @@ radius = 21946.0 # m mass = 1_131_221_218_000.0 # kg default_transform = [1_561_140.0, 0.0, 0.0] planet_resource = { name = "Hydrogen", color = { LinearRgba = { red = 0.7, green = 0.7, blue = 0.7, alpha = 1.0 } }, mining_speed = 5.0 } -orbit = { orbiting = "Sun", eccentricity = 0.0484 } +orbit = { orbiting = "Sun", eccentricity = 0.0484, period = 11218.0 } [[planets]] name = "Saturn" @@ -71,7 +74,7 @@ radius = 18_280.4 # m mass = 561_386_112_000.0 # kg default_transform = [2_874_780.0, 0.0, 0.0] planet_resource = { name = "Helium", color = { LinearRgba = { red = 0.7, green = 0.7, blue = 0.7, alpha = 1.0 } }, mining_speed = 5.0 } -orbit = { orbiting = "Sun", eccentricity = 0.0541 } +orbit = { orbiting = "Sun", eccentricity = 0.0541, period = 28297.0 } [[planets]] name = "Uranus" @@ -80,7 +83,7 @@ radius = 8014.0 # m mass = 69_763_532_000.0 # kg default_transform = [4_050_000.0, 0.0, 0.0] planet_resource = { name = "Rubber", color = { LinearRgba = { red = 0.7, green = 0.7, blue = 0.7, alpha = 1.0 } }, mining_speed = 5.0 } -orbit = { orbiting = "Sun", eccentricity = 0.0472 } +orbit = { orbiting = "Sun", eccentricity = 0.0472, period = 46791.0 } [[planets]] name = "Neptune" @@ -89,7 +92,7 @@ radius = 7_766.0 # m mass = 106_674_649_000.0 # kg default_transform = [5_000_000.0, 0.0, 0.0] planet_resource = { name = "Methane", color = { LinearRgba = { red = 0.7, green = 0.7, blue = 0.7, alpha = 1.0 } }, mining_speed = 5.0 } -orbit = { orbiting = "Sun", eccentricity = 0.0086 } +orbit = { orbiting = "Sun", eccentricity = 0.0086, period = 60454.0 } [[planets]] name = "Pluto" @@ -98,4 +101,4 @@ radius = 373.6 # m mass = 10_817_000.0 # kg default_transform = [5_922_300.0, 0.0, 0.0] planet_resource = { name = "Ice", color = { LinearRgba = { red = 0.7, green = 0.7, blue = 0.7, alpha = 1.0 } }, mining_speed = 5.0 } -orbit = { orbiting = "Sun", eccentricity = 0.2488 } +orbit = { orbiting = "Sun", eccentricity = 0.2488, period = 118234.0 } diff --git a/crates/unified/src/client_plugins.rs b/crates/unified/src/client_plugins.rs index 74e7cadbb9b07e70e54cf02392603ce83a2e6783..235e3d72d31817e441ba3be2a88f78a020680601 100644 --- a/crates/unified/src/client_plugins.rs +++ b/crates/unified/src/client_plugins.rs @@ -25,6 +25,7 @@ impl PluginGroup for ClientPluginGroup { .add(UiPlugin) .add(InputDispatchPlugin) .add(InputManagerPlugin::::default()) + .add(PhysicsDebugPlugin) } } diff --git a/crates/unified/src/config/planet.rs b/crates/unified/src/config/planet.rs index f617f38cf038137a546e62055592cd3669683556..c95a67c0c7f85fd90dacea6a0735fe13dfb90612 100644 --- a/crates/unified/src/config/planet.rs +++ b/crates/unified/src/config/planet.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; #[derive(Deserialize, Asset, TypePath, Component, Serialize, Clone, Debug)] #[require( - RigidBody::Static, + RigidBody::Dynamic, Replicated, )] pub struct Planet { @@ -21,6 +21,23 @@ pub struct Planet { pub orbit: Option } +#[derive(Component, Serialize, Deserialize)] +#[require( + RigidBody::Static, + Replicated, +)] +pub struct PlanetSpring { + pub name: String +} + +#[derive(Component, Serialize, Deserialize)] +#[require( + Replicated, +)] +pub struct PlanetSpringJoint { + pub name: String +} + #[derive(Deserialize, TypePath, Serialize, Clone, Debug)] pub struct PlanetResource { pub name: String, @@ -32,6 +49,7 @@ pub struct PlanetResource { pub struct OrbitData { pub orbiting: String, pub eccentricity: f32, + pub period: f32 } #[derive(Deserialize, TypePath, Serialize, Clone, Debug)] @@ -50,4 +68,10 @@ pub struct PlanetBundle { #[derive(Deserialize, Asset, TypePath)] pub struct PlanetConfigCollection { pub planets: Vec, + pub orbit: OrbitConfig +} + +#[derive(Deserialize, Asset, TypePath, Clone, Debug)] +pub struct OrbitConfig { + pub planet_spring_compliance: f32 } diff --git a/crates/unified/src/config/world.rs b/crates/unified/src/config/world.rs index e890089832df793d47639d443c18e4f5865f9ba5..45cbb02f2ee82b790ebc9143b02610b8d0c045e6 100644 --- a/crates/unified/src/config/world.rs +++ b/crates/unified/src/config/world.rs @@ -6,7 +6,7 @@ use serde::Deserialize; pub struct GlobalWorldConfig { pub world: WorldConfig, pub part: WPartConfig, - pub hearty: HeartyConfig, + pub hearty: HeartyConfig } #[derive(Deserialize, Asset, TypePath, Clone)] diff --git a/crates/unified/src/server/mod.rs b/crates/unified/src/server/mod.rs index d5a2ebdea4e4ce88913981407eb2d1383df957ab..d5f8de9b3e7fbc199400513fadf0b380b872e420 100644 --- a/crates/unified/src/server/mod.rs +++ b/crates/unified/src/server/mod.rs @@ -8,6 +8,7 @@ mod damping; pub mod planets; pub mod player; mod system_sets; +pub mod orbit; use crate::server::craft::craft_plugin; use crate::server::damping::damping_plugin; @@ -29,6 +30,7 @@ use aeronet_websocket::server::WebSocketServer; use crate::prelude::*; use bevy_replicon::prelude::Replicated; use std::net::SocketAddr; +use crate::server::orbit::OrbitPlugin; use crate::server::player::thrust::server_thrust_plugin; pub struct ServerPlugin { @@ -63,6 +65,7 @@ impl Plugin for ServerPlugin { .add_plugins(heat_conduction_plugin)*/ .add_plugins(drill_plugin) .add_plugins(craft_plugin) + .add_plugins(OrbitPlugin) .add_plugins(damping_plugin) .configure_sets(Update, WorldUpdateSet.before(PlayerInputSet)); //.configure_sets(Update, PlayerInputSet.before(PhysicsSet::SyncBackend)); diff --git a/crates/unified/src/server/orbit/mod.rs b/crates/unified/src/server/orbit/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..e8b4c0c89b64a57f06f2512def7a5e4f9c5ec16b --- /dev/null +++ b/crates/unified/src/server/orbit/mod.rs @@ -0,0 +1,74 @@ +use std::collections::HashMap; +use avian2d::math::TAU; +use avian2d::prelude::LinearVelocity; +use bevy::log::debug; +use bevy::math::ops::atan2; +use bevy::prelude::{Component, Plugin, Transform}; +use bevy::time::Time; +use serde::{Deserialize, Serialize}; +use crate::config::planet::{Planet, PlanetSpring, PlanetSpringJoint}; +use crate::prelude::{App, Query, Res, Update, Without}; + +pub struct OrbitPlugin; +impl Plugin for OrbitPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Update, update_orbits); + } +} + +fn update_orbits( + mut planets: Query<(&Planet, &Transform, &mut LinearVelocity), Without>, + planets_2: Query<(&Planet, &Transform), Without>, + mut planet_springs: Query<(&PlanetSpring, &mut Transform), Without>, + time: Res