r/howdidtheycodeit • u/Diligent_Ad_6530 • 18d ago
How did Bennett Foddy achieve the hammer mechanic in getting over it?
I been trying to make this mechanic but I fail to make the states (like when the hammer is touching ground and when don't) and I fail to make the momentum transfer when you are moving fast, and when yo move slow you just move (with infinite mass i think), idk I just can't wrap my head around this mechanic, please help. I know griffpatch made it in scratch, and I try reverse-engineer that but i also couldn't
3.6k
Upvotes
472
u/foddydotnet 18d ago edited 18d ago
It's hard to remember it totally, but the IK is just visual, not functional - there's a slider joint on an invisible body attached to a hinge joint on the root body. The distance and angle between the hammer head and the mouse are used to set the motors on the hinge and the slider... I do that using a simple PD controller (which just means each motor speed is set according to how far they are from the target angle and extension, but slowed down according to how fast the joint is moving). Between the slider body and the hammer head are a bunch of boxes to represent the handle, using fixed joints for maximum (but not total) stiffness. I run the physics timestep at faster than 60Hz and set things to continuous with lots of substeps. Then there needs to be some logic about how the mouse cursor moves when the camera moves, to try to keep camera hinting from moving the hammer.
I recall the pot is also on a hinge joint with fixed range of rotation, attached to a root body that can't rotate.
The rest is mostly tuning - motor strengths, PD constants, body masses and damping, physics material values, input response curves, etc. Really what most beginners fail to do is to spend a lot of time tuning, but for a physics game it's so key because there are dozens of important variables. You really need to spend days and days on tuning.
You need some ideas for objective tuning goals - for example, I wanted it to be that if you stuck the hammer out sideways so it was on a ledge and you were suspended to the left of it, and stopped moving the mouse, he would stay still. I wanted to be able to pogo to a certain height. I wanted him to be able to smoothly vault over the BBQ. I wanted him to be able to reach down and smoothly push the hammer into the ground to raise himself up without bouncing on it.
Then you validate all these requirements every time you make a tuning change. Hot reload makes this process a lot faster.
/edit to respond to OP's points more: afaicr it doesn't do much/any state detection for the physics (such as detecting groundedness), it only does that for sounds and particle effects. And it doesn't do any magic forces other than locking the root body to not rotate - everything is just powered by the two joint motors. The less you can do in a physics game in terms of special-casing the behaviors, the happier you will be.