Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Make the iter_combinators examples more pretty #3075

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 50 additions & 19 deletions examples/ecs/iter_combinations.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::{core::FixedTimestep, prelude::*};
use bevy::{core::FixedTimestep, pbr::AmbientLight, prelude::*, render::camera::Camera};
use rand::{thread_rng, Rng};

#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
Expand All @@ -10,6 +10,10 @@ fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.insert_resource(AmbientLight {
brightness: 0.03,
..Default::default()
})
.add_startup_system(generate_bodies)
.add_stage_after(
CoreStage::Update,
Expand All @@ -19,11 +23,12 @@ fn main() {
.with_system(interact_bodies)
.with_system(integrate),
)
.add_system(look_at_star)
.insert_resource(ClearColor(Color::BLACK))
.run();
}

const GRAVITY_CONSTANT: f32 = 0.001;
const SOFTENING: f32 = 0.01;
const NUM_BODIES: usize = 100;

#[derive(Component, Default)]
Expand All @@ -32,6 +37,8 @@ struct Mass(f32);
struct Acceleration(Vec3);
#[derive(Component, Default)]
struct LastPos(Vec3);
#[derive(Component)]
struct Star;

#[derive(Bundle, Default)]
struct BodyBundle {
Expand All @@ -52,33 +59,33 @@ fn generate_bodies(
subdivisions: 3,
}));

let pos_range = 1.0..15.0;
let color_range = 0.5..1.0;
let vel_range = -0.5..0.5;

let mut rng = thread_rng();
for _ in 0..NUM_BODIES {
let mass_value_cube_root: f32 = rng.gen_range(0.5..4.0);
let mass_value: f32 = mass_value_cube_root * mass_value_cube_root * mass_value_cube_root;
let radius: f32 = rng.gen_range(0.1..0.7);
let mass_value = radius.powi(3) * 10.;

let position = Vec3::new(
rng.gen_range(-1.0..1.0),
rng.gen_range(-1.0..1.0),
rng.gen_range(-1.0..1.0),
)
.normalize()
* rng.gen_range(pos_range.clone());
* rng.gen_range(0.2f32..1.0).powf(1. / 3.)
* 15.;

commands.spawn_bundle(BodyBundle {
pbr: PbrBundle {
transform: Transform {
translation: position,
scale: Vec3::splat(mass_value_cube_root * 0.1),
scale: Vec3::splat(radius),
..Default::default()
},
mesh: mesh.clone(),
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()),
Expand All @@ -101,29 +108,40 @@ fn generate_bodies(
}

// add bigger "star" body in the center
let star_radius = 1.;
commands
.spawn_bundle(BodyBundle {
pbr: PbrBundle {
transform: Transform {
scale: Vec3::splat(0.5),
..Default::default()
},
transform: Transform::from_scale(Vec3::splat(star_radius)),
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 1.0,
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()
},
mass: Mass(1000.0),
mass: Mass(500.0),
..Default::default()
})
.insert(PointLight {
color: Color::ORANGE_RED,
..Default::default()
.insert(Star)
.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()
});
});
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()
});
}
Expand All @@ -136,7 +154,7 @@ fn interact_bodies(mut query: Query<(&Mass, &GlobalTransform, &mut Acceleration)
let delta = transform2.translation - transform1.translation;
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;
acc1.0 += force_unit_mass * *m2;
acc2.0 -= force_unit_mass * *m1;
Expand All @@ -156,3 +174,16 @@ fn integrate(mut query: Query<(&mut Acceleration, &mut Transform, &mut LastPos)>
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;
}