Book Image

Android Game Programming by Example

By : John Horton
Book Image

Android Game Programming by Example

By: John Horton

Overview of this book

Table of Contents (18 chapters)
Android Game Programming by Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
7
Platformer – Guns, Life, Money, and the Enemy
Index

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 and then using a simple lookup method instead of this calculation.

However, we maintain our goal of over 60 frames per second, so don't do so here.

Delete the return statement and add this code in the if(possibleCollision) block:

if (possibleCollision...