use std::time::Duration; use bevy::math::Vec3Swizzles; //use bevy::time::FixedTimestep; use bevy::{prelude::*, color::palettes::css}; use bevy_framepace::{FramepaceSettings, Limiter}; const TICK_RATE: f64 = 1.0 / 60.0; const VISUAL_SLOWDOWN: f64 = 1.0; fn main() { App::new() .insert_resource(ClearColor(css::DARK_GRAY.into())) .insert_resource(Msaa::default()) .add_plugins(DefaultPlugins) //.add_plugin(bevy_editor_pls::EditorPlugin::new()) .add_plugins(bevy_inspector_egui::quick::WorldInspectorPlugin::default()) .insert_resource(FramepaceSettings { limiter: Limiter::Manual(Duration::from_secs_f64(TICK_RATE)), ..default() }) .add_systems(Startup, (setup_graphics, setup_rope, setup_translation, setup_rotational,) ) .add_systems(PostUpdate, ( 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(Camera2dBundle { camera: Camera { is_active: true, ..default() }, transform: Transform::from_xyz(0.0, 300.0, 5.0), ..default() }).insert(Name::new("Camera")); } #[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 { pub linear: Vec2, pub angular: f32, } #[derive(Default, Debug, Copy, Clone, Component, Reflect)] #[reflect(Component)] pub struct Impulse { pub linear: Vec2, pub angular: f32, } #[derive(Debug, Copy, Clone, Component, Reflect)] #[reflect(Component)] pub struct Inertia { pub linear: f32, pub angular: f32, } impl Default for Inertia { fn default() -> Self { Self { linear: 1.0, angular: 0.05, } } } impl Inertia { pub const INFINITY: Self = Inertia { linear: f32::INFINITY, angular: f32::INFINITY, }; pub fn inverse_linear(&self) -> f32 { if self.linear.is_normal() { 1.0 / self.linear } else { 0.0 } } pub fn inverse_angular(&self) -> f32 { if self.angular.is_normal() { 1.0 / self.angular } 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