216 lines
5.4 KiB
Rust
216 lines
5.4 KiB
Rust
mod gen;
|
|
mod quadtree;
|
|
|
|
use bevy::{
|
|
ecs::query::BatchingStrategy,
|
|
prelude::*,
|
|
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
|
|
};
|
|
|
|
#[global_allocator]
|
|
static ALLOCATOR: cap::Cap<std::alloc::System> =
|
|
cap::Cap::new(std::alloc::System, 1024 * 1024 * 1024);
|
|
|
|
static UNIVERSE_POS: (Vec2, Vec2) = (Vec2::new(-1e6, -1e6), Vec2::new(1e6, 1e6));
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((
|
|
DefaultPlugins,
|
|
bevy_fps_counter::FpsCounterPlugin,
|
|
bevy_pancam::PanCamPlugin,
|
|
))
|
|
.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::default())
|
|
.insert(bevy_pancam::PanCam::default());
|
|
|
|
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()
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, Hash, PartialEq, SystemSet)]
|
|
enum Set {
|
|
Force,
|
|
Apply,
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct Speed(Vec2);
|
|
|
|
#[derive(Component)]
|
|
struct Mass(f32);
|
|
|
|
#[derive(Bundle)]
|
|
struct Planet {
|
|
pos: TransformBundle,
|
|
mass: Mass,
|
|
speed: Speed,
|
|
visibility: InheritedVisibility,
|
|
}
|
|
|
|
#[derive(Resource)]
|
|
struct Constants {
|
|
g: f32,
|
|
}
|
|
|
|
struct Body {
|
|
mass: f32,
|
|
pos: Vec2,
|
|
}
|
|
|
|
impl quadtree::Body for Body {
|
|
fn mass(&self) -> f32 {
|
|
self.mass
|
|
}
|
|
fn pos(&self) -> Vec2 {
|
|
self.pos
|
|
}
|
|
fn add_mass(&mut self, mass: f32) {
|
|
self.mass += mass;
|
|
}
|
|
}
|
|
|
|
/*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 mut tree = quadtree::Node::new(UNIVERSE_POS);
|
|
let gdt = constants.g * time.delta_seconds();
|
|
for (pos, mass, _speed) in query.iter() {
|
|
tree.add_body(Body {
|
|
mass: mass.0,
|
|
pos: pos.translation.xy(),
|
|
});
|
|
}
|
|
query.par_iter_mut().for_each(|(pos, _mass, mut speed)| {
|
|
speed.0 += gdt * tree.apply(pos.translation.xy(), 0.5);
|
|
});
|
|
// 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.0.x * dt;
|
|
pos.translation.y += speed.0.y * dt;
|
|
});
|
|
}
|