updatePosition method
Calculates the displacement within a time step and by that the new position of the Node based on mechanics using the given parameters.
It takes the displacement scaling as factor of 0 to 1, the
minimumVelocity as a double, the maxStaticFriction as a double, as well
as the damping as a double.
It returns: whether the Node is considered currently in motion.
If fixed, the Node is not moving and the force and velocity on it is zero.
When moving slower than minVelocity and the force is smaller than the
maxStaticFriction, the Node is considered in a static state and no
calculation is required.
Implementation
bool updatePosition({
required double scaling,
required double minVelocity,
required double maxStaticFriction,
required double damping,
}) {
if (_isFixed) {
_force = Vector2.zero();
_velocity = Vector2.zero();
return false;
}
if (_velocity.length < minVelocity) {
// static state
if (_force.length < maxStaticFriction) {
// If the force is too small in the static state, no calculation is required
_velocity = Vector2.zero();
_force = Vector2.zero();
return false;
}
}
// dynamic state
final friction = -_velocity.normalized() * maxStaticFriction;
_force += friction;
_velocity += _force / mass;
_velocity *= damping;
position += _velocity * scaling;
_force = Vector2.zero();
return true;
}