Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

Create a terrain like the one in Tiny Wings with Flash and Box2D

Do you know Tiny Wings?

It’s a cute and addictive game for iPhone featuring a bird with tiny wings – too tiny to fly – which tries to fly anyway using a procedural generated terrain as a ramp.

What makes the game interesting is the physics response of the bird running along slopes.

At a first glance it seems the author is using some kind of physics engine which allows the creation of curves. But at a closer look (or playing on an iPad with zoom mode) we can see hills are made by a series of segments.

So we are going to reproduce such hills with Box2D. But before we dive into physics, some theory.

Tiny Wings hills are generated by trigonometric functions. The more complex the functions, the more interesting the result. In my example I am using a simple cosine, but feel free to try your own formulas and send me your results. I will be happy to publish them.

Once you defined your function, you’ll obviously end with a curve.

The trick is to divide such curve into an amount of slices, having the curve made of segments rather than points.

The higher the number of segments, the better will look the curve, the more CPU consuming will be the game.

In this example, I am assuming I want two hills in a 640×480 stage, with a slice width of 10 pixels.

I won’t use any physics engine, I will just draw two hills:

The script is quite simple and it’s fully commented, and produces this result:

Click on the movie to generate new hills.

Now it’s time to achieve the same results using Box2D. We can say slices are polygons made by four vertices, and we know the coordinates of all vertices, so we just have to create a series of static bodies built starting from such vertices.

To create the polygons, we’ll use the same concept seen in the second part of the slicing tutorial, storing vertices in a Vector, finding polygon centroid starting from such Vector and finally drawing it and placing in the right place.

And this is the script:

The concept is the same of the previous script, we are just building Box2D polygons rather than drawing them on the stage.

And this is the result:

Click on the movie to generate new hills.

Now I have two questions:

1) How would you produce more various shaped hills?

2) How would you scroll/zoom the hills?

Think, meanwhile download the full source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (32 votes, average: 4.88 out of 5)
Loading ... Loading ...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 24 comments

  1. Chris

    on July 14, 2011 at 11:45 pm

    Interesting article!
    It’s nice to see your ideas on how it was achieved and spreading it :)
    I’ve come across a nice github project that was actually trying to achieve the effect aswell and has a really nice result:
    https://github.com/haqu/tiny-wings#readme

    Maybe it would be nice to try to port that into Flash since it was accomplished using Box2D aswell :]

  2. davidp

    on July 14, 2011 at 11:46 pm

    wow, this is awesome! great article :)

  3. Chris Moeller

    on July 15, 2011 at 1:52 am

    Very nice! I’m also going to be going through your procedural textures tutorials- going to be using them a lot for a big project in the near future.

    Thanks for posting this! Lots of ideas based on these two!

  4. Ovidiu

    on July 15, 2011 at 11:15 am

    Awesome, awesome article! Never thought this would be so simple!
    Thank you, really!

    Keep up!

  5. Fintan

    on July 15, 2011 at 12:06 pm

    cool tutorial. how would you get the tiny bird to always be oriented to the slope of the hill if it was in contact with it?

  6. Ramon Fritsch

    on July 15, 2011 at 4:44 pm

    Isn’t it too performance intence to have a that many polygons running on box2d inside flash? I’m wondering if you could group some polygons at the top of the hills. I’ve created a game with smooth slopes like that but I couldn’t realise what was the best way to reproduce with a smooth framerate.

    Cheers

  7. Emanuele Feronato

    on July 15, 2011 at 11:56 pm

    @chris: the demo looks god, I’ll try to port it into AS3

    @ramon: do you remove polygons when as they leave the stage on the left side?

  8. Farzad

    on July 16, 2011 at 7:56 am

    Awesome!

  9. This Week on Twitter and Google+ | Flash Video Traning Source

    on July 17, 2011 at 11:02 pm

    [...] this post, Emanuele Feronato explains how to use trigonometric functions to build a terrain that seems [...]

  10. patrik

    on July 18, 2011 at 11:37 am

    cool, thanks for sharing!

  11. Ant

    on July 18, 2011 at 9:12 pm

    Purely for the shape generation (no box2d stuff) you could use the BezierSegment class in Flash for smooth and efficient curves

  12. Aeon

    on July 19, 2011 at 2:39 am

    Very nice article!! \o/

    This same technic is used into cyclomaniacs?

    http://www.kongregate.com/games/LongAnimals/cyclomaniacs

    “you remove polygons when as they leave the stage on the left side?”

    How manage the npcs?

  13. Eric

    on July 20, 2011 at 8:27 am

    How about using fractals to generate random and cool looking hills?

    I just found this article while doing some oter research and thought it might be perfect to use for this:
    http://www.gameprogrammer.com/fractal.html#ptII

    You probably then can smooth it out to get more “slidy” hills than just jagged sharp scary hills.

  14. How to create a game like Tiny Wings? Links | sabawgames

    on July 25, 2011 at 4:41 am

    [...] http://www.emanueleferonato.com/2011/07/14/create-a-terrain-like-the-one-in-tiny-wings-with-flash-an… [...]

  15. astro75

    on July 26, 2011 at 1:22 pm

    http://www.sideroller.com/wck/ – animated line arcs demo. Those arcs would improve performance.

  16. Janitha

    on September 1, 2011 at 6:33 am

    Just need to know what the “|” does in the following piece of code.

    worldDebugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit);

  17. carmine

    on September 24, 2011 at 10:06 am

    i need help please , what program do I need to do this ?

  18. Flash Tutorials von Emanuele Feronato | senäh

    on October 1, 2011 at 12:02 am

    [...] Zombies. Als heute morgen im Google Reader ein neuer Artikel von mir aufpoppte, in dem er versucht Tiny Wings als Flashspiel umzusetzen, musste ich das einfach als Anlass nehmen hier mal eine Empfehlung [...]

  19. Javascript physics engine and simulated infinite curve

    on May 23, 2012 at 8:19 pm

    [...] first saw a technique using Box2D, I’m using the closure-web version (because of the memory leaks fix). In short, I explode the [...]

  20. Javascript physics engine and simulated infinite curve | PHP Developer Resource

    on May 29, 2012 at 11:01 am

    [...] first saw a technique using Box2D, I'm using the closure-web version (because of the memory leaks fix). In short, I explode the curve [...]

  21. Creating deformable terrain in cocos2dx with box2d – part 1 | JiffyApps

    on July 2, 2012 at 9:08 pm

    [...] is here: http://www.emanueleferonato.com/ and her tutorial for creating terrain can be found here: http://www.emanueleferonato.com/2011/07/14/create-a-terrain-like-the-one-in-tiny-wings-with-flash-an…  That tutorial may be hard to understand for those using cocos2dx and is not as easy as reading a [...]

  22. Javascript physics engine and simulated infinite curve - Tech Forum Network

    on June 7, 2013 at 11:34 am

    [...] first saw a technique using Box2D, I’m using the closure-web version (because of the memory leaks fix). In short, I explode the [...]

  23. Javascript physics engine and simulated infinite curve | Ask Programming & Technology

    on November 8, 2013 at 11:18 am

    [...] first saw a technique using Box2D, I’m using the closure-web version (because of the memory leaks fix). In short, I explode the [...]

  24. Azareal

    on May 5, 2014 at 1:55 am

    What version of Box2D are you using for this?

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×