From 7b4075d7cf1815c610b5d77dd57efde302780e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Eng=C3=A9libert?= Date: Wed, 3 Jan 2024 17:43:54 +0100 Subject: [PATCH] Mesh as child --- README.md | 11 ++++++ src/gen.rs | 2 +- src/main.rs | 110 ++++++++++++++++++++++++++++++++-------------------- 3 files changed, 81 insertions(+), 42 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..ac80a7e --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +## 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 diff --git a/src/gen.rs b/src/gen.rs index 32243d7..4de5e64 100644 --- a/src/gen.rs +++ b/src/gen.rs @@ -17,7 +17,7 @@ pub fn planet() -> Mesh { let a = std::f32::consts::TAU * i as f32 / perimeter as f32; [r * a.cos(), r * a.sin(), 0.] }) - .chain([[0., 0., 0.]].into_iter()) + .chain([[0., 0., 0.]]) .collect::>(), ); let mut triangles = Vec::new(); diff --git a/src/main.rs b/src/main.rs index 097c611..2ee5d7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,11 @@ mod gen; mod quadtree; -use bevy::{ecs::query::BatchingStrategy, prelude::*, sprite::MaterialMesh2dBundle}; +use bevy::{ + ecs::query::BatchingStrategy, + prelude::*, + sprite::{MaterialMesh2dBundle, Mesh2dHandle}, +}; #[global_allocator] static ALLOCATOR: cap::Cap = @@ -38,47 +42,70 @@ fn setup( .spawn(Camera2dBundle::default()) .insert(bevy_pancam::PanCam::default()); - commands.spawn(Planet { - mass: Mass(1.988e18), - speed: Speed(Vec2::new(0.0, 0.0)), - mesh: MaterialMesh2dBundle { - mesh: meshes.add(gen::planet()).into(), - material: materials.add(ColorMaterial::from(Color::YELLOW)), - transform: Transform::from_translation(Vec3::new(0., 0., 0.)), - ..default() - }, - }); - commands.spawn(Planet { - mass: Mass(5.9736e14), - speed: Speed(Vec2::new(0.0, 500.0)), - mesh: MaterialMesh2dBundle { - mesh: meshes.add(shape::Circle::new(10.).into()).into(), - material: materials.add(ColorMaterial::from(Color::BLUE)), - transform: Transform::from_translation(Vec3::new(400., 0., 0.)), - ..default() - }, - }); - commands.spawn(Planet { - mass: Mass(5.9736e14), - speed: Speed(Vec2::new(0.0, -500.0)), - mesh: MaterialMesh2dBundle { - mesh: meshes.add(shape::Circle::new(10.).into()).into(), - material: materials.add(ColorMaterial::from(Color::BLUE)), - transform: Transform::from_translation(Vec3::new(-400., 0., 0.)), - ..default() - }, - }); - for i in 0..4000u32 { - commands.spawn(Planet { - mass: Mass(1.), - speed: Speed(Vec2::new(0.0, -500.0)), - mesh: MaterialMesh2dBundle { - mesh: meshes.add(shape::Circle::new(5.).into()).into(), - material: materials.add(ColorMaterial::from(Color::RED)), - transform: Transform::from_translation(Vec3::new(-450. - i as f32 / 4., 0., 0.)), + let red = materials.add(ColorMaterial::from(Color::RED)); + let circle_5: Mesh2dHandle = meshes.add(shape::Circle::new(5.).into()).into(); + + commands + .spawn(Planet { + 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)), + ..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() + }); + }); } } @@ -96,9 +123,10 @@ struct Mass(f32); #[derive(Bundle)] struct Planet { + pos: TransformBundle, mass: Mass, speed: Speed, - mesh: MaterialMesh2dBundle, + visibility: InheritedVisibility, } #[derive(Resource)]