ignore gravity when calculating g forces

Because gravity is obviously not a force, just a magical property of
spacetime, conveyed by messenger particles called Higgs Bosons which act
as force carriers for this non-force.  Who made this up? :D
This commit is contained in:
yuni 2024-06-11 03:29:26 +02:00
parent 91bf2ddc54
commit 6275a64d7c

View file

@ -30,6 +30,7 @@ impl Plugin for ActorPlugin {
(
update_physics_lifeforms,
update_power,
handle_gravity,
handle_wants_maxrotation,
handle_wants_maxvelocity.run_if(any_with_component::<WantsMaxVelocity>),
handle_wants_lookat.run_if(alive),
@ -44,7 +45,6 @@ impl Plugin for ActorPlugin {
app.add_systems(
Update,
(
handle_gravity,
handle_input.run_if(in_control),
handle_collisions,
handle_damage,
@ -52,16 +52,9 @@ impl Plugin for ActorPlugin {
);
app.add_systems(PostUpdate, (handle_vehicle_enter_exit,));
app.add_event::<VehicleEnterExitEvent>();
app.insert_resource(GravityUpdateTimer(Timer::from_seconds(
1.0,
TimerMode::Repeating,
)));
}
}
#[derive(Resource)]
pub struct GravityUpdateTimer(Timer);
#[derive(Copy, Clone)]
pub enum DamageType {
Unknown,
@ -126,6 +119,7 @@ pub struct ExperiencesGForce {
pub visual_effect_threshold: f32,
pub visual_effect: f32,
pub last_linear_velocity: DVec3,
pub gravitational_component: DVec3,
pub ignore_gforce_seconds: f32,
}
impl Default for ExperiencesGForce {
@ -135,7 +129,8 @@ impl Default for ExperiencesGForce {
damage_threshold: 100.0,
visual_effect_threshold: 20.0,
visual_effect: 0.0,
last_linear_velocity: DVec3::splat(0.0),
last_linear_velocity: DVec3::ZERO,
gravitational_component: DVec3::ZERO,
ignore_gforce_seconds: 0.01,
}
}
@ -650,8 +645,10 @@ fn handle_gforce(
let dt = time.delta_seconds();
let factor = 1.0 / dt / nature::EARTH_GRAVITY;
for (v, mut hp, mut gforce) in &mut q_actor {
gforce.gforce = factor * (v.0 - gforce.last_linear_velocity).length() as f32;
gforce.gforce = factor
* (v.0 - gforce.last_linear_velocity - gforce.gravitational_component).length() as f32;
gforce.last_linear_velocity = v.0;
gforce.gravitational_component = DVec3::ZERO;
if gforce.ignore_gforce_seconds > 0.0 {
gforce.ignore_gforce_seconds -= dt;
continue;
@ -675,18 +672,25 @@ fn handle_gforce(
fn handle_gravity(
time: Res<Time>,
mut timer: ResMut<GravityUpdateTimer>,
mut q_pos: Query<(&Position, &mut LinearVelocity), With<OrbitsJupiter>>,
mut q_pos: Query<
(
&Position,
&mut LinearVelocity,
Option<&mut ExperiencesGForce>,
),
With<OrbitsJupiter>,
>,
jupiter_pos: Res<game::JupiterPos>,
) {
if !timer.0.tick(time.delta()).just_finished() {
return;
}
let dt = time.delta_seconds() as f64;
// this assumes prograde orbits for every object
for (pos, mut v) in &mut q_pos {
for (pos, mut v, gforce_maybe) in &mut q_pos {
let relative_pos = pos.0 - jupiter_pos.0;
let accel = nature::gravitational_acceleration(relative_pos, nature::JUPITER_MASS);
let accel = dt * nature::gravitational_acceleration(relative_pos, nature::JUPITER_MASS);
if let Some(mut gforce) = gforce_maybe {
gforce.gravitational_component += accel;
}
v.0 += accel;
}
}