Talking about Tiny Wings game, Actionscript 3, Box2D, Flash and Game development.
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:
package {
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.MouseEvent;
public class Main extends Sprite {
public function Main() {
drawHills(2,10);
stage.addEventListener(MouseEvent.CLICK,mouseClicked);
}
private function mouseClicked(e:MouseEvent):void{
graphics.clear();
drawHills(2,10);
}
// this is the core function: drawHills
// arguments: the number of hills to generate, and the horizontal step, in pixels, between two hill points
private function drawHills(numberOfHills:int,pixelStep:int):void{
// setting a starting y coordinate, around the vertical center of the stage
var hillStartY:Number=140+Math.random()*200;
// defining hill width, in pixels, that is the stage width divided by the number of hills
var hillWidth:Number=640/numberOfHills;
// defining the number of slices of the hill. This number is determined by the width of the hill in pixels divided by the amount of pixels between two points
var hillSlices=hillWidth/pixelStep;
// drawing stuff
graphics.lineStyle(0,0xAAAAAA);
graphics.moveTo(0,480);
// looping through the hills
for (var i:int=0; i
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:
package {
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.MouseEvent;
import flash.events.Event;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class Main extends Sprite {
private var world:b2World=new b2World(new b2Vec2(0,10),true);
private var worldScale:int=30;
var worldDebugDraw:b2DebugDraw;
public function Main() {
debugDraw();
drawHills(2,10);
stage.addEventListener(MouseEvent.CLICK,mouseClicked);
addEventListener(Event.ENTER_FRAME,updateWorld);
}
private function mouseClicked(e:MouseEvent):void{
drawHills(2,10);
}
private function debugDraw():void {
worldDebugDraw=new b2DebugDraw();
var debugSprite:Sprite = new Sprite();
addChild(debugSprite);
worldDebugDraw.SetSprite(debugSprite);
worldDebugDraw.SetDrawScale(worldScale);
worldDebugDraw.SetFlags(b2DebugDraw.e_shapeBit|b2DebugDraw.e_jointBit);
worldDebugDraw.SetFillAlpha(0.5);
}
private function drawHills(numberOfHills:int,pixelStep:int):void{
world=new b2World(new b2Vec2(0,10),true);
world.SetDebugDraw(worldDebugDraw);
var hillStartY:Number=140+Math.random()*200;
var hillWidth:Number=640/numberOfHills;
var hillSliceWidth=hillWidth/pixelStep;
var hillVector:Vector.;
for (var i:int=0; i();
hillVector.push(new b2Vec2((j*pixelStep+hillWidth*i)/worldScale,480/worldScale));
hillVector.push(new b2Vec2((j*pixelStep+hillWidth*i)/worldScale,(hillStartY+randomHeight*Math.cos(2*Math.PI/hillSliceWidth*j))/worldScale));
hillVector.push(new b2Vec2(((j+1)*pixelStep+hillWidth*i)/worldScale,(hillStartY+randomHeight*Math.cos(2*Math.PI/hillSliceWidth*(j+1)))/worldScale));
hillVector.push(new b2Vec2(((j+1)*pixelStep+hillWidth*i)/worldScale,480/worldScale));
var sliceBody:b2BodyDef=new b2BodyDef;
var centre:b2Vec2=findCentroid(hillVector,hillVector.length);
sliceBody.position.Set(centre.x,centre.y);
for(var z:int=0;z, count:uint):b2Vec2 {
var c:b2Vec2 = new b2Vec2();
var area:Number=0.0;
var p1X:Number=0.0;
var p1Y:Number=0.0;
var inv3:Number=1.0/3.0;
for (var i:int = 0; i < count; ++i) {
var p2:b2Vec2=vs[i];
var p3:b2Vec2=i+1
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.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.