From 1d426f0b29104e972d4be31ddc6e232d4b8f4f6e Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 17 Oct 2026 18:45:17 +0100 Subject: [PATCH] Create animation --- src/bin/anim.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/bin/anim.rs diff --git a/src/bin/anim.rs b/src/bin/anim.rs new file mode 100644 index 0000000..f49012c --- /dev/null +++ b/src/bin/anim.rs @@ -0,0 +1,44 @@ +use symplectic_double_pendulum::*; + +use macroquad::prelude::*; + +fn draw(dp: &DoublePendulum, fg: Color) { + let rod_length = 120.0; + + let pivot = vec2(screen_width() / 2.0, screen_height() / 2.0); + let bob1 = vec2( + pivot.x + rod_length * (dp.theta.sin() as f32), + pivot.y + rod_length * (dp.theta.cos() as f32)); + let bob2 = vec2( + bob1.x + rod_length * ((dp.theta + dp.phi).sin() as f32), + bob1.y + rod_length * ((dp.theta + dp.phi).cos() as f32)); + + draw_line(pivot.x, pivot.y, bob1.x, bob1.y, 2.0, fg); + draw_line(bob1.x, bob1.y, bob2.x, bob2.y, 2.0, fg); + + draw_circle(bob1.x, bob1.y, 8.0, fg); + draw_circle(bob2.x, bob2.y, 8.0, fg); +} + +#[macroquad::main("Double Pendulum")] +async fn main() { + let omega = 500.0; + let h = 1e-3; + + let mut state = Augmented::from(State { + q: DoublePendulum { theta: std::f64::consts::PI - 0.01, phi: 0.0 }, + p: DoublePendulum { theta: 0.0, phi: 0.0 }, + }, omega); + + loop { + clear_background(WHITE); + + let t = get_frame_time() as f64; + for _ in 0..((t / h) as i32) { + state.evolve(h); + } + draw(&state.average().q, BLACK); + + next_frame().await + } +}