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

Javascript 让对象检测是否完全在其他对象内,javascript,canvas,collision-detection,collision,Javascript,Canvas,Collision Detection,Collision,你好,我正在用JavaScript为我的CS类制作一个游戏。我知道如何使画布中的对象相互碰撞,但我试图让一个对象检测它是否完全位于另一个对象内 If (object1.xcoord > object2.xcoord && object1.xcoord + object1.width < object2.xcoord + object2.width && object1.ycoord + object1.height < o

你好,我正在用JavaScript为我的CS类制作一个游戏。我知道如何使画布中的对象相互碰撞,但我试图让一个对象检测它是否完全位于另一个对象内

If (object1.xcoord > object2.xcoord 
     && object1.xcoord + object1.width < object2.xcoord + object2.width 
     && object1.ycoord + object1.height < object2.ycoord +object2.height) {
  alert("hi")
}
If(object1.xcoord>object2.xcoord
&&object1.xcoord+object1.width
请注意,我只需要这三个面,如果对象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;
//和宽度。 if(obect1.widthobject2.x&&object1.y>object2.y){ 返回True; } } 返回False;
//高度和宽度都必须小于包含对象的高度
//和宽度。
if(obect1.widthobject2.x&&object1.y>object2.y){
返回True;
}
}
返回False;

它是什么形状,它们都是相同的形状吗?@Viliami都是矩形它是什么形状,它们都是相同的形状吗?@Viliami都是矩形谢谢你的回答,但是你的代码只检查底部和右侧。即使对象1位于对象2的左侧,代码仍会返回true,以供您回答,但您的代码只会检查以确保底部和右侧。即使对象1位于对象2的左侧,代码仍然返回true