pub struct BHQuadtree { /* private fields */ }Expand description
A Quadtree specially optimized for the Barnes-Hut algorithm
An interactive explanation of the algorithm can be found here
This quadtree is immutable and flat, not recursive. It is optimized to be rebuilt frequently
and supports efficient accumulation of approximated force. Each step, convert your items into
WeightedPoint and call the build method to clear and reconstruct the tree. Then for each
item you need to accumulate force on, call the accumulate method, passing a custom force
function.
This implementation is heavily inspired by DeadlockCode’s Barnes-Hut implementation
Implementations§
Source§impl BHQuadtree
impl BHQuadtree
Sourcepub fn new(node_capacity: usize, max_depth: usize, theta: f32) -> Self
pub fn new(node_capacity: usize, max_depth: usize, theta: f32) -> Self
Create a new empty BHQuadtree
§Arguments
node_capacity: The maximum number of items a node can hold before subdividingmax_depth: The maximum depth of the tree at which nodes will ignore capacitytheta: Theta parameter of Barnes-Hut algorithm
Sourcepub fn build(&mut self, items: Vec<WeightedPoint>)
pub fn build(&mut self, items: Vec<WeightedPoint>)
Clear all internal data and reconstruct the tree from a sequence of weighted points
Sourcepub fn accumulate<F: Fn(WeightedPoint) -> Vec2>(
&self,
target: Vec2,
force_fn: F,
) -> Vec2
pub fn accumulate<F: Fn(WeightedPoint) -> Vec2>( &self, target: Vec2, force_fn: F, ) -> Vec2
Accumulate a force vector to act on a target position with an arbitrary force function, approximating weighted points based on the theta parameter.