我正在用javascript制作一个游戏,我找不到类似函数的函数,该函数将接受2个边界框对象,如果它们相交则返回true。 我发现的所有示例对于我的小游戏来说似乎都太复杂了,而且大多数都是特定于语言的:/
//my box object
function bounding_box (x, y, width, height, rotation) {
var box = {};
box.x = x;
box.y = y;
box.w = width
box.h = height;
box.r = rotation; //degrees from origin - all objects in the game have the same rotation origin
return box;
}
function boxes_collide (a, b) {
//if collision return true
//else return false
//my box collision function at the moment
//doesn't work with rotation
return !(
((a.y + a.h) < (b.y)) ||
(a.y > (b.z + b.h)) ||
((a.x + a.w) < b.x) ||
(a.x > (b.x + b.h))
);
}
//create boxes
var boxa = bounding_box(0, 0, 5, 3, 45);
var boxb = bounding_box(1, 3, 4, 2, 90);
//test for collision
if (boxes_collide (boxa, boxb)) {
alert('collision');
}
我已经在这个小问题上被困了几个小时,任何帮助将不胜感激! :)
参数 b 没有名为 z 的属性。