after discussion with my friends
it seems that suggestion could be

if( ox < oxmin && oy < oymin )oy=oymin-h/2; // check boundary codes of blue object top side hitting the left jaw

trying not to nest the codes for ease of debugging.

will try later at home :)

need the feedback from the Ejs simulation to refine the codes further