fn spring(nodes: &mut Vec<Node>, spring_connection: &Spring) {
    let length = spring_connection.length;
    let stiffness = spring_connection.stiffness;
    let damping = spring_connection.damping;

    let mut diff = vec2(nodes[spring_connection.to].x, nodes[spring_connection.to].y)
        - vec2(
            nodes[spring_connection.from].x,
            nodes[spring_connection.from].y,
        );
    diff = diff.normalize();

    // Deviation from true spring
    // If longer than length, don't apply forces. 1 way spring.
    if abs(diff.magnitude()) > length {
        return;
    }

    diff *= length;
    let target = vec2(
        nodes[spring_connection.from].x,
        nodes[spring_connection.from].y,
    ) + diff;

    let mut force = target - vec2(nodes[spring_connection.to].x, nodes[spring_connection.to].y);
    force *= 0.5;
    force *= stiffness;
    force *= 1.0 - damping;

    nodes[spring_connection.to].velocity += force;
    force *= -1.0;
    nodes[spring_connection.from].velocity += force;
}
// ------ apply forces on spring and attached nodes ------
apply forces on spring and attached nodes
apply forces on spring and attached nodes
fn spring(nodes: &mut Vec<Node>, spring_connection: &Spring) {
    let length = spring_connection.length;
    let stiffness = spring_connection.stiffness;
    let damping = spring_connection.damping;
    let mut diff = vec2(nodes[spring_connection.to].x, nodes[spring_connection.to].y)
        - vec2(
            nodes[spring_connection.from].x,
            nodes[spring_connection.from].y,
        );
    diff = diff.normalize();
    // Deviation from true spring
    // If longer than length, don't apply forces. 1 way spring.
    if abs(diff.magnitude()) > length {
        return;
    }
    diff *= length;
    let target = vec2(
        nodes[spring_connection.from].x,
        nodes[spring_connection.from].y,
    ) + diff;
    let mut force = target - vec2(nodes[spring_connection.to].x, nodes[spring_connection.to].y);
    force *= 0.5;
    force *= stiffness;
    force *= 1.0 - damping;
    nodes[spring_connection.to].velocity += force;
    force *= -1.0;
    nodes[spring_connection.from].velocity += force;
}
fn spring ( nodes : & mut Vec < Node > , spring_connection : & Spring ) { let length = spring_connection . length ; let stiffness = spring_connection . stiffness ; let damping = spring_connection . damping ; let mut diff = vec2 ( nodes [ spring_connection . to ] . x , nodes [ spring_connection . to ] . y ) - vec2 ( nodes [ spring_connection . from ] . x , nodes [ spring_connection . from ] . y , ) ; diff = diff . normalize ( ) ; // Deviation from true spring // If longer than length, don't apply forces. 1 way spring. if abs ( diff . magnitude ( ) ) > length { return ; } diff *= length ; let target = vec2 ( nodes [ spring_connection . from ] . x , nodes [ spring_connection . from ] . y , ) + diff ; let mut force = target - vec2 ( nodes [ spring_connection . to ] . x , nodes [ spring_connection . to ] . y ) ; force *= 0.5 ; force *= stiffness ; force *= 1.0 - damping ; nodes [ spring_connection . to ] . velocity += force ; force *= - 1.0 ; nodes [ spring_connection . from ] . velocity += force ; }
apply forces on spring and attached nodes
apply forces on spring and attached nodes
nodes
&mut Vec<Node>
spring_connection
&Spring