I've been on this problem for days: how to sort objects in a 2.5d game?
Here's basic sorting based on each object's y position:
It works well in general but as you can see in the gif, if you can stand on top of objects then it breaks. This is because if you're standing on the object and you go over its y position, as it happens in the gif, it will get sorted to behind it (because that's what the sort is based on) and look wrong.
Some solutions I've tried include sorting based on the sprite's bottom y position instead of the collision boxes y position, but this also has problems:
Because the table's sprite bottom y position is lower than the boxes', it gets drawn last, which hides the boxes behind the table when it should be above it. Another issue that comes up is when you jump:
Because the y bottom is higher, it gets sorted behind when it totally shouldn't. Using the y bottom doesn't generally work for this reason. Even trying to fix the box situation by taking the z height into account doesn't fix this particular issue presented by this gif.
Another solution I've tried is go back to using the normal y position of the object but then also take z into account. If z is higher than it should be drawn on top. But this presents another problem:
Here since the box is on top of the table its z height is higher than the player's, so its drawn last, but obviously it looks wrong.
... Does anyone know how to fix this problem in a generic and possibly reasonable way? One solution that I think would work would be to divide objects up into multiple pieces, but I'm trying to avoid this unless it's impossible to find something else because it'd make everything a lot more complex.