Wip quadtree

This commit is contained in:
Pascal Engélibert 2023-12-23 18:07:16 +01:00
parent 038211fb8b
commit e494d3992c
4 changed files with 249 additions and 208 deletions

411
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -4,6 +4,6 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
bevy = "0.12.0" bevy = "0.12.1"
opensimplex_noise_rs = "0.3.0" opensimplex_noise_rs = "0.3.0"
rand = "0.8.5" rand = "0.8.5"

View file

@ -1,4 +1,5 @@
mod gen; mod gen;
mod quadtree;
use bevy::{ecs::query::BatchingStrategy, prelude::*, sprite::MaterialMesh2dBundle}; use bevy::{ecs::query::BatchingStrategy, prelude::*, sprite::MaterialMesh2dBundle};
@ -29,7 +30,7 @@ fn setup(
}); });
commands.spawn(Planet { commands.spawn(Planet {
mass: Mass(1.9885e18), mass: Mass(1.988e18),
speed: Speed { x: 0., y: 0. }, speed: Speed { x: 0., y: 0. },
mesh: MaterialMesh2dBundle { mesh: MaterialMesh2dBundle {
mesh: meshes.add(gen::planet()).into(), mesh: meshes.add(gen::planet()).into(),

41
src/quadtree.rs Normal file
View file

@ -0,0 +1,41 @@
use bevy::prelude::*;
pub trait Body {
fn mass(&self) -> f32;
fn center_of_mass(&self) -> (f32, Vec2);
}
pub enum Node<L: Body> {
Node([Node; 4]),
Leaf(Vec<L>),
}
impl<L: Body> Body for Node<L> {
fn mass(&self) -> f32 {
match self {
Node::Node([n1, n2, n3, n4]) => n0.mass() + n1.mass() + n2.mass() + n3.mass(),
Node::Leaf(v) => v.iter().map(Body::mass).sum(),
}
}
fn center_of_mass(&self) {
match self {
Node::Node([n1, n2, n3, n4]) => n0.mass() + n1.mass() + n2.mass() + n3.mass(),
Node::Leaf(v) => {
let mut mass = 0.0;
let mut center_of_mass = Vec2::zero();
for(n in v) {
let (n_mass, n_center) = n.center_of_mass();
mass += n_mass;
center_of_mass += mass * n_center;
}
(mass, center_of_mass / mass)
},
}
}
}
impl<L: Body> Node<L> {
fn add_body(&mut self, body: L) {
}
}