jsb-gravity/src/main.rs

153 lines
4 KiB
Rust

mod gen;
use bevy::{ecs::query::BatchingStrategy, prelude::*, sprite::MaterialMesh2dBundle};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.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),
),
)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn(Camera2dBundle {
camera: Camera { ..default() },
..default()
});
commands.spawn(Planet {
mass: Mass(1.9885e18),
speed: Speed { x: 0., y: 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 { x: 0., y: 500. },
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 { x: 0., y: -500. },
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..100u32 {
commands.spawn(Planet {
mass: Mass(1.),
speed: Speed { x: 0., y: -500. },
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()
},
});
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, SystemSet)]
enum Set {
Force,
Apply,
}
#[derive(Component)]
struct Speed {
x: f32,
y: f32,
}
#[derive(Component)]
struct Mass(f32);
#[derive(Bundle)]
struct Planet {
mass: Mass,
speed: Speed,
mesh: MaterialMesh2dBundle<ColorMaterial>,
}
#[derive(Resource)]
struct Constants {
g: f32,
}
/*fn weight_system(
constants: Res<Constants>,
query1: Query<(&Transform, &Mass)>,
mut query2: Query<(&Transform, &mut Speed)>,
time: Res<Time>,
) {
let gdt = constants.g * time.delta_seconds();
for (n1_pos, n1_mass) in query1.iter() {
for (n2_pos, mut n2_speed) in query2.iter_mut() {
let d2 = (n1_pos.translation.x - n2_pos.translation.x).powi(2)
+ (n1_pos.translation.y - n2_pos.translation.y).powi(2);
if d2 == 0.0 {
continue;
}
let f = n1_mass.0 * gdt / (d2 * d2.sqrt());
n2_speed.x += (n1_pos.translation.x - n2_pos.translation.x) * f;
n2_speed.y += (n1_pos.translation.y - n2_pos.translation.y) * f;
}
}
}*/
fn weight_system(
constants: Res<Constants>,
mut query: Query<(&Transform, &Mass, &mut Speed)>,
time: Res<Time>,
) {
let gdt = constants.g * time.delta_seconds();
let mut iter = query.iter_combinations_mut();
while let Some([(n1_pos, n1_mass, mut n1_speed), (n2_pos, n2_mass, mut n2_speed)]) =
iter.fetch_next()
{
let d2 = (n1_pos.translation.x - n2_pos.translation.x).powi(2)
+ (n1_pos.translation.y - n2_pos.translation.y).powi(2);
let f = gdt / (d2 * d2.sqrt());
n1_speed.x -= (n1_pos.translation.x - n2_pos.translation.x) * f * n2_mass.0;
n1_speed.y -= (n1_pos.translation.y - n2_pos.translation.y) * f * n2_mass.0;
n2_speed.x += (n1_pos.translation.x - n2_pos.translation.x) * f * n1_mass.0;
n2_speed.y += (n1_pos.translation.y - n2_pos.translation.y) * f * n1_mass.0;
}
}
fn apply_system(mut query: Query<(&mut Transform, &Speed)>, time: Res<Time>) {
let dt = time.delta_seconds();
query
.par_iter_mut()
.batching_strategy(BatchingStrategy::fixed(128))
.for_each(|(mut pos, speed)| {
pos.translation.x += speed.x * dt;
pos.translation.y += speed.y * dt;
});
}