This commit is contained in:
Leon Liu 2025-08-14 16:52:35 +09:00
parent b992a1dd5f
commit c798cc8547

View File

@ -3,7 +3,7 @@ use bevy::{
input::mouse::{MouseScrollUnit, MouseWheel},
math::DVec3,
prelude::*,
window::{WindowMode, WindowResolution},
window::{WindowMode, WindowResized, WindowResolution},
};
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
use iyes_perf_ui::{PerfUiPlugin, prelude::*};
@ -206,7 +206,6 @@ fn sync_name_labels(
mut commands: Commands,
objects_with_names: Query<(Entity, &Name, &Position), Changed<Name>>,
existing_labels: Query<&ObjectLabel>,
ui_scale: Res<UiScale>,
) {
for (entity, name, _position) in objects_with_names.iter() {
// Check if label already exists for this entity
@ -243,7 +242,6 @@ fn update_label_positions(
mut label_query: Query<(&mut Node, &ObjectLabel, &mut Text), With<ObjectLabel>>,
objects_query: Query<(&GlobalTransform, &Name, &Radius)>,
camera_query: Query<(&Camera, &GlobalTransform)>,
ui_scale: Res<UiScale>,
) {
let Ok((camera, camera_transform)) = camera_query.single() else {
return;
@ -491,11 +489,10 @@ fn n_body(mut query: Query<(&Mass, &mut Position, &mut Velocity)>) {
.map(|(mass, pos, vel)| (mass.0.0, pos.0.0, vel.0.0))
.collect();
// Perform integration steps
for _ in 0..STEPS {
// Calculate forces for all bodies
// Helper function to calculate gravitational forces
let calculate_forces = |bodies: &Vec<(f64, DVec3, DVec3)>| -> Vec<DVec3> {
let mut forces: Vec<DVec3> = vec![DVec3::ZERO; bodies.len()];
for i in 0..bodies.len() {
for j in 0..bodies.len() {
if i != j {
@ -504,26 +501,36 @@ fn n_body(mut query: Query<(&Mass, &mut Position, &mut Velocity)>) {
if r_mag_au > 1e-10 {
// Avoid division by zero
// Calculate force directly in AU/day² units
let force_magnitude =
G_AU * bodies[i].0 * bodies[j].0 / (r_mag_au * r_mag_au);
let acceleration = force_magnitude / bodies[i].0; // F/m = a
let force_vec = r_vec.normalize() * acceleration;
forces[i] += force_vec;
// Calculate gravitational acceleration in AU/day² units
let acceleration_magnitude = G_AU * bodies[j].0 / (r_mag_au * r_mag_au);
let acceleration_vec = r_vec.normalize() * acceleration_magnitude;
forces[i] += acceleration_vec;
}
}
}
}
forces
};
// Update velocities and positions using Verlet integration
// Perform integration steps using Leapfrog (Verlet) symplectic integrator
for _ in 0..STEPS {
// Step 1: Half-step momentum update - p ← p - (h/2)∇U(q)
let forces = calculate_forces(&bodies);
for i in 0..bodies.len() {
// Update velocity: v += a * dt
bodies[i].2 += forces[i] * DT;
bodies[i].2 += forces[i] * (DT * 0.5); // Update velocity (momentum/mass)
}
// Update position: x += v * dt
// Step 2: Full-step position update - q ← q + h∇T(p)
// For classical mechanics, ∇T(p) = p/m = v (velocity)
for i in 0..bodies.len() {
let velocity = bodies[i].2;
bodies[i].1 += velocity * DT;
bodies[i].1 += velocity * DT; // Update position
}
// Step 3: Half-step momentum update - p ← p - (h/2)∇U(q)
let forces = calculate_forces(&bodies);
for i in 0..bodies.len() {
bodies[i].2 += forces[i] * (DT * 0.5); // Update velocity again
}
}