Book Image

LibGDX Game Development By Example

By : James Cook
Book Image

LibGDX Game Development By Example

By: James Cook

Overview of this book

Table of Contents (18 chapters)
LibGDX Game Development By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Collisions


Well, so far so good. However, we haven't implemented any collision handling. Luckily, LibGDX contains really useful tools to check for collision between the shapes that we are using. This is going to make our lives a lot easier.

The one class we will use is the Intersector class. To find out more, visit http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html and take a look at its API, you will see all sorts of useful methods there. However, we are only going to use two of them:

boolean overlaps(Circle c1, Circle c2)
boolean overlaps(Circle c, Rectangle r)

These will return true if the shapes overlap.

So, let's add a method to our Flower class that will take an instance of Flappee in and check the collision areas, as follows:

public boolean isFlappeeColliding(Flappee flappee) {
  Circle flappeeCollisionCircle = flappee.getCollisionCircle();
  return
  Intersector.overlaps(flappeeCollisionCircle, ceilingCollisionCircle) ||
  Intersector.overlaps(flappeeCollisionCircle...