I'm making a game in javascript and I cant find anything like a function that will take 2 bounding box objects and return true if they intersect. all the examples I've found seem far too complex for my little game and most are language specific :/
//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');
}
I've been stuck for hours on this little problem, any help would be appreciated! :)