How do I make a character's head swing on two opposite hinges without clipping into the body near 45°?

Godot Version

v4.1.1

Question

After having committed (on paper) what is likely a horrific mathematical crime, I eagerly replaced my previous robot-head-tilt code with it all at once and pressed play. The results were baffling and I have been struggling with this problem for days now.

CONTEXT:
My game’s pc is this short little robot with a head mounted on two hinges at the front and back of the torso. When the player looks down, the head swings forward following the camera rotation. When the player looks up, the head swings back.


The original code for this. To be completely honest I’ve forgotten how exactly it works, but it does, mostly. The only issue is that the headbox clips into the torso segment when not at the extremes of its swing, due to y being on a straight gradient rather than a circular calculation. I’ll assume that makes sense


My utterly genius rewrite

Observable issues with the rewrite:

  • the head’s z-shift (to account for the hinge placements) is backwards somehow
  • the head appears to swing around a point above it
  • when facing a certain angle downward the head refuses to go further in that direction and stops following the rest of the body (because of this, the head can only face up or nearly so)
    ^the above point only happens after first mouse input, I should add

So,
a. what have I done
b. how to achieve the desired effect

TIA, Orange