Verlet integration
This commit is contained in:
parent
3a04fe95b6
commit
be816127fb
|
@ -8,7 +8,7 @@ pub struct Body {
|
|||
pub velocity: Vec2,
|
||||
pub acceleration: Vec2,
|
||||
pub mass: f32,
|
||||
pub drag: f32,
|
||||
pub friction: f32,
|
||||
pub fixed: bool,
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ impl Default for Body {
|
|||
position: Vec2::ZERO,
|
||||
velocity: Vec2::ZERO,
|
||||
acceleration: Vec2::ZERO,
|
||||
drag: 0.1,
|
||||
friction: 0.1,
|
||||
mass: 1.0,
|
||||
fixed: false,
|
||||
}
|
||||
|
@ -35,13 +35,16 @@ impl Body {
|
|||
}
|
||||
|
||||
pub fn update(&mut self, dt: f32) {
|
||||
self.velocity += self.acceleration * dt;
|
||||
self.velocity *= self.drag.powf(dt);
|
||||
self.acceleration = Vec2::ZERO;
|
||||
let pos = self.position;
|
||||
let vel = self.velocity;
|
||||
let acc = self.acceleration;
|
||||
|
||||
if !self.fixed {
|
||||
self.position += self.velocity * dt;
|
||||
}
|
||||
let new_pos = pos + vel * dt + acc * (dt * dt * 0.5);
|
||||
let new_vel = vel + acc * (dt * 0.5);
|
||||
|
||||
self.position = new_pos;
|
||||
self.velocity = new_vel * self.friction.powf(dt);
|
||||
self.acceleration = Vec2::ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -237,7 +240,7 @@ impl Constraint {
|
|||
let distance = delta.length();
|
||||
let displacement = distance - self.length;
|
||||
delta *= displacement / distance * self.strength * ALPHA;
|
||||
delta
|
||||
delta / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -304,7 +307,7 @@ impl ForceGraph {
|
|||
for i in 1..self.nodes.len() {
|
||||
for j in i..self.nodes.len() {
|
||||
let force = self.with_unhidden(i - 1, j, |from, to| {
|
||||
const REPULSION_CONSTANT: f32 = 1000.0;
|
||||
const REPULSION_CONSTANT: f32 = 500.0;
|
||||
let delta = to.body.position - from.body.position;
|
||||
let proximity = delta.length().max(0.1);
|
||||
let force = -(REPULSION_CONSTANT / (proximity * proximity));
|
||||
|
@ -339,8 +342,8 @@ impl ForceGraph {
|
|||
|
||||
let theta = std::f32::consts::TAU / neighbor_num as f32;
|
||||
|
||||
for child in children {
|
||||
let angle = Vec2::from_angle((child + 1) as f32 * theta);
|
||||
for (index, child) in children.into_iter().enumerate() {
|
||||
let angle = Vec2::from_angle((index + 1) as f32 * theta);
|
||||
let child = &mut self.nodes[child];
|
||||
child.info.hidden = false;
|
||||
child.body.fixed = false;
|
||||
|
|
Loading…
Reference in New Issue