Adding Collision Detection

Now that you’ve had a chance to have a bit of fun and drag the mallet around, you’ve probably noticed our first problem: the mallet can go way out of bounds, as seen in Figure 46, An out-of-bounds mallet. In this section, we’ll add some basic collision detection to keep our mallet where it belongs. We’ll also add some really basic physics to let us smack the puck around the table.

images/Touch/MalletOutOfBounds.png
Figure 46. An out-of-bounds mallet

Keeping the Player’s Mallet Within Bounds

Let’s start off by adding the following bounds definitions to AirHockeyRenderer:

AirHockeyTouch/src/com/airhockey/android/AirHockeyRenderer.java
 
private​ ​final

Get OpenGL ES 2 for Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.