Given a rectangle taking the shape of {x,y,w,h}
If read somewhere on the internet the fastest way to do it was:
function BoxesIntersect(a,b) { return (Math.abs(a.x - b.x) * 2 < (a.w + b.w)) && (Math.abs(a.y - b.y) * 2 < (a.h + b.h)); }Looks amazingly compact. But the result isn't very accurate. Especially if the bounding boxes differ in size much.
So I thought a bit and I came to this. Which is ALWAYS accurate. Maybe not as fast and certainly not consistently fast, because it bails out at the first sign of failure, but pretty fast anyway.
And here is the algorithm, but it would seem it would be faster, if instead of rectangles, you'd keep a minx-maxx and a miny-maxy.
function _hybridGameBoxIntersect(a,b) {
if((a.x+a.w)<b.x) return false; if((a.y+a.h)<b.y) return false; if((b.x+b.w)<a.x) return false; if((b.y+b.h)<a.y) return false; return true; }
Geen opmerkingen:
Een reactie posten