How to make spring button

I want to have a button which detects the weight on top of it and depresses.

The more weight put on the button, the farther it goes into the ground, but it shouldn't rotate at all or move side to side.
I think what I want is similar to a critically damped spring, so that it returns to equilibrium as fast as possible without oscillating too much when the amount of weight changes. But the amount of damping required depends on the weight. Should I just tune it for the most common weights and hope for the best? I'm worried about stability in cases of huge weights.

More importantly, what is the best way to implement such a spring? Should I manually set the force each frame based on current position and velocity of the button?

Currently, I'm using a line joint with motor and limits, but it doesn't work as well as I'd like. Not only does it not actually model a spring, it sometimes has unstable behavior, even under normal loading. Sometimes it goes rapidly up and down each frame for no discernible reason.
You want prismatic joint, not line joint. The difference is line joint allows rotation of the two bodies wrt to each other, while prismatic does not. Then give limits for the extent of the button, and a motor for damping / return to equilibrium. If your limit is also the equilibrium point (as i imagine most buttons), you will never get oscillation, as the limit will stop it dead.
But how can I make it handle different weight loads gracefully?
You want a stronger force in proportion to the depression of the button. Google "Hooke spring".
b2DistanceJoint can be used for springs, but in your case I would think it better to build the spring into the motor forces of the prismatic joint.
I am interested - How would you make it not collide with the floor? In a sense you want it to kind of "stretch", as if its disappearing into the floor when its pressed.
I used the prismatic joint for floor buttons (pressure plates) in a game. We set it up so they would require a certain weight to be applied.

The prismatic joint pointed upward. I used the joint limit to control the range. The button did not collide with the floor (joints prevent connected bodies from colliding by default). You can use the motor to apply an upward force. You can tune that force so only certain weights will cause the button to be pressed. You can detect the button is pressed when it hits the lower limit.
Have you used Box2D in any games you've made, Erin?
The way Erin describes above is exactly how my floor buttons work in this game - http://www.youtube.com/watch?v=_k87Ybaz8tY

Works a treat - I just modify the force of the motor in the pris. joint in the editor to set the weight required to depress and examine the joint position to test the button position.
BorisTheBrave wrote:
Have you used Box2D in any games you've made, Erin?


No, but I'm usually working on a 3D engine that is used in commercial games. I wrote the physics engine that was used in Tomb Raider Legend and later games in the TR franchise. I'm currently working on a 3D engine used at Blizzard called Domino.

We used physics for many of the interactive elements in Tomb Raider Legend:
- pressure plates
- rope swinging simulation
- motorcycle and forklift
- breakables
- traps
- puzzles
- grapple
- buoyancy

I like the idea of making a game with Box2D, but never seem to find the time. A long time ago I wrote 2D games on the HP48 calculator. 🙂
Cool.

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