From 0ba87de5533795cf198070b575b7a4382483eb15 Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 11 Jul 2024 19:19:27 +0200 Subject: [PATCH] Make CanvasController sole owner of planet vector --- src/canvas.rs | 34 ++++++++++++++++++++++++++- src/gravity.rs | 19 +++++++-------- src/main.rs | 63 ++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 87 insertions(+), 29 deletions(-) diff --git a/src/canvas.rs b/src/canvas.rs index 427e990..3fcb9e4 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,19 +1,25 @@ use opengl_graphics::GlGraphics; use piston::RenderArgs; -use crate::planet::Planet; +use crate::{gravity::GravityCalculator, planet::Planet}; pub const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0]; pub const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0]; pub const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0]; pub const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0]; pub const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0]; +pub const PURPLE: [f32; 4] = [1.0, 0.0, 1.0, 1.0]; +pub const YELLOW: [f32; 4] = [1.0, 1.0, 0.0, 1.0]; +pub const CYAN: [f32; 4] = [0.0, 1.0, 1.0, 1.0]; pub struct Canvas { pub gl: GlGraphics, } impl Canvas { + fn new(gl: GlGraphics) -> Self { + return Canvas { gl }; + } pub fn render(&mut self, planets: &Vec, args: &RenderArgs) { use graphics::*; @@ -35,3 +41,29 @@ impl Canvas { }); } } + +pub struct CanvasContoller { + planets: Vec, + canvas: Canvas, + calculator: GravityCalculator, +} + +impl CanvasContoller { + pub fn new(gl: GlGraphics, planets: Vec) -> Self { + return CanvasContoller { + planets, + canvas: Canvas::new(gl), + calculator: GravityCalculator::new(), + }; + } + + pub fn update(&mut self, dt: f64) { + self.calculator + .calculate_forces_newtonian(&mut self.planets); + self.calculator.update_positions(&mut self.planets, dt); + } + + pub fn render(&mut self, args: RenderArgs) { + self.canvas.render(&mut self.planets, &args) + } +} diff --git a/src/gravity.rs b/src/gravity.rs index c022253..aa87713 100644 --- a/src/gravity.rs +++ b/src/gravity.rs @@ -1,20 +1,17 @@ use crate::planet::Planet; const GRAVITATIONAL_CONTANT: f64 = 6.67430e-11; -#[derive(Clone)] -pub struct GravityCalculator { - pub planets: Vec, -} +pub struct GravityCalculator {} impl GravityCalculator { - pub fn new(planets: Vec) -> Self { - return GravityCalculator { planets }; + pub fn new() -> Self { + return GravityCalculator {}; } - pub fn calculate_forces_newtonian(&mut self) { - let orig_set_of_planets = self.planets.clone(); + pub fn calculate_forces_newtonian(&mut self, planets: &mut Vec) { + let orig_set_of_planets = planets.clone(); - for this_planet in self.planets.iter_mut() { + for this_planet in planets.iter_mut() { let mut sum_of_forces: Vec = vec![0.0, 0.0]; for other_planet in orig_set_of_planets.iter() { @@ -31,8 +28,8 @@ impl GravityCalculator { } } - pub fn update_positions(&mut self, dt: f64) { - for this_planet in self.planets.iter_mut() { + pub fn update_positions(&mut self, planets: &mut Vec, dt: f64) { + for this_planet in planets.iter_mut() { let x = this_planet.pos[0]; let y = this_planet.pos[1]; let fx = this_planet.force_affecting[0]; diff --git a/src/main.rs b/src/main.rs index ba0cd5b..c3002d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,11 +8,13 @@ mod gravity; mod physics; mod planet; +use canvas::CanvasContoller; use glutin_window::GlutinWindow as Window; use opengl_graphics::{GlGraphics, OpenGL}; use piston::event_loop::{EventSettings, Events}; use piston::input::{RenderEvent, UpdateEvent}; use piston::window::WindowSettings; +use piston::{ButtonEvent, MouseCursorEvent}; fn main() { let opengl = OpenGL::V4_5; @@ -26,44 +28,71 @@ fn main() { let planets = vec![ planet::PlanetBuilder::new() .with_name(String::from("Earth")) - .with_mass(100000.0) + .with_mass(20000000.0) .with_radius(10.0) .with_positsion(0.0, 0.00) - .with_color(canvas::BLUE) + .with_color(canvas::BLACK) .build(), planet::PlanetBuilder::new() .with_name(String::from("Moon I")) - .with_velocity(vec![0.0, -0.0004]) - .with_positsion(50.0, 0.0) - .with_mass(1000.0) + .with_velocity(vec![0.005, 0.0]) + .with_positsion(0.0, 60.0) + .with_mass(10000.0) .with_radius(5.0) .with_color(canvas::GREEN) .build(), planet::PlanetBuilder::new() - .with_name(String::from("Moon II")) - .with_velocity(vec![0.0004, 0.0]) - .with_positsion(0.0, 50.0) - .with_mass(100.0) + .with_name(String::from("Moon I")) + .with_velocity(vec![0.005, 0.0]) + .with_positsion(0.0, 55.0) + .with_mass(10000.0) .with_radius(5.0) .with_color(canvas::RED) .build(), + planet::PlanetBuilder::new() + .with_name(String::from("Moon I")) + .with_velocity(vec![0.005, 0.0]) + .with_positsion(0.0, 45.0) + .with_mass(10000.0) + .with_radius(5.0) + .with_color(canvas::BLUE) + .build(), + planet::PlanetBuilder::new() + .with_name(String::from("Moon I")) + .with_velocity(vec![0.0065, 0.0]) + .with_positsion(0.0, 50.0) + .with_mass(10000.0) + .with_radius(5.0) + .with_color(canvas::PURPLE) + .build(), + planet::PlanetBuilder::new() + .with_name(String::from("Moon II")) + .with_velocity(vec![-0.0008, 0.0]) + .with_positsion(0.0, -400.0) + .with_mass(1000.0) + .with_radius(5.0) + .with_color(canvas::CYAN) + .build(), ]; - let mut canvas = canvas::Canvas { - gl: GlGraphics::new(opengl), - }; - - let mut calculator = gravity::GravityCalculator::new(planets); + let mut canvas_controller = CanvasContoller::new(GlGraphics::new(opengl), planets); let mut events = Events::new(EventSettings::new()); while let Some(e) = events.next(&mut window) { if let Some(args) = e.render_args() { - canvas.render(&calculator.planets.clone(), &args); + canvas_controller.render(args); } if let Some(args) = e.update_args() { - calculator.calculate_forces_newtonian(); - calculator.update_positions(100000.0 * args.dt); + canvas_controller.update(25000.0 * args.dt); + } + + if let Some(args) = e.mouse_cursor_args() { + println!("Mouse cursor event with args: {:?}", args); + } + + if let Some(args) = e.button_args() { + println!("Button event with args: {:?}", args); } } }