I am trying to create a revolute joint with a "snapping" action. You can imagine an angular spring which tends to torque the joined body back to some fixed angle. You can push the joined body and it will resist (possible as a function of the angular displacement), and when you let go, it will spin back to where it was. (It's sort of like the springy things that keep the door from slamming into the wall. The cat "plucks" it, and you get a boooooiiiiinnnnng as it goes back straight.)
Any ideas on how to do this?
Thanks!
Stuart
I think the most true-to-life way would be to set up a revolute joint, then every time step check the joint angle to see how far away it is from some specified natural resting angle, and just apply a torque depending on how far the spring had been 'wound'. This would allow you to have a spring that goes in both directions and can be wound more than one revolution away from the rest angle, and lets you control how the torque increases, linearly or otherwise. In other words, pretty much what you suggested 🙂
From there you can set joint limits if you want to have the spring come to a hard stop somewhere, just make the rest angle a bit outside the limit so that it will never be reached and there is always a bit of torque pushing against it.
Things to consider:
- if the sprung body moves very fast you might want to set it as a bullet to prevent it tunneling through things it should hit.
- there is an upper limit on how fast a body can rotate, b2_maxRotation defined in b2Settings.h so you will not be able to have the spring rotate a body around any faster than that.
- fast moving joints can penetrate past their limit for a few time steps sometimes.
- joint limits do not bounce, if you want some bouncing at the limit you'll need to place a fixture to set restitution and bounce off.