Book Image

Mastering Android Game Development

By : Raul Portales
Book Image

Mastering Android Game Development

By: Raul Portales

Overview of this book

Table of Contents (18 chapters)
Mastering Android Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
API Levels for Android Versions
Index

Rectangular bodies


The first way we are going to implement detection is through the intersection of rectangles, which is also the simplest method.

We will use the bounding rectangle of the ScreenGameObject and check if it intersects with the bounding rectangle of the other ScreenGameObject.

The bounding rectangle changes each time we update the position of the sprite and, since we may be required to check with many other objects, it is best if we recalculate it after onUpdate. We are going to make a new method called onPostUpdate and do that inside it.

We have to add a new method to ScreenGameObject.

public void onPostUpdate(GameEngine gameEngine) {
  mBoundingRect.set(
    (int) mX,
    (int) mY,
    (int) mX + mWidth,
    (int) mY + mHeight);
}

If you need to override onPostUpdate on other objects, remember to always call the super method, otherwise collisions will misbehave.

Then, when checking for collisions, we do a check for rectangular ones:

@Override
public boolean checkCollision(ScreenGameObject...