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 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<Planet>, args: &RenderArgs) {
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;
const GRAVITATIONAL_CONTANT: f64 = 6.67430e-11;
#[derive(Clone)]
pub struct GravityCalculator {
pub planets: Vec<Planet>,
}
pub struct GravityCalculator {}
impl GravityCalculator {
pub fn new(planets: Vec<Planet>) -> 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<Planet>) {
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];
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<Planet>, 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];

View File

@ -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);
}
}
}