Mesh as child

This commit is contained in:
Pascal Engélibert 2024-01-03 17:43:54 +01:00
parent 8fc0c391ca
commit 7b4075d7cf
3 changed files with 81 additions and 42 deletions

11
README.md Normal file
View file

@ -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

View file

@ -17,7 +17,7 @@ pub fn planet() -> Mesh {
let a = std::f32::consts::TAU * i as f32 / perimeter as f32; let a = std::f32::consts::TAU * i as f32 / perimeter as f32;
[r * a.cos(), r * a.sin(), 0.] [r * a.cos(), r * a.sin(), 0.]
}) })
.chain([[0., 0., 0.]].into_iter()) .chain([[0., 0., 0.]])
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
); );
let mut triangles = Vec::new(); let mut triangles = Vec::new();

View file

@ -1,7 +1,11 @@
mod gen; mod gen;
mod quadtree; mod quadtree;
use bevy::{ecs::query::BatchingStrategy, prelude::*, sprite::MaterialMesh2dBundle}; use bevy::{
ecs::query::BatchingStrategy,
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
#[global_allocator] #[global_allocator]
static ALLOCATOR: cap::Cap<std::alloc::System> = static ALLOCATOR: cap::Cap<std::alloc::System> =
@ -38,47 +42,70 @@ fn setup(
.spawn(Camera2dBundle::default()) .spawn(Camera2dBundle::default())
.insert(bevy_pancam::PanCam::default()); .insert(bevy_pancam::PanCam::default());
commands.spawn(Planet { let red = materials.add(ColorMaterial::from(Color::RED));
mass: Mass(1.988e18), let circle_5: Mesh2dHandle = meshes.add(shape::Circle::new(5.).into()).into();
speed: Speed(Vec2::new(0.0, 0.0)),
mesh: MaterialMesh2dBundle { commands
mesh: meshes.add(gen::planet()).into(), .spawn(Planet {
material: materials.add(ColorMaterial::from(Color::YELLOW)), pos: TransformBundle::from(Transform::from_translation(Vec3::new(0., 0., 0.))),
transform: Transform::from_translation(Vec3::new(0., 0., 0.)), mass: Mass(1.988e18),
..default() speed: Speed(Vec2::new(0.0, 0.0)),
}, visibility: InheritedVisibility::VISIBLE,
}); })
commands.spawn(Planet { .with_children(|parent| {
mass: Mass(5.9736e14), parent.spawn(MaterialMesh2dBundle {
speed: Speed(Vec2::new(0.0, 500.0)), mesh: meshes.add(gen::planet()).into(),
mesh: MaterialMesh2dBundle { material: materials.add(ColorMaterial::from(Color::YELLOW)),
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.)),
..default() ..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)] #[derive(Bundle)]
struct Planet { struct Planet {
pos: TransformBundle,
mass: Mass, mass: Mass,
speed: Speed, speed: Speed,
mesh: MaterialMesh2dBundle<ColorMaterial>, visibility: InheritedVisibility,
} }
#[derive(Resource)] #[derive(Resource)]