[英]JavaScript collision detection not working with simple rectangle
I'm trying to create a on load random background with triangles. 我正在尝试创建一个带有三角形的负载随机背景。 But I'm trying to exclude any collision and if there is any revoke the
createDiamond()
function. 但是我试图排除任何冲突,如果有的话,请撤销
createDiamond()
函数。
I tried it with various functions from: Javascript: Collision detection 我使用以下各种功能进行了尝试: Javascript:碰撞检测
Here is my current code: https://codepen.io/mikeyhaklander/pen/OeONOz My current collision code is: 这是我当前的代码: https : //codepen.io/mikeyhaklander/pen/OeONOz我当前的碰撞代码是:
{
(x <= cx && x + size >= cx && y <= cy && y + size >= cy)
|| (x <= cx + cSize && x + size >= cx + cSize && y <= cy && y + size >= cy)
|| (x <= cx && x + size >= cx && y <= cy + cSize && y + size >= cy + cSize)
|| (x <= cx + cSize && x + size >= cx + cSize && y <= cy + cSize && y + size >= cy + cSize)
}
When refreshing the codepen, you can see it still sometimes collides. 刷新代码笔时,您有时仍会看到它碰撞。
Got it fixed myself. 搞定了我自己。 I was calculating with the wrong sizes.
我使用错误的尺寸进行计算。 In css I used bigger sizes then I gave the object with JavaScipt.
在css中,我使用了更大的尺寸,然后使用JavaScipt给出了对象。 And then I've got the collision correct with this formula:
然后,我使用以下公式纠正了碰撞:
if ( x < cx + cSize && x + size > cx && y < cy + cSize && y + size > cy ) {
return createDiamond(diamondArray, bgHeight, parent)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.