Develop a Flash game like Angry Birds using Box2D – Killing the pigs

Do you know why Angry Birds had such as a worldwide success? Because people love to kill pigs in videogames.

I got sick when I saw Babe movie and I want to kill pigs since then.

Today, you will learn how to do it thanks to the second part of the Angry Birds tutorial.

Before we start, let me explain how we are going to kill them. A pig dies when something, including the bird, the objects in the game, and even the ground, hits it too hard.

Various objects in the game also break when they hit something too hard.

You are already able to see when two objects collide thanks to tutorials like Create a Flash prototype of The Moops – Combos of Joy, but we’ve never seen how to determine the force of the collision.

You still have to create a custom contact listener class, but rather than using BeginContact function which just has an argument we can use to determine which objects collided, we will use PostSolve, which has another argument, a b2ContactImpulse variable.

With b2ContactImpulse we can know the amount of impulse Box2D has to give to bodies to avoid them from penetrating each other due to the collision. In other words, the collision force.

Once we know the strength of the collision, we can compare it with the maximum strength allowed before an object breaks (which can also be different according to the material, such as glass, wood, stone) and if the collision will break the object, we simply remove it from the Box2D world, and then we’ll show a cute animation.

This is what I made with this concept:

Throw the pigeon to the castle on the right to see how all blocks (and the pig) which are hit hard get destroyed.

In this prototype, world boundaries and the bird itself can’t be destroyed.

This is the Main class:

and this is the custom contact listener:

Next time, I’ll add some scrolling, and you’ll see how you can have a working prototype of Angry Birds in a bunch of lines.

Download the source code.

  • This is very cool! Nice use of physics…

  • tlearsakura

    Very nice example !!

  • luis


  • Kris

    Hello Sir, i just want to ask, how to removeChild the object inside the currentBody,
    example : i create a Box MovieClip for the blocks, and if the block is destroy i want it to be remove in stage also..


  • Awesome addition to part 1 of this tutorial.

  • Janitha

    I’m new to Box2D so pardon me, is the b2ContactListener class no longer available or has it been renamed to something different?

  • prezire

    awesome tutorial!

    it’s the structure of this tutorial’s package. just point your publish settings to the angrybirds2 directory and you should be fine.