Wip quadtree
This commit is contained in:
parent
038211fb8b
commit
e494d3992c
4 changed files with 249 additions and 208 deletions
41
src/quadtree.rs
Normal file
41
src/quadtree.rs
Normal 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) {
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue