apply gravity towards Jupiter for objects orbiting Jupiter

This commit is contained in:
yuni 2024-06-08 04:17:28 +02:00
parent 7be6b0746f
commit e16a650b22
2 changed files with 38 additions and 0 deletions

View file

@ -44,6 +44,7 @@ impl Plugin for ActorPlugin {
app.add_systems( app.add_systems(
Update, Update,
( (
handle_gravity,
handle_input.run_if(in_control), handle_input.run_if(in_control),
handle_collisions, handle_collisions,
handle_damage, handle_damage,
@ -51,9 +52,16 @@ impl Plugin for ActorPlugin {
); );
app.add_systems(PostUpdate, (handle_vehicle_enter_exit,)); app.add_systems(PostUpdate, (handle_vehicle_enter_exit,));
app.add_event::<VehicleEnterExitEvent>(); app.add_event::<VehicleEnterExitEvent>();
app.insert_resource(GravityUpdateTimer(Timer::from_seconds(
1.0,
TimerMode::Repeating,
)));
} }
} }
#[derive(Resource)]
pub struct GravityUpdateTimer(Timer);
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub enum DamageType { pub enum DamageType {
Unknown, Unknown,
@ -157,6 +165,8 @@ pub struct WantsMaxVelocity(pub f64);
pub struct WantsToLookAt(pub String); pub struct WantsToLookAt(pub String);
#[derive(Component)] #[derive(Component)]
pub struct Identifier(pub String); pub struct Identifier(pub String);
#[derive(Component)]
pub struct OrbitsJupiter;
#[derive(Component)] #[derive(Component)]
pub struct LifeForm { pub struct LifeForm {
@ -632,3 +642,27 @@ fn handle_gforce(
} }
} }
} }
fn handle_gravity(
time: Res<Time>,
mut timer: ResMut<GravityUpdateTimer>,
mut q_pos: Query<(&Position, &mut LinearVelocity), With<OrbitsJupiter>>,
id2pos: Res<game::Id2Pos>,
) {
if !timer.0.tick(time.delta()).just_finished() {
return;
}
let pos_jupiter: DVec3 = if let Some(pos_jupiter) = id2pos.0.get(cmd::ID_JUPITER) {
*pos_jupiter
} else {
return;
};
// this assumes prograde orbits for every object
for (pos, mut v) in &mut q_pos {
let relative_pos = pos.0 - pos_jupiter;
let accel = nature::gravitational_acceleration(relative_pos, nature::JUPITER_MASS);
v.0 += accel;
}
}

View file

@ -668,6 +668,7 @@ fn spawn_entities(
1.0 1.0
} * state.model_scale, } * state.model_scale,
); );
let orbits_jupiter = state.id != ID_JUPITER;
// Spawn the actor // Spawn the actor
let actor_entity; let actor_entity;
@ -680,6 +681,9 @@ fn spawn_entities(
..default() ..default()
}); });
actor.insert(SleepingDisabled); actor.insert(SleepingDisabled);
if orbits_jupiter {
actor.insert(actor::OrbitsJupiter);
}
actor.insert(world::DespawnOnPlayerDeath); actor.insert(world::DespawnOnPlayerDeath);
actor.insert(actor::HitPoints::default()); actor.insert(actor::HitPoints::default());
actor.insert(Position::from(absolute_pos)); actor.insert(Position::from(absolute_pos));