Precise collision detection with the border

To upgrade our detect method, we need to replace the return statement in the if(possibleCollision) block with the more precise detection code.

First, initialize radianAngle to be the radian equivalent of whichever direction (in degrees) our object is facing. The Math class uses radians as they are more mathematically useful in calculations than the easier to visualize degree measurement.

The variables cosAngle and sinAngle are just what the name suggests, and are used in the block of code which follows this one.

Tip

It is worth mentioning that the Math.cos() and Math.sin() methods are relatively time consuming. We can speed up our collision detection class by precomputing 360 values for both sin and cos ...

Get Android Game Programming by Example 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.