Perform a hit test between two rectangles

Simple example of performing a hit test between two rectangles.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>

    /*
    
    x1 and y1 are the coordinates of the first rectangle
    w1 and h1 are the width and height of the first rectangle

    x2 and y2 are the coordinates of the second rectangle
    w2 and h2 are the width and height of the second rectangle

    If both of the rectangles will overlap, the function will return true, if not it will return false

   */

	function hitTest(x1, y1, w1, h1, x2, y2, w2, h2)
	{
	    if (x1 + w1 > x2)
	        if (x1 < x2 + w2)
	            if (y1 + h1 > y2)
	                if (y1 < y2 + h2)
	                    return true;
	               

	    return false;
	};

</script>
</body>
</html>

Responses

0 Replies