Bounding Box Collision

We saw in our preceding explanation points that Bounding Box Collision technique is one of the simplest. That's because we are simply testing for between two rectangles. Consider the following pseudo code for a better understanding:

rectangle1 = {x: 5, y: 5, width: 50, height: 50}rectangle2 = {x: 20, y: 10, width: 10, height: 10}if(rectangle1.x < rectangle2.x + rectangle2.width && rectangle1.width > rectangle2.x && rectangle1.y < rectangle2.y + rectangle2.height && rectangle1.height + rectangle1.y > rectangle2.y) {    //Bounding Box Collision Detected}// Taking the values from our variablesif (5 < 30 && 55 > 20 && 5 < 20 && 55 > 10) { // Bounding Box Collision Detected!}

As you can see from the preceding code, the mathematical ...

Get Learning Android Game Development now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.