1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//! The [`Force`] trait — implement this to create custom forces.
use SimNode;
/// A force that acts on simulation nodes each tick.
///
/// Forces modify node **velocities** (`vx`, `vy`) — they should not set
/// positions directly. The simulation integrates velocities into positions
/// after all forces have been applied.
///
/// # Implementing a custom force
///
/// ```rust,ignore
/// use egui_xyflow::physics::{Force, SimNode};
///
/// struct GravityWell {
/// center_x: f32,
/// center_y: f32,
/// strength: f32,
/// }
///
/// impl Force for GravityWell {
/// fn apply(&mut self, nodes: &mut [SimNode], alpha: f32) {
/// for node in nodes.iter_mut().filter(|n| n.fx.is_none()) {
/// node.vx += (self.center_x - node.x) * self.strength * alpha;
/// node.vy += (self.center_y - node.y) * self.strength * alpha;
/// }
/// }
/// }
/// ```