Make CanvasController sole owner of planet vector

This commit is contained in:
Marco 2024-07-11 19:19:27 +02:00
parent 43b6290f4a
commit 0ba87de553
3 changed files with 87 additions and 29 deletions

View File

@ -1,19 +1,25 @@
use opengl_graphics::GlGraphics; use opengl_graphics::GlGraphics;
use piston::RenderArgs; 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 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 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 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 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 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 struct Canvas {
pub gl: GlGraphics, pub gl: GlGraphics,
} }
impl Canvas { impl Canvas {
fn new(gl: GlGraphics) -> Self {
return Canvas { gl };
}
pub fn render(&mut self, planets: &Vec<Planet>, args: &RenderArgs) { pub fn render(&mut self, planets: &Vec<Planet>, args: &RenderArgs) {
use graphics::*; use graphics::*;
@ -35,3 +41,29 @@ impl Canvas {
}); });
} }
} }
pub struct CanvasContoller {
planets: Vec<Planet>,
canvas: Canvas,
calculator: GravityCalculator,
}
impl CanvasContoller {
pub fn new(gl: GlGraphics, planets: Vec<Planet>) -> 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)
}
}

View File

@ -1,20 +1,17 @@
use crate::planet::Planet; use crate::planet::Planet;
const GRAVITATIONAL_CONTANT: f64 = 6.67430e-11; const GRAVITATIONAL_CONTANT: f64 = 6.67430e-11;
#[derive(Clone)] pub struct GravityCalculator {}
pub struct GravityCalculator {
pub planets: Vec<Planet>,
}
impl GravityCalculator { impl GravityCalculator {
pub fn new(planets: Vec<Planet>) -> Self { pub fn new() -> Self {
return GravityCalculator { planets }; return GravityCalculator {};
} }
pub fn calculate_forces_newtonian(&mut self) { pub fn calculate_forces_newtonian(&mut self, planets: &mut Vec<Planet>) {
let orig_set_of_planets = self.planets.clone(); 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<f64> = vec![0.0, 0.0]; let mut sum_of_forces: Vec<f64> = vec![0.0, 0.0];
for other_planet in orig_set_of_planets.iter() { for other_planet in orig_set_of_planets.iter() {
@ -31,8 +28,8 @@ impl GravityCalculator {
} }
} }
pub fn update_positions(&mut self, dt: f64) { pub fn update_positions(&mut self, planets: &mut Vec<Planet>, dt: f64) {
for this_planet in self.planets.iter_mut() { for this_planet in planets.iter_mut() {
let x = this_planet.pos[0]; let x = this_planet.pos[0];
let y = this_planet.pos[1]; let y = this_planet.pos[1];
let fx = this_planet.force_affecting[0]; let fx = this_planet.force_affecting[0];

View File

@ -8,11 +8,13 @@ mod gravity;
mod physics; mod physics;
mod planet; mod planet;
use canvas::CanvasContoller;
use glutin_window::GlutinWindow as Window; use glutin_window::GlutinWindow as Window;
use opengl_graphics::{GlGraphics, OpenGL}; use opengl_graphics::{GlGraphics, OpenGL};
use piston::event_loop::{EventSettings, Events}; use piston::event_loop::{EventSettings, Events};
use piston::input::{RenderEvent, UpdateEvent}; use piston::input::{RenderEvent, UpdateEvent};
use piston::window::WindowSettings; use piston::window::WindowSettings;
use piston::{ButtonEvent, MouseCursorEvent};
fn main() { fn main() {
let opengl = OpenGL::V4_5; let opengl = OpenGL::V4_5;
@ -26,44 +28,71 @@ fn main() {
let planets = vec![ let planets = vec![
planet::PlanetBuilder::new() planet::PlanetBuilder::new()
.with_name(String::from("Earth")) .with_name(String::from("Earth"))
.with_mass(100000.0) .with_mass(20000000.0)
.with_radius(10.0) .with_radius(10.0)
.with_positsion(0.0, 0.00) .with_positsion(0.0, 0.00)
.with_color(canvas::BLUE) .with_color(canvas::BLACK)
.build(), .build(),
planet::PlanetBuilder::new() planet::PlanetBuilder::new()
.with_name(String::from("Moon I")) .with_name(String::from("Moon I"))
.with_velocity(vec![0.0, -0.0004]) .with_velocity(vec![0.005, 0.0])
.with_positsion(50.0, 0.0) .with_positsion(0.0, 60.0)
.with_mass(1000.0) .with_mass(10000.0)
.with_radius(5.0) .with_radius(5.0)
.with_color(canvas::GREEN) .with_color(canvas::GREEN)
.build(), .build(),
planet::PlanetBuilder::new() planet::PlanetBuilder::new()
.with_name(String::from("Moon II")) .with_name(String::from("Moon I"))
.with_velocity(vec![0.0004, 0.0]) .with_velocity(vec![0.005, 0.0])
.with_positsion(0.0, 50.0) .with_positsion(0.0, 55.0)
.with_mass(100.0) .with_mass(10000.0)
.with_radius(5.0) .with_radius(5.0)
.with_color(canvas::RED) .with_color(canvas::RED)
.build(), .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 { let mut canvas_controller = CanvasContoller::new(GlGraphics::new(opengl), planets);
gl: GlGraphics::new(opengl),
};
let mut calculator = gravity::GravityCalculator::new(planets);
let mut events = Events::new(EventSettings::new()); let mut events = Events::new(EventSettings::new());
while let Some(e) = events.next(&mut window) { while let Some(e) = events.next(&mut window) {
if let Some(args) = e.render_args() { if let Some(args) = e.render_args() {
canvas.render(&calculator.planets.clone(), &args); canvas_controller.render(args);
} }
if let Some(args) = e.update_args() { if let Some(args) = e.update_args() {
calculator.calculate_forces_newtonian(); canvas_controller.update(25000.0 * args.dt);
calculator.update_positions(100000.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);
} }
} }
} }