Last active
October 11, 2023 18:30
-
-
Save jan-tennert/37cb17b5e260aceefac5d94fef92205c to your computer and use it in GitHub Desktop.
asdasdasdsa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn update_position( | |
mut query: Query<(Entity, &Mass, &mut Acceleration, &mut Velocity, &mut SimPosition, &mut Transform)>, | |
time: Res<Time>, | |
speed: Res<Speed>, | |
selected_entity: Res<SelectedEntity>, | |
) { | |
let delta = time.delta_seconds() / 10.0; | |
for _ in 0..9 { | |
let mut other_bodies: Vec<(&Mass, Mut<Acceleration>, Mut<SimPosition>)> = Vec::new(); | |
for (_, mass, mut acc, _, sim_pos, _) in query.iter_mut() { | |
acc.0 = Vec3::ZERO; | |
for (other_mass, ref mut other_acc, other_sim_pos) in other_bodies.iter_mut() { | |
let r_sq = (sim_pos.0 - other_sim_pos.0).length_squared() as f64; | |
let force_direction = (other_sim_pos.0 - sim_pos.0).normalize(); // Calculate the direction vector | |
let force_magnitude = G * mass.0 * other_mass.0 / r_sq; | |
// println!("r_sq: {}", G * mass.0 * other_mass.0 / r_sq); | |
let force = force_direction * (force_magnitude as f32); | |
// println!("force: {}", force); | |
acc.0 += force; | |
other_acc.0 -= force; | |
} | |
other_bodies.push((mass, acc, sim_pos)); | |
} | |
for (_, mass, mut acc, _, _, _) in query.iter_mut() { | |
acc.0 /= mass.0 as f32; | |
} | |
//Velocity | |
for (_, _, acc, mut vel, _, _) in query.iter_mut() { | |
vel.0 += acc.0 * delta * speed.0 ; | |
} | |
//New position | |
let delta_time = delta; | |
// Calculate the offset based on the selected entity's position | |
let offset = match selected_entity.0 { | |
Some(selected) => { | |
if let Ok((_, _, _, vel, mut sim_pos, mut transform)) = query.get_mut(selected) { | |
sim_pos.0 += vel.0 * delta_time * speed.0; //this is the same step as below, but we are doing this first for the offset | |
let raw_translation = sim_pos.0 * KM_TO_AU; | |
transform.translation = Vec3::ZERO; //the selected entity will always be at 0,0,0 | |
-raw_translation | |
} else { | |
Vec3::ZERO | |
} | |
} | |
None => Vec3::ZERO, | |
}; | |
for (entity, _, _, vel, mut sim_pos, mut transform) in query.iter_mut() { | |
if let Some(s_entity) = selected_entity.0 { | |
if s_entity == entity { | |
continue; | |
} | |
} | |
sim_pos.0 += vel.0 * delta_time * speed.0; | |
transform.translation = (sim_pos.0 * KM_TO_AU) + offset; //apply offset | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment