Say I have two tables: rect1, and rect2. Each one follows the format of x,y,width,height.
For x, the bigger the number, the farther right it is. for y, the bigger the number, the farther down it is.
var rect1 = [20,40,80,60];
var rect2 = [10,30,67,10];
How would I check if these are colliding? I know it is math related, I am just having issues thinking at the moment.
It should be something along the lines of:
var rect1 = [20, 40, 80, 60];
var rect2 = [10, 30, 67, 10];
function checkCollision(rect1, rect2) {
var x1 = rect1[0];
var y1 = rect1[1];
var w1 = rect1[2];
var h1 = rect1[3];
var x2 = rect2[0];
var y2 = rect2[1];
var w2 = rect2[2];
var h2 = rect2[3];
if (x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2) {
return true;
} else {
return false;
}
}
console.log(checkCollision(rect1, rect2));