Jump to content
  • Advertisement
Sign in to follow this  
lemour9907

My spring based system blows up :(

This topic is 4022 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I've been trying to develop a very simple spring based system using just trivial F = kx, but my problem is that my system blows up all the time if some of the variables change too rapidly. One solution I could do is decrease the time increment dt, or perform many iterations of force calculations each tick, but I wanted to know if there are any other ways of doing this... like applying some sort of damping or friction? I couldn't find any proper article on this... Anyone knows of anything, or has some suggestions for me? Thank you

Share this post


Link to post
Share on other sites
Advertisement
You already have the answer. Damping will do the trick.

What damping does is, it allows you to keep a check on the maximum velocity.
Example, velocity of an object falling through the earths atmosphere is governed by F=ma, so if the object were to fall for an infinite amount of time then you would expect it to reach an infinite velocity. This doesn't happen because of the damping produced by the viscosity of the atmosphere - the final velocity will be equal to the terminal velocity (which depends on both gravity & damping produced by atmosphere)

Share this post


Link to post
Share on other sites
Quote:
Original post by Geoff the Medio
What integration scheme are you using? You may need to change to one that's more numerically stable...


Absolutely. I'd bet lemour9907 is using simple explicit Euler integration, which can only be stable when damping is implemented, for problems with springs. May want to search on Verlet integration, or maybe first try Symplectic Euler (since it is super trivial to switch from simple explicit Euler to Symplectic Euler).

Share this post


Link to post
Share on other sites
i don't understand what you guys mean by integration scheme...
F = kx
F = ma
=> ma = kx => a = kx/m

I have that all, so i get acceleration for each of the nodes that the spring connects, and i just apply that... v = at, and then add velocity to position each tick...

Share this post


Link to post
Share on other sites
You're using the euler integration method. The other methods mentioned here are Verlet, and "Symplectic Euler". Verlet doesn't store a velocity vector, and calculates the velocity from the past position, and current position. I'm not 100% what "Symplectic Euler" is, but I think it may be what some people call the "Semi-implicit Euler" integration, which just switches the calculations of the acceleration, velocity and position.

IE:
Euler -
acceleration = force/mass;
velocity = acceleration*dt;
position = velocity*dt;

"Symplectic Euler" -
position = velocity*dt;
velocity = acceleration*dt;
acceleration = force/mass;

You could also look into the RK4 integrator, which is much more numerically stable (4th-order), but also more costly to calculate.

Share this post


Link to post
Share on other sites
Quote:
Original post by lemour9907
i don't understand what you guys mean by integration scheme...
F = kx
F = ma
=> ma = kx => a = kx/m

I have that all, so i get acceleration for each of the nodes that the spring connects, and i just apply that... v = at, and then add velocity to position each tick...


Unfortunately its not as simple as that. Those equations assume the time step t to be infinitesimally small such that v and a are both constant over t.
With finite t errors accumulate and can cause instability.

As others have suggested, Euler integration is the simplest to implement but worst when it comes to stability. A quick way to solve this problem is to add damping.

More accurate integration schemes are available but are more complicated to implement. Numerical integration methods are based on the Taylor series - something you will need to take a look at before going to RK4 integrator.

Share this post


Link to post
Share on other sites
There's also RK2 which isn't as heavy as RK4. Having said that if you can do RK2 then you can probably do RK4.

All of these methods have different conditions for stability. I haven't touched it in a few years... I remember it was conditions like oscillating systems etc? Anyone refresh my memory?

Share this post


Link to post
Share on other sites
Quote:
Original post by bronxbomber92
You're using the euler integration method. The other methods mentioned here are Verlet, and "Symplectic Euler". Verlet doesn't store a velocity vector, and calculates the velocity from the past position, and current position. I'm not 100% what "Symplectic Euler" is, but I think it may be what some people call the "Semi-implicit Euler" integration, which just switches the calculations of the acceleration, velocity and position.

IE:
Euler -
acceleration = force/mass;
velocity = acceleration*dt;
position = velocity*dt;

"Symplectic Euler" -
position = velocity*dt;
velocity = acceleration*dt;
acceleration = force/mass;


Er, you got the two reversed:

Simple Explicit Euler is:
position = velocity * dt; // using the old velocity
velocity = acceleration * dt; // using the old acceleration
acceleration = force/mass;

Symplectic Euler (yes, a semi-implicit technique) is:
acceleration = force/mass;
velocity = acceleration * dt; // using the new acceleration
position = velocity * dt; // using the new velocity

(And, yes, flipping the order can have a dramatic effect on the results in some cases!)

Here's the Wikipedia page:

Symplectic Euler (aka Euler-Cromer)

Share this post


Link to post
Share on other sites
Gotcha, I presumed Symplectic Euler meant Semi-Implicit Euler. I guess I was wrong.

So, was I correct in that the Semi-Implicit Euler is much more numerically stable?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!