Game Development Reference
In-Depth Information
In this project, we check collision with the nearest object using
the basic rectangle overlapping algorithm; however, LibGDX
also provides a callback interface for detecting collisions. The
callback approach will be helpful in a game with many rigid
bodies at motion.
An excellent article on contact listeners can be found at
http://sysmagazine.com/posts/162079/ .
Now, make the following changes to the WorldController class:
private void initLevel () {
score = 0;
scoreVisual = score;
goalReached = false;
level = new Level(Constants.LEVEL_01);
cameraHelper.setTarget(level.bunnyHead);
initPhysics();
}
private void testCollisions (float deltaTIme) {
r1.set(level.bunnyHead.position.x, level.bunnyHead.position.y,
level.bunnyHead.bounds.width, level.bunnyHead.bounds.height);
// Test collision: Bunny Head <-> Rocks
...
// Test collision: Bunny Head <-> Gold Coins
...
// Test collision: Bunny Head <-> Feathers
...
// Test collision: Bunny Head <-> Goal
if (!goalReached) {
r2.set(level.goal.bounds);
r2.x += level.goal.position.x;
r2.y += level.goal.position.y;
if (r1.overlaps(r2)) onCollisionBunnyWithGoal();
}
}
public void update (float deltaTime) {
handleDebugInput(deltaTime);
 
Search WWH ::




Custom Search