From 3affe0195b6373f099431c7908d60d4e1f3fc008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Eng=C3=A9libert?= Date: Wed, 3 Jan 2024 18:54:25 +0100 Subject: [PATCH] =?UTF-8?q?Code=20de=20d=C3=A9part?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 36 +++++----- src/gen.rs | 22 ++---- src/main.rs | 176 +++--------------------------------------------- src/quadtree.rs | 160 ------------------------------------------- 4 files changed, 34 insertions(+), 360 deletions(-) delete mode 100644 src/quadtree.rs diff --git a/README.md b/README.md index 44b1874..aef82d8 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,19 @@ -## Feuille de route -* Créer deuxième planète -* Gravité naïve - * Vitesse - * Poids -* Faire un système solaire (trouver valeurs) -* Quadtree -* Réutiliser les mêmes Mesh et Material (augmente FPS en dézoom) -* Générer un mesh avec noise -* Générer plusieurs meshes -* (?) Collision - * Détection - * Double boucle naïve - * Intégré au quadtree - * Fusion simple - * Fragmentation - * Cratère +# Stage JSB : Simulation spatiale + +## Aide + +[Documentation de Bevy](https://docs.rs/bevy/latest/bevy/) + +## Commandes + +Vérifier la validité du code : + + cargo check + +Compiler et lancer le programme : + + cargo run + +Compiler et lancer le programme optimisé (plus lent à compiler, mais plus léger et rapide à exécuter) : + + cargo run --release diff --git a/src/gen.rs b/src/gen.rs index 4de5e64..ee57f63 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -2,28 +2,14 @@ use bevy::{prelude::*, render::render_resource::PrimitiveTopology}; use opensimplex_noise_rs::OpenSimplexNoise; use rand::Rng; -pub fn planet() -> Mesh { +pub fn _planet() -> Mesh { let mut rng = rand::thread_rng(); - let simplex = OpenSimplexNoise::new(Some(rng.gen())); + let _simplex = OpenSimplexNoise::new(Some(rng.gen())); let mut mesh = Mesh::new(PrimitiveTopology::TriangleList); - let perimeter: u32 = 1000; mesh.insert_attribute( Mesh::ATTRIBUTE_POSITION, - (0..perimeter) - .map(|i| { - let mut r = simplex.eval_2d(i as f64 * 0.02, 0.) as f32 * 20.0 + 100.0; - r += simplex.eval_2d(i as f64 * 0.05, 10.) as f32 * 10.0; - r += simplex.eval_2d(i as f64 * 0.2, 10.) as f32 * 4.0; - let a = std::f32::consts::TAU * i as f32 / perimeter as f32; - [r * a.cos(), r * a.sin(), 0.] - }) - .chain([[0., 0., 0.]]) - .collect::>(), + vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], ); - let mut triangles = Vec::new(); - for i in 0..perimeter { - triangles.extend_from_slice(&[i, perimeter, (i + 1) % perimeter]); - } - mesh.set_indices(Some(bevy::render::mesh::Indices::U32(triangles))); + mesh.set_indices(Some(bevy::render::mesh::Indices::U32(vec![0, 1, 2]))); mesh } diff --git a/src/main.rs b/src/main.rs index 2ee5d7e..8ec9ad3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,11 @@ mod gen; -mod quadtree; -use bevy::{ - ecs::query::BatchingStrategy, - prelude::*, - sprite::{MaterialMesh2dBundle, Mesh2dHandle}, -}; +use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; #[global_allocator] static ALLOCATOR: cap::Cap = cap::Cap::new(std::alloc::System, 1024 * 1024 * 1024); -static UNIVERSE_POS: (Vec2, Vec2) = (Vec2::new(-1e6, -1e6), Vec2::new(1e6, 1e6)); - fn main() { App::new() .add_plugins(( @@ -20,16 +13,9 @@ fn main() { bevy_fps_counter::FpsCounterPlugin, bevy_pancam::PanCamPlugin, )) - .insert_resource(Constants { g: 6.674e-11 }) .add_systems(Startup, setup) - .configure_sets(Update, (Set::Force, Set::Apply).chain()) - .add_systems( - Update, - ( - weight_system.in_set(Set::Force), - apply_system.in_set(Set::Apply), - ), - ) + .configure_sets(Update, (Set::Demo).chain()) + .add_systems(Update, (demo_system.in_set(Set::Demo),)) .run(); } @@ -42,174 +28,34 @@ fn setup( .spawn(Camera2dBundle::default()) .insert(bevy_pancam::PanCam::default()); - let red = materials.add(ColorMaterial::from(Color::RED)); - let circle_5: Mesh2dHandle = meshes.add(shape::Circle::new(5.).into()).into(); - commands - .spawn(Planet { + .spawn(Ball { pos: TransformBundle::from(Transform::from_translation(Vec3::new(0., 0., 0.))), - mass: Mass(1.988e18), - speed: Speed(Vec2::new(0.0, 0.0)), - visibility: InheritedVisibility::VISIBLE, - }) - .with_children(|parent| { - parent.spawn(MaterialMesh2dBundle { - mesh: meshes.add(gen::planet()).into(), - material: materials.add(ColorMaterial::from(Color::YELLOW)), - ..default() - }); - }); - commands - .spawn(Planet { - pos: TransformBundle::from(Transform::from_translation(Vec3::new(400., 0., 0.))), - mass: Mass(5.9736e14), - speed: Speed(Vec2::new(0.0, 500.0)), visibility: InheritedVisibility::VISIBLE, }) .with_children(|parent| { parent.spawn(MaterialMesh2dBundle { mesh: meshes.add(shape::Circle::new(10.).into()).into(), - material: materials.add(ColorMaterial::from(Color::BLUE)), + material: materials.add(ColorMaterial::from(Color::ORANGE)), ..default() }); }); - commands - .spawn(Planet { - pos: TransformBundle::from(Transform::from_translation(Vec3::new(-400., 0., 0.))), - mass: Mass(5.9736e14), - speed: Speed(Vec2::new(0.0, -500.0)), - visibility: InheritedVisibility::VISIBLE, - }) - .with_children(|parent| { - parent.spawn(MaterialMesh2dBundle { - mesh: meshes.add(shape::Circle::new(10.).into()).into(), - material: materials.add(ColorMaterial::from(Color::BLUE)), - ..default() - }); - }); - for i in 0..4000u32 { - commands - .spawn(Planet { - pos: TransformBundle::from(Transform::from_translation(Vec3::new( - -450. - i as f32 / 4., - 0., - 0., - ))), - mass: Mass(1.), - speed: Speed(Vec2::new(0.0, -500.0)), - visibility: InheritedVisibility::VISIBLE, - }) - .with_children(|parent| { - parent.spawn(MaterialMesh2dBundle { - mesh: circle_5.clone(), - material: red.clone(), - ..default() - }); - }); - } } #[derive(Clone, Debug, Eq, Hash, PartialEq, SystemSet)] enum Set { - Force, - Apply, + Demo, } -#[derive(Component)] -struct Speed(Vec2); - -#[derive(Component)] -struct Mass(f32); - #[derive(Bundle)] -struct Planet { +struct Ball { pos: TransformBundle, - mass: Mass, - speed: Speed, visibility: InheritedVisibility, } -#[derive(Resource)] -struct Constants { - g: f32, -} - -struct Body { - mass: f32, - pos: Vec2, -} - -impl quadtree::Body for Body { - fn mass(&self) -> f32 { - self.mass - } - fn pos(&self) -> Vec2 { - self.pos - } - fn add_mass(&mut self, mass: f32) { - self.mass += mass; +fn demo_system(query: Query<&Transform>, time: Res