【发布时间】:2014-04-21 15:56:15
【问题描述】:
我正在使用 HTML5 canvas 标签开发一个小游戏,我发现了一个问题 (that you can see here),为了解决它,我需要检查数组中每个矩形之间的交集(完成)和返回交叉点的位置和大小(我不能这样做,因为我的数学知识很差)。有人可以帮我吗?
这里是矩形数组碰撞检测:
for (var i = 0; i < rects.length; i++) {
for (var others = 0; others < rects.length; others++) {
if (others != i) {
if (rects[others].y + rects[others].height >= rects[i].y &&
rects[others].x + rects[others].width >= rects[i].x &&
rects[others].x <= rects[i].x + rects[i].width &&
rects[others].y <= rects[i].y + rects[i].height) {
// They're intersecting!
intery = 0; // Intersection Y
interx = 0; // Intersection X
interw = 0; // Intersection Width
interh = 0; // Intersection Height
}
}
}
}
And here is the entire code, in JSFiddle
此计算的目的是我需要禁用英雄与矩形交点内的矩形之间的任何碰撞。例如这个区域:
【问题讨论】:
-
我在stackoverflow.com/questions/32104083/… 中介绍了一些更高级的场景。
标签: javascript jquery html5-canvas collision-detection easeljs