让对象检测是否完全在其他对象内部 JavaScript

Have object detect if completely inside other object JavaScript

您好,我正在使用 JavaScript 为我的 CS class 制作游戏。我知道如何让啤酒对象在 canvas 中相互碰撞,但我试图让一个对象检测它是否完全在另一个对象内部

If (object1.xcoord > object2.xcoord 
     && object1.xcoord + object1.width < object2.xcoord + object2.width 
     && object1.ycoord + object1.height < object2.ycoord +object2.height) {
  alert("hi")
}

请注意,我只需要这三个边,如果对象 1 位于对象 2 的顶部,这对我来说并不重要

是否可以仅使用 < 或 > 之类的比较而不使用其他任何东西

//both height and width need to be smaller than the containing objects height
//and width.
if(obect1.width < object2.width && object1.height < object2.height){
    //check if right side x of object1 is less than right side x of object2
    //and check if bottom y of object1 y is less than bottom y of object2
    //and check if left x of object1 is greater than left side x of object2
    //and check if top y of object1 is greater than top y of object2
    if(object1.x+object1.width < object2.x+object2.width 
    && object1.y+object1.height < object2.y + object2.height
    && object1.x > object2.x && object1.y > object2.y){
        return True;
    }
}
return False;