@@ 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 } } }
@@ 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,