I'm utilising some of NeHe's spring code, and after getting some pretty weird results I eventually realised the source of my error - my "dt" value in my Update function; the value that everything is multiplied against to speed up/slow down the calculations, hopefully based on frame rate. For example:
public void Update(GameTime gt) {
float dt = gt.ElapsedGameTime.Milliseconds / 160.0f;
velocity += (force / mass) * dt;
position += velocity * dt;
}
160.0f seems to work pretty well for the player's update function, but for my spring simulation I need a value of about 3000, or I end up with my springs located at (NaN,NaN) pretty much instantly.
Why do bad values for this cause everything to go so crazy? I thought it would just slow down or speed up my simulation but it seems to cause some weird cascading failure.
Edit: Sorry, forgot to link to NeHe's post on this: http://nehe.gamedev.net/tutorial/introduction_to_physical_simulations/18005/
160.0f
come from? Usually you'll want to have adt
value in seconds.. so it should begt.ElapsedGameTime.Milliseconds * 0.001f
– bummzack Apr 20 '12 at 09:50