addSpringForce method

void addSpringForce(
  1. String name,
  2. Vector2 anchor,
  3. double stiffness, {
  4. double restLength = 0,
})

Add spring force (like elastic band)

Implementation

void addSpringForce(
  String name,
  Vector2 anchor,
  double stiffness, {
  double restLength = 0,
}) {
  final displacement = position - anchor;
  final distance = displacement.length;
  final extension = distance - restLength;

  if (distance > 0) {
    final springForce = displacement.normalized() * (-stiffness * extension);
    addForce(name, springForce);
  }
}