Thursday, February 20, 2014

Flappy Bird Game Algorithm Part 3 - Collision Detection

So far we managed to make an object flying smoothly with the adjustable gravity and the forward speeds.

Next, it comes the major part of the game: Obstacles generation and Collision Detection

Note that the obstacles (tunnels/pipes) can be generated in the similar ways as the background movement. Now the thing is, how to know if our flying object hits them?

To put it in a better picture, let's have a look at the analogy below:

Here, to investigate whether the object hits both the pipes (upward and downward), it is to check  whether the object is not successfully contained 'inside' the green box region (on the right side).

With this understanding, we only require a few information to complete the task:
a) Coordinates of the flying object + its Width and Height
b) Coordinates of the green box region  + its Width and Height

Luckily, the Width and the Height of the object and the green box are constants. All we need to perform, is a fundamental collision checking, as shown in the function below (Again, it is inside the onEnterFrame function):

public function collisionDetection(_obj1:FlyingObject, _obj2:GreenBox):Boolean
     //*Note that my flying object is pivoted in the center
     //check if it is within the pipes
_obj1.x + obj1.width/2 >= _obj2.x &&
obj1.x + obj1.width/2 <= _obj2.x + _obj2.width)

         //hit upper pipe
         if( _
obj1.y - _obj1.height/2 <= _obj2.y)   return true;
         //hit lower pipe
         if( _y1 + _
obj1.height/2 >= _obj2.y + _obj2.height) return true;


Straightforward enough. Hopefully with these ideas, you can design and customize the Flappy Bird Game in anyway you want.

Next : Flappy Bird Game Algorithm Part 4 - An Android Game Demo (Floppy Balloon)

No comments:

Post a Comment