Settling springs

Started by
4 comments, last by too_many_stars 9 years, 1 month ago

Hi guys,

A question about springs. Given the following (all in 2d space)

Vec2 anchor;

Vec2 point;

float k(spring constant)

float l(rest len)

float d(drag coefficient)

we are able to generate a force something like this using Hook's Law

Vec2 rel_pos=point-anchor

float dist=rel_pos.len()

Vec2 force=-k(abs(dist)-l)*rel_pos.normal()

every update method call, the velocity is dragged a bit.

The end result is an oscilating spring which is dampened. However, the issue appears when the spring starts to settle. It keeps vibrating around the anchor.

Does anyone know how to "shut down" that is stop a spring from vibrating around an anchor after it has reached some minimum distance or force? I don't want to hack a solution together so I thought I would ask.

Thanks,

Mike

Advertisement

the issue appears when the spring starts to settle. It keeps vibrating around the anchor.

Likely has something to do with your update method. The code you posted is for an undampened spring.

In the code you posted, you also have a parentheses problem:


// this...
Vec2 force=-k(abs(dist)-l)*rel_pos.normal(); // no reason to take abs of dist. IIRC, Vec2::len() is always >= 0
// should be...
Vec2 force=-k(abs(dist-l)*rel_pos.normal());


Does anyone know how to "shut down" that is stop a spring from vibrating around an anchor after it has reached some minimum distance or force?

Dampening should take care of most of the oscillation. But you may need to cheat a little. If you don't account for mass or gravity, then when abs(dist - l) is small enough for you (close enough to rest position), and velocity is small, set velocity to 0 and set position to rest position. That reduces the force to 0, and, with velocity 0, you won't get further displacement. If that doesn't work, post some code.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Depending on how much damping your have, you will be in a regime where the solution of the differential equation oscillates forever, or in a regime where it doesn't. This link seems useful: http://hyperphysics.phy-astr.gsu.edu/hbase/oscda2.html

For this sort of thing I always hack in a 'slide' whereby the spring moves towards its centre/rest point at a constant, but very low speed. The slide is made to never overshoot the centre/rest point.

I've only really used this for springs in UIs though (e.g. scroll bars and swiping through pages on a smartphone). It might not be a good idea in a proper physics situation.

I would use Buckeye's "cheat" approach - If force and velocity are both very close to zero, set the position directly to its equilibrium point and set velocity and force to zero.

Thanks for the help guys, testing for both velocity and force magnitudes beneath some epsilon value has dont the trick.

Mike

This topic is closed to new replies.

Advertisement