use bevy::math::Vec3Swizzles; use bevy::{prelude::*, color::palettes::css}; const TICK_RATE: f64 = 1.0 / 60.0; const VISUAL_SLOWDOWN: f64 = 1.0; const MIN_DAMP_RATIO: f32 = 0.00055; fn main() { App::new() .insert_resource(ClearColor(css::DARK_GRAY.into())) .insert_resource(Msaa::default()) .add_plugins(DefaultPlugins) //.add_plugins(bevy_editor_pls::EditorPlugin) .add_systems(Startup, (setup, setup_graphics)) .add_systems( Update, (spring_impulse, gravity, symplectic_euler,).chain() ) .register_type::() .register_type::() .register_type::() .register_type::() .register_type::() .run(); } fn setup_graphics(mut commands: Commands) { commands.spawn_bundle(Camera2dBundle { transform: Transform::from_xyz(0.0, 300.0, 0.0), ..default() }); } #[derive(Debug, Copy, Clone, Component)] pub struct Spring { pub containing: Entity, } #[derive(Default, Debug, Copy, Clone, Component, Reflect)] #[reflect(Component)] pub struct SpringSettings(springy::Spring); #[derive(Default, Debug, Copy, Clone, Component, Reflect)] #[reflect(Component)] pub struct Velocity(Vec2); #[derive(Default, Debug, Copy, Clone, Component, Reflect)] #[reflect(Component)] pub struct Impulse(Vec2); #[derive(Debug, Copy, Clone, Component, Reflect)] #[reflect(Component)] pub struct Mass(pub f32); impl Default for Mass { fn default() -> Self { Self(1.0) } } impl Mass { pub fn inverse_mass(&self) -> f32 { if self.0 != 0.0 { 1.0 / self.0 } else { 0.0 } } } #[derive(Debug, Copy, Clone, Component, Reflect)] #[reflect(Component)] pub struct Gravity(pub Vec2); impl Default for Gravity { fn default() -> Self { Self(Vec2::new(0.0, -9.817)) } } /// Basic symplectic euler integration of the impulse/velocity/position. pub fn symplectic_euler( time: Res