javascript - 让对象检测是否完全在其他对象 JavaScript 中

标签 javascript canvas collision-detection collision

您好,我正在使用 JavaScript 为我的 CS 类(class)制作游戏。我知道如何让 ale 对象在 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;

关于javascript - 让对象检测是否完全在其他对象 JavaScript 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40945374/

相关文章:

python - Tkinter : Make canvas draggable

ios - 是否有更准确的方法来使用 Sphero API 检测球与球之间的碰撞?

java - 不同角色速度的 Pacman 碰撞检测?

javascript - Meteor - 只发布集合的计数

javascript - 如何在 Angular 中为自定义验证器添加自定义验证错误消息

javascript - 在 Javascript 中监听 Android 中的后退按钮

javascript - Firefox、Selenium、toDataURL、Uint8ClampedArray 和 Python

canvas - 当 "globalCompositionOperation"为 false 时,使用 'source-atop' 设置为 "objectCaching"的屏蔽不起作用

c++ - 碰撞解决 - 圆外点

JavaScript Onclick 在标签中不起作用