~starkingdoms/starkingdoms

c1453fb8af91aa76679a061a4c592e9ef614b51b — ghostly_zsh 17 days ago b5f0ffc
feat: initial orbit velocity
M crates/unified/assets/config/planets.pc.toml => crates/unified/assets/config/planets.pc.toml +1 -0
@@ 8,6 8,7 @@ sprite = "textures/sun.png"
radius = 20_000.0 # m
#mass = 16_000_000_000_000.0 # kg
mass = 160_000_000.0 # kg
#mass = 1600000_000_000.0 # kg
default_transform = [0.0, 0.0, 0.0]
planet_resource = { name = "Plasma", color = { LinearRgba = { red = 0.7, green = 0.7, blue = 0.7, alpha = 1.0 } }, mining_speed = 5.0 }
special_sprite_properties = { ForceColor = { Oklcha = { lightness = 10.0, chroma = 0.058, hue = 104.26, alpha = 1.0 } } }

M crates/unified/src/server/gravity.rs => crates/unified/src/server/gravity.rs +2 -2
@@ 37,9 37,9 @@ fn update_gravity(
            let dt = time.delta_secs() as f64 / world_config.world.gravity_iterations as f64;
            for i in 0..world_config.world.gravity_iterations {
                let f =
                    world_config.world.gravity * ((part_mass * planet_mass) / (distance*distance)-x);
                    world_config.world.gravity * ((part_mass * planet_mass) / ((distance - x)*(distance - x)));
                let dx = dt*(v.project_onto((planet_translation-part_translation).truncate().as_dvec2())).length()
                    - 1.0/2.0*(f/part_mass)*dt*dt;
                    - 1.0/2.0*(f/part_mass)*(dt*dt);
                x += dx;
                v += f/part_mass*dt;
                if i == 0 || i == world_config.world.gravity_iterations-1 {

M crates/unified/src/server/planets.rs => crates/unified/src/server/planets.rs +31 -13
@@ 1,5 1,5 @@
use crate::{config::planet::{Planet, PlanetBundle, PlanetConfigCollection}, ecs::PlanetSensor};
use bevy::asset::Handle;
use crate::{config::planet::{Planet, PlanetBundle, PlanetConfigCollection}, ecs::PlanetSensor, world_config::WorldConfigResource};
use bevy::{asset::Handle, math::DVec3};
use crate::prelude::*;
use bevy_replicon::prelude::Replicated;
use crate::config::planet::{PlanetSpring, PlanetSpringJoint};


@@ 30,8 30,12 @@ pub fn update_planets(
        &mut Transform,
        &mut Mass,
    )>,
    mut planet_joint_springs: Query<(&PlanetSpringJoint, &mut FixedJoint)>
    mut planet_joint_springs: Query<(&PlanetSpringJoint, &mut FixedJoint)>,
    world_config: Res<WorldConfigResource>,
) {
    let Some(ref world_config) = world_config.config else {
        return;
    };
    let Some(handle) = planets.handle.as_ref() else {
        return;
    };


@@ 45,6 49,7 @@ pub fn update_planets(
                    debug!("planet config loaded - creating planets");
                    let planet_config = assets.get(*id).unwrap();
                    for planet in &planet_config.planets {
                        let planet_position = vec3(planet.default_transform[0], planet.default_transform[1], planet.default_transform[2]);
                        let mut planet_entity = commands
                            .spawn((PlanetBundle {
                                planet: planet.clone(),


@@ 57,14 62,28 @@ pub fn update_planets(
                                mass: Mass(planet.mass)
                            },
                            SleepingDisabled
                            )).with_child((
                                Collider::circle(planet.radius+2.0),
                                Sensor,
                                PlanetSensor(planet.name.clone()),
                                CollisionEventsEnabled,
                            )).id();
                        if planet.orbit.is_some() {
                            let spring =commands.spawn((
                        ));
                        planet_entity.with_child((
                            Collider::circle(planet.radius+2.0),
                            Sensor,
                            PlanetSensor(planet.name.clone()),
                            CollisionEventsEnabled,
                        ));
                        let planet_entity_id = planet_entity.id();
                        if let Some(orbit) = &planet.orbit {
                            let Some(parent_planet) = planet_config.planets.iter().find(|u| orbit.orbiting == u.name) else { continue };
                            let parent_planet_position = vec3(parent_planet.default_transform[0],
                                parent_planet.default_transform[1], parent_planet.default_transform[2]);
                            let r = (planet_position - parent_planet_position).as_dvec3();
                            let g = world_config.world.gravity * parent_planet.mass as f64
                                / r.length_squared();
                            // tangential velocity
                            let v = (g*r.length() as f64).sqrt();
                            let v_dir = DVec3::Z.cross(r).truncate().normalize();
                            let v = v_dir * v;
                            planet_entity.insert(LinearVelocity(v));

                            let spring = commands.spawn((
                                PlanetSpring {
                                    name: planet.name.clone()
                                },


@@ 74,13 93,12 @@ pub fn update_planets(
                                    planet.default_transform[2],
                                ),
                                RigidBody::Kinematic,
                                LinearVelocity::ZERO,
                            )).id();
                            commands.spawn((
                                PlanetSpringJoint {
                                    name: planet.name.clone()
                                },
                                FixedJoint::new(planet_entity, spring).with_point_compliance(planet_config.orbit.planet_spring_compliance),
                                FixedJoint::new(planet_entity_id, spring).with_point_compliance(planet_config.orbit.planet_spring_compliance),
                                JointDamping {
                                    linear: planet_config.orbit.planet_spring_damping,
                                    angular: 1.0,