From c798cc8547b59b57fdae106d82bb084ca9ac7d73 Mon Sep 17 00:00:00 2001 From: Leon Liu Date: Thu, 14 Aug 2025 16:52:35 +0900 Subject: [PATCH] update --- src/main.rs | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index a6fa09e..02b16b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>, existing_labels: Query<&ObjectLabel>, - ui_scale: Res, ) { 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>, objects_query: Query<(&GlobalTransform, &Name, &Radius)>, camera_query: Query<(&Camera, &GlobalTransform)>, - ui_scale: Res, ) { 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 { let mut forces: Vec = 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 } }