Game Development Reference
In-Depth Information
private void updateCoins( float deltaTime) {
int len = coins.size();
for ( int i = 0; i < len; i++) {
Coin coin = coins.get(i);
coin.update(deltaTime);
}
}
In the updateSquirrels() method, we update each Squirrel instance via its update() method,
passing in the current delta time. We do the same for each Coin instance in the updateCoins()
method.
Collision Detection and Response
Looking back at our original World.update() method, we can see that the next thing we do is
check for collisions between Bob and all the other objects with which he can collide in the world.
We do this only if Bob is in a state not equal to BOB_STATE_HIT , because in that state he just
continues to fall down due to gravity. Let's have a look at those collision-checking methods in
Listing 9-18.
Listing 9-18. Excerpt from World.java; the Collision-Checking Methods
private void checkCollisions() {
checkPlatformCollisions();
checkSquirrelCollisions();
checkItemCollisions();
checkCastleCollisions();
}
The checkCollisions() method is more or less another master method, which simply invokes
all the other collision-checking methods. Bob can collide with a couple of things in the world:
platforms, squirrels, coins, springs, and the castle. For each of those object types, we have a
separate collision-checking method. Remember that we invoke this method and the slave methods
after we have updated the positions and bounding shapes of all objects in our world. Think of it as
a snapshot of the state of our world at the given point in time. All we do is observe this still image
and see whether anything overlaps. We can then take action and make sure that the objects that
collide react to those overlaps or collisions in the next frame by manipulating their states, positions,
velocities, and so on.
private void checkPlatformCollisions() {
if (bob.velocity.y > 0)
return ;
int len = platforms.size();
for ( int i = 0; i < len; i++) {
Platform platform = platforms.get(i);
if (bob.position.y > platform.position.y) {
if (OverlapTester
. overlapRectangles (bob.bounds, platform.bounds)) {
bob.hitPlatform();
 
Search WWH ::




Custom Search