Make the iter_combinators
examples more pretty (#3075)
# Objective - More colours ## Solution - Various changes - Would have a video here, but don't have an easy way to record one before I go to sleep - I intend to additionally change the distribution of the satellites, to be more uniform in space.
This commit is contained in:
parent
507441d96f
commit
44f370a26c
@ -1,4 +1,4 @@
|
|||||||
use bevy::{core::FixedTimestep, prelude::*};
|
use bevy::{core::FixedTimestep, pbr::AmbientLight, prelude::*, render::camera::Camera};
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
|
|
||||||
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
|
||||||
@ -10,6 +10,10 @@ fn main() {
|
|||||||
App::new()
|
App::new()
|
||||||
.insert_resource(Msaa { samples: 4 })
|
.insert_resource(Msaa { samples: 4 })
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
|
.insert_resource(AmbientLight {
|
||||||
|
brightness: 0.03,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
.add_startup_system(generate_bodies)
|
.add_startup_system(generate_bodies)
|
||||||
.add_stage_after(
|
.add_stage_after(
|
||||||
CoreStage::Update,
|
CoreStage::Update,
|
||||||
@ -19,11 +23,12 @@ fn main() {
|
|||||||
.with_system(interact_bodies)
|
.with_system(interact_bodies)
|
||||||
.with_system(integrate),
|
.with_system(integrate),
|
||||||
)
|
)
|
||||||
|
.add_system(look_at_star)
|
||||||
|
.insert_resource(ClearColor(Color::BLACK))
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
const GRAVITY_CONSTANT: f32 = 0.001;
|
const GRAVITY_CONSTANT: f32 = 0.001;
|
||||||
const SOFTENING: f32 = 0.01;
|
|
||||||
const NUM_BODIES: usize = 100;
|
const NUM_BODIES: usize = 100;
|
||||||
|
|
||||||
#[derive(Component, Default)]
|
#[derive(Component, Default)]
|
||||||
@ -32,6 +37,8 @@ struct Mass(f32);
|
|||||||
struct Acceleration(Vec3);
|
struct Acceleration(Vec3);
|
||||||
#[derive(Component, Default)]
|
#[derive(Component, Default)]
|
||||||
struct LastPos(Vec3);
|
struct LastPos(Vec3);
|
||||||
|
#[derive(Component)]
|
||||||
|
struct Star;
|
||||||
|
|
||||||
#[derive(Bundle, Default)]
|
#[derive(Bundle, Default)]
|
||||||
struct BodyBundle {
|
struct BodyBundle {
|
||||||
@ -52,14 +59,13 @@ fn generate_bodies(
|
|||||||
subdivisions: 3,
|
subdivisions: 3,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let pos_range = 1.0..15.0;
|
|
||||||
let color_range = 0.5..1.0;
|
let color_range = 0.5..1.0;
|
||||||
let vel_range = -0.5..0.5;
|
let vel_range = -0.5..0.5;
|
||||||
|
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
for _ in 0..NUM_BODIES {
|
for _ in 0..NUM_BODIES {
|
||||||
let mass_value_cube_root: f32 = rng.gen_range(0.5..4.0);
|
let radius: f32 = rng.gen_range(0.1..0.7);
|
||||||
let mass_value: f32 = mass_value_cube_root * mass_value_cube_root * mass_value_cube_root;
|
let mass_value = radius.powi(3) * 10.;
|
||||||
|
|
||||||
let position = Vec3::new(
|
let position = Vec3::new(
|
||||||
rng.gen_range(-1.0..1.0),
|
rng.gen_range(-1.0..1.0),
|
||||||
@ -67,18 +73,19 @@ fn generate_bodies(
|
|||||||
rng.gen_range(-1.0..1.0),
|
rng.gen_range(-1.0..1.0),
|
||||||
)
|
)
|
||||||
.normalize()
|
.normalize()
|
||||||
* rng.gen_range(pos_range.clone());
|
* rng.gen_range(0.2f32..1.0).powf(1. / 3.)
|
||||||
|
* 15.;
|
||||||
|
|
||||||
commands.spawn_bundle(BodyBundle {
|
commands.spawn_bundle(BodyBundle {
|
||||||
pbr: PbrBundle {
|
pbr: PbrBundle {
|
||||||
transform: Transform {
|
transform: Transform {
|
||||||
translation: position,
|
translation: position,
|
||||||
scale: Vec3::splat(mass_value_cube_root * 0.1),
|
scale: Vec3::splat(radius),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
mesh: mesh.clone(),
|
mesh: mesh.clone(),
|
||||||
material: materials.add(
|
material: materials.add(
|
||||||
Color::rgb_linear(
|
Color::rgb(
|
||||||
rng.gen_range(color_range.clone()),
|
rng.gen_range(color_range.clone()),
|
||||||
rng.gen_range(color_range.clone()),
|
rng.gen_range(color_range.clone()),
|
||||||
rng.gen_range(color_range.clone()),
|
rng.gen_range(color_range.clone()),
|
||||||
@ -101,29 +108,40 @@ fn generate_bodies(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add bigger "star" body in the center
|
// add bigger "star" body in the center
|
||||||
|
let star_radius = 1.;
|
||||||
commands
|
commands
|
||||||
.spawn_bundle(BodyBundle {
|
.spawn_bundle(BodyBundle {
|
||||||
pbr: PbrBundle {
|
pbr: PbrBundle {
|
||||||
transform: Transform {
|
transform: Transform::from_scale(Vec3::splat(star_radius)),
|
||||||
scale: Vec3::splat(0.5),
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
mesh: meshes.add(Mesh::from(shape::Icosphere {
|
mesh: meshes.add(Mesh::from(shape::Icosphere {
|
||||||
radius: 1.0,
|
radius: 1.0,
|
||||||
subdivisions: 5,
|
subdivisions: 5,
|
||||||
})),
|
})),
|
||||||
material: materials.add((Color::ORANGE_RED * 10.0).into()),
|
material: materials.add(StandardMaterial {
|
||||||
|
base_color: Color::ORANGE_RED,
|
||||||
|
emissive: (Color::ORANGE_RED * 2.),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
mass: Mass(1000.0),
|
mass: Mass(500.0),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.insert(PointLight {
|
.insert(Star)
|
||||||
color: Color::ORANGE_RED,
|
.with_children(|p| {
|
||||||
|
p.spawn_bundle(PointLightBundle {
|
||||||
|
point_light: PointLight {
|
||||||
|
color: Color::WHITE,
|
||||||
|
intensity: 400.0,
|
||||||
|
range: 100.0,
|
||||||
|
radius: star_radius,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
});
|
||||||
commands.spawn_bundle(PerspectiveCameraBundle {
|
commands.spawn_bundle(PerspectiveCameraBundle {
|
||||||
transform: Transform::from_xyz(0.0, 10.5, -20.0).looking_at(Vec3::ZERO, Vec3::Y),
|
transform: Transform::from_xyz(0.0, 10.5, -30.0).looking_at(Vec3::ZERO, Vec3::Y),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -136,7 +154,7 @@ fn interact_bodies(mut query: Query<(&Mass, &GlobalTransform, &mut Acceleration)
|
|||||||
let delta = transform2.translation - transform1.translation;
|
let delta = transform2.translation - transform1.translation;
|
||||||
let distance_sq: f32 = delta.length_squared();
|
let distance_sq: f32 = delta.length_squared();
|
||||||
|
|
||||||
let f = GRAVITY_CONSTANT / (distance_sq * (distance_sq + SOFTENING).sqrt());
|
let f = GRAVITY_CONSTANT / distance_sq;
|
||||||
let force_unit_mass = delta * f;
|
let force_unit_mass = delta * f;
|
||||||
acc1.0 += force_unit_mass * *m2;
|
acc1.0 += force_unit_mass * *m2;
|
||||||
acc2.0 -= force_unit_mass * *m1;
|
acc2.0 -= force_unit_mass * *m1;
|
||||||
@ -156,3 +174,16 @@ fn integrate(mut query: Query<(&mut Acceleration, &mut Transform, &mut LastPos)>
|
|||||||
transform.translation = new_pos;
|
transform.translation = new_pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn look_at_star(
|
||||||
|
mut camera: Query<&mut Transform, (With<Camera>, Without<Star>)>,
|
||||||
|
star: Query<&Transform, With<Star>>,
|
||||||
|
) {
|
||||||
|
let mut camera = camera.single_mut();
|
||||||
|
let star = star.single();
|
||||||
|
let new_rotation = camera
|
||||||
|
.looking_at(star.translation, Vec3::Y)
|
||||||
|
.rotation
|
||||||
|
.lerp(camera.rotation, 0.1);
|
||||||
|
camera.rotation = new_rotation;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user