public void solve() { for (int i = 0; i != nodes.Count - 1; ++i) { for (int j = i + 1; j != nodes.Count; ++j) { T a = nodes[i].element; T b = nodes[j].element; Vector3 dist = (b.transform.position - a.transform.position); float magsqr = dist.sqrMagnitude; if (magsqr != 0.0f && magsqr < REPULSION_DISTANCE) { nodes[i].velocity -= dist / magsqr * SPRING; nodes[j].velocity += dist / magsqr * SPRING; } float desire = (float)metric.getDistance(a, b); desire *= desire; float diff = desire - magsqr; Vector3 dir = dist.normalized; if (diff != 0.0f) { nodes[i].velocity -= dir * diff * SPRING * SPRING; nodes[j].velocity += dir * diff * SPRING * SPRING; } } } foreach (Node n in nodes) { n.element.transform.position += n.velocity; n.velocity *= DAMPING; } }