This project has moved and is read-only. For the latest updates, please go here.

Linear Springs and NaN issues

Apr 20, 2009 at 5:27 PM
After having lots of issues with my LinearSpring type Paths, I decided to create a repro case. To reproduce:
1) Open Demo5Screen in the Advanced Samples
2) Change the call to CreateChain that uses LinearSpring to have a mass of 0.2 or below (instead of 1).
The sim blows up horribly. If you set the mass to 0.3, it doesn't blow up, but the chain spasms around creating quite an interesting effect. I realise that interacting bodies with masses that differ largely can cause instability issues, but all bodies in the chain have the same mass. Maybe I should be changing the other spring properties to account for the lower mass?

I managed to alleviate the problem by limiting the force in the LinearSpring, however, I did have to adjust the maximum force based on the mass of the bodies or they would be too loose or it would still blow up. I also tried limiting the dampning force which actually seemed to have a better result as the picking spring couldn't keep extending the first link. I'm not sure what the best approach is, but I am sure that the LinearSpring should have some sort of force limiting property. Here's how I did it temporarily (just replace last line with whole block):

            // Clamp forces
            float maxForce = 2000f; // Works OK with 0.2 mass bodies
            if (Math.Abs(_springForce) > maxForce)
                _springForce = Math.Sign(_springForce) * maxForce;

            if (Math.Abs(_dampningForce) > maxForce)
                _dampningForce = Math.Sign(_dampningForce) * maxForce;

            //calculate final _force (spring + dampning)
            Vector2.Multiply(ref _differenceNormalized, -(_springForce + _dampningForce), out _force);

Hope it helps somebody.

Apr 20, 2009 at 8:05 PM
Thanks roonda.

I have send an email to Matthew to notify him about this bug. A simple solution to this problem would be to make sure that a mass < 1 is not used in the chain. Matt might have a better solution to this tho.