I have done the leg rig for a character, following some online tutorials. Somehow, mine is not behaving completely like those in the tutorials. I get this odd flips and I have no idea how to start stabilizing the rig. see below:
You might want to fix your bone roll first.
In edit mode you select all bones and press ctrl+n and let the Z axis recalculate.
After that is fixed, you need a locked track constraint to lock the y roll (along the bone axis) of your upper and lower leg.
I assume foot.l is your IK tip.
Duplicate the lower leg bone in edit mode, move it in front of the knees in profile view, be sure not to rotate the bone in any manor.
After that parent it to your foot.l.
Same for the other side if you havenĀ“t got x-mirror on already.
Back in pose mode, all looks wonky, thats because the lower leg duplicate that is your knee now, still has an IK modifier. delete it.
After that, select the kneebone and shiftselect the lower leg bone. Press ctrl+alt+c. Add a locked track modifier. if you reset your boneroll, the default settings should be right, track to Y, lock to Z if i remember correctly.
Repeat for upper leg. Select kneebone+upper legbone, add locked track. repeat for other side.
if you move your ik bone now, the parented kneebone moves along, and the lower and upper legbone always track to it, with locked axis preventing this flipping. also moving the kneebone in frontview makes the ik leg track towards the kneebone.
There are two other reasons when this flipping appears, if your Z axis is wonky it often happens and if your bones are all straight when you apply a IK chain, then the solver is not sure how to solve it
You will most likely notice with your current setup the flip will occur once in profile view you move the ik tip bone past the kneebone. then the whole chain flips.
In the IK chain there is also a lock mechanism. IK chains work in quaternions, so the regular lock axis constraint does not work for them. Once you applied an IK chain, you get a new option in the edit tab under armature bones, where you can lock and limit axes in an IK chain. like for the lower leg bone, once fixing the boneroll, a knee obviouly can only rotate about one axis without major surgery. so lock Y and Z for the lower leg bone. also fixes a lot of issues.
hth.