Shaking and unstable behaviour.

Hello,

Let me share my experience with trying to make a snake with Box2D.
To make a clear - I have spend 3Weeks+ on this problem only and I have no clue about solution.
This is fully repeatable and easy to reproduce.

I have bulled a snake which goes out of the whole in ground.
Snake is build with 20+ balls connected with WeldJoints.
Snake have a square head (to have a larger mass and allow user to easy operate with Snake)
Snake tail is connected to the ground with PrismaticJoint so it can't ever fully goes-out of the whole.

It looks more-less like this.
||||||||||||||||||||||||||||||
X------O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-O-[]
||||||||||||||||||||||||||||||

X Ground
------ PrismaticJoint
O- Ball with WeldJoints (I have tried other types of joints and problem was same) - joints are short and connect centers of balls. instead of balls you can use squares - result is same.
[] head.

I have set parameters in a way to that snake can not make a loop with his body. joints tries to keep it one straight line - user is trying to bend it. result is quite realistic.
Other thing is, that sometime it happen that we have a loop in snake body and I have no clue how to fully avoid it. (I explain it to myself ,that it's because of calculation frequency = velocityIterations = 16;positionIterations = 6;)

When user start to play with snake, 2 wrong things happens.
1. snake is trying to keep his body in one line, user us trying to bend the body.... but sometime user bend snake too much (still no loop) and snake is bended from this moment. (no more straight - it looks like joints have some limit, and when we bend more than that - they remember new state as orginal one and never back to straight line.

2. When user plays with snake more, and point 1 happens few times. snakes often spontaneously start shaking his body, with very high frequency and large distance. shake becomes than like bubbles in boiling watter - this is no longer snake - it is just like electrons in atom.
I detect this states checking distance between 3 balls if distance between ball 1 and 3 is much smaller then between 1 and 2 or 2 and 3 than it shakes.
No way to stop, no way to prevent.

A.... user moves the snake with mouse joint mouse_frequency=0.9; mouse_dumping=0.9;

PS: I have impression that it have something to do with calculation frequency. I have seen that having 5 snakes it happens more often than having only 1.
Is it possible that mouse joint which is asynchronous here may cause such issue? (touching of the snake may happen few times between physics single update)

I am programming now only on iPad with Xcode and I don;t have compilable test bed for this environment to give you example how it works.
Mariusz
You could try also using revolute joints between the snake segments, because revolute joints can have a limit constraint. The limit constraint will prevent each connected pair of bodies from rotating too far relative to each other, eg. max 90 degrees in each direction. I don't think it would prevent the snake from getting stuck in itself if you bent it all the way around, but it should stop the stability problem you are seeing. I think.

Another suggestion is to make the bodies gradually lighter (lower density) towards the head, so that the bodies closer to the base are able to constrain those above them better.
I have tried to use revolute joints but result was exactly same.
limit constraint works until some pressure only, after breaking some magical barrier (I use mouse joints) instability is more visible. (or this is related with slow calculation, however my calculation rate is not so slow).
regarding the mass - less mass makes instability appearing more often.
I have tried also larger mass + friction + lower frequency, but all I have achieved is that the effect is less often. (still not acceptable often)
There must be some general issue because I can't imagine that physics engine BECOME UNSTABLE just like that.
Did you try making each body a bit lighter as toward the end of the snake?
Heavier bodies tend to push light bodies into a position to meet the joint constraints, so it helps to have an "chain of command" where the pushing is only going one way. If you let all the bodies push each other around (ie. with equal weights for them all) the structure will not stay together well.
Thanks for suggestions.

I see however that I did not described it well enough.

While it happed, it looks like snake is under high voltage. It just shaking irregularly. I can't stop it, or predict movements. It doesn't looks like movements at all - it is just like high amplitude movement noise.

PS: Lighter body did not helped as well.
The point I meant was to make each body lighter toward the head of the snake, is that what you tried?

Original thread: https://box2d.org/forum/viewtopic.php?t=9211