Initial commit
This commit is contained in:
commit
038211fb8b
6 changed files with 4073 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/target
|
3874
Cargo.lock
generated
Normal file
3874
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "jsb-gravity"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bevy = "0.12.0"
|
||||||
|
opensimplex_noise_rs = "0.3.0"
|
||||||
|
rand = "0.8.5"
|
8
rustfmt.toml
Normal file
8
rustfmt.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
hard_tabs = true
|
||||||
|
newline_style = "unix"
|
||||||
|
|
||||||
|
unstable_features = true
|
||||||
|
format_code_in_doc_comments = true
|
||||||
|
format_macro_bodies = true
|
||||||
|
format_macro_matchers = true
|
||||||
|
format_strings = true
|
29
src/gen.rs
Normal file
29
src/gen.rs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
use bevy::{prelude::*, render::render_resource::PrimitiveTopology};
|
||||||
|
use opensimplex_noise_rs::OpenSimplexNoise;
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
pub fn planet() -> Mesh {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let simplex = OpenSimplexNoise::new(Some(rng.gen()));
|
||||||
|
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
|
||||||
|
let perimeter: u32 = 1000;
|
||||||
|
mesh.insert_attribute(
|
||||||
|
Mesh::ATTRIBUTE_POSITION,
|
||||||
|
(0..perimeter)
|
||||||
|
.map(|i| {
|
||||||
|
let mut r = simplex.eval_2d(i as f64 * 0.02, 0.) as f32 * 20.0 + 100.0;
|
||||||
|
r += simplex.eval_2d(i as f64 * 0.05, 10.) as f32 * 10.0;
|
||||||
|
r += simplex.eval_2d(i as f64 * 0.2, 10.) as f32 * 4.0;
|
||||||
|
let a = std::f32::consts::TAU * i as f32 / perimeter as f32;
|
||||||
|
[r * a.cos(), r * a.sin(), 0.]
|
||||||
|
})
|
||||||
|
.chain([[0., 0., 0.]].into_iter())
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
);
|
||||||
|
let mut triangles = Vec::new();
|
||||||
|
for i in 0..perimeter {
|
||||||
|
triangles.extend_from_slice(&[i, perimeter, (i + 1) % perimeter]);
|
||||||
|
}
|
||||||
|
mesh.set_indices(Some(bevy::render::mesh::Indices::U32(triangles)));
|
||||||
|
mesh
|
||||||
|
}
|
152
src/main.rs
Normal file
152
src/main.rs
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in a new issue