O'Reilly logo

Learning Libgdx Game Development by Andreas Oehlke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding the game logic

The next step would be to add the game logic that constitutes the rules of our game world. However, the game logic will need to be able to detect the so-called collisions between two game objects before it can handle all of our events, such as walking over an item to collect it. So, we will implement a very basic collision detection method that tests two overlapping rectangles. If an overlap is detected, it means that there is also a collision between those two tested objects. So, we can bind a certain action to this event in the game logic to handle collisions as required.

Adding collision detection

First, add the following import lines to WorldController:

import com.badlogic.gdx.math.Rectangle; import com.packtpub.libgdx.canyonbunny.game.objects.BunnyHead; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required