# Beginner’s Guide to Augmented Reality Part 2

Welcome to Part 2 of my Beginner's Guide to Augmented Reality, I hope you finished off the first part of this tutorial, or at least downloaded and read the source files (otherwise you may get a bit confused with what's going on).

## Quick Recap

Last time we looked at setting up a simple AR environment, creating a cube, applying materials to the cube and the "hole in the wall" effect.

Today we will be building on that knowledge and creating the final as shown in the demo. To create that final effect, we need to know how to render spheres, animate objects, play sound effects and finally, render 3d objects. Rendering 3D objects in the palm of your hand is the main reason why Augmented Reality has become a big hit this year, especially when you create some of the more interesting shapes or animate them. If you are in the UK you may have seen The Gadget Show recently where they featured Augmented reality and had an animated version of Suzi Perry that you could hold in your hand. Very nice.

So let's get down to business. I'll be starting with the world and stars render first of all, then moving on to animating the shapes and finally creating the cow. Open up your files from last time and let's get creating.

Source: NASA Visible Earth. Credit: Reto Stöckli, Robert Simmon.

Download the 2048x1024px version of the image above from the NASA Visible Earth page and call it map.jpg. Place it into the following folder: deploy > assets. This is the same folder where you saved the images for the inside of the cube in the last part of the tutorial. This lovely image of the world came from NASA. They take good photos don't they?

## Step 2: Creating a Sphere

In your code, navigate to where we set up the "hole in the wall" effect for Marker 0 last time. Just after the "hole in the wall" code, add in the following code:

 1 2 var Earth:Sphere = new Sphere(earth, 40);  3 Earth.z=200;  4 dispObj.addChild(Earth); 

## Step 3: Mapping the World

Now that we have set up a sphere, it needs a texture. This is done in the exact same way as mapping a texture to a cube. Notice how in the set up for the sphere, it's looking for something called "earth" this is our texture variable.

Navigate in the code to where we set up materials before. Add to that code the following line:

 1 2 var earth : BitmapFileMaterial = new BitmapFileMaterial("assets/map.jpg"); 

Just the same as the others.

## Step 4: Test it!

When you test it, you should see something like the following image (but of course without that handsome devil holding up the marker for you):

Just as last time, you'll need to download the marker image and print it off with plenty of white space around the edges.

## Step 5: Stars in Your Eyes

Now what would a space image be without some stars? Let's add some in shall we? This is possibly the most tedious part for me to write up since all the stars need to be individually positioned. But you cheeky rascals get the benefit of copy and paste.

Add in the following code after the earth render code:

 1 2 var star1:Sphere = new Sphere(star, 4);  3 star1.z=65;  4 star1.x=84;  5 star1.y=164;  6 dispObj.addChild(star1);  7 8 var star2:Sphere = new Sphere(star, 3);  9 star2.z=246;  10 star2.x=32;  11 star2.y=64;  12 dispObj.addChild(star2);  13   14 var star3:Sphere = new Sphere(star, 2);  15 star3.z=163;  16 star3.x=78;  17 star3.y=98;  18 dispObj.addChild(star3);  19 20 var star4:Sphere = new Sphere(star, 4);  21 star4.z=120;  22 star4.x=164;  23 star4.y=157;  24 dispObj.addChild(star4);  25 26 var star5:Sphere = new Sphere(star, 2);  27 star5.z=148;  28 star5.x=-164;  29 star5.y=-157;  30 dispObj.addChild(star5);  31   32 var star6:Sphere = new Sphere(star, 3);  33 star6.z=46;  34 star6.x=-36;  35 star6.y=-156;  36 dispObj.addChild(star6);  37 38 var star7:Sphere = new Sphere(star, 5);  39 star7.z=40;  40 star7.x=-16;  41 star7.y=-84;  42 dispObj.addChild(star7);  43   44 var star8:Sphere = new Sphere(star, 5);  45 star8.z=59;  46 star8.x=-84;  47 star8.y=30;  48 dispObj.addChild(star8);  49 50 var star9:Sphere = new Sphere(star, 4);  51 star9.z=87;  52 star9.x=-134;  53 star9.y=84;  54 dispObj.addChild(star9);  55 56 var star10:Sphere = new Sphere(star, 2);  57 star10.z=49;  58 star10.x=10;  59 star10.y=18;  60 dispObj.addChild(star10);  61 62 var star11:Sphere = new Sphere(star, 5);  63 star11.z=94;  64 star11.x=-84;  65 star11.y=41;  66 dispObj.addChild(star11);  67 68 var star12:Sphere = new Sphere(star, 3);  69 star12.z=54;  70 star12.x=91;  71 star12.y=-46;  72 dispObj.addChild(star12);  73 74 var star13:Sphere = new Sphere(star, 2);  75 star13.z=180;  76 star13.x=88;  77 star13.y=-130;  78 dispObj.addChild(star13);  79 80 var star14:Sphere = new Sphere(star, 4);  81 star14.z=102;  82 star14.x=134;  83 star14.y=-13;  84 dispObj.addChild(star14);  85 86 var star15:Sphere = new Sphere(star, 1);  87 star15.z=61;  88 star15.x=-35;  89 star15.y=145;  90 dispObj.addChild(star15); 

Phew, that"s quite a lot. Of course, you can add more if you want more stars. Or just use a loop an array to add an arbitrary number.

## Step 6: Color the Stars

Now we need to add a material for the stars. Add the following with the other material code:

 1 2 var star : ColorMaterial = new ColorMaterial(0xFFFFFF); 

## Step 7: Testing Time!

Test the flash movie and hopefully you'll see something like this:

## Step 8: Trapped in a Box

Wouldn't it be cool if, say, we could have the space scene we just created, be trapped in a little box held within the marker and then explode out? Well you're in luck, and probably saw the outcome as that's exactly what we are going to do.

It's fairly simple to animate things in AR. Especially if you just want to move them from one point to another like we want to. To do this, we need to download some extra classes for Flash. GreenSock do a very nice series of animation libraries that help us easily move objects from one point to another.

## Step 9: Importing a new Library

Once you have downloaded the TweenMax library, you need to extract the .zip file and place the greensock folder into src > com of our project. This is where the squidder library is also kept. Extract here and all will be just swell.

Now move back over to Flash. Add the following line at the top of your code with the other import lines:

 1 2 import com.greensock.*; 

## Step 10: Animating the Earth

Find the code where you set up the Earth variable. Replace it with the following code:

 1 2 var Earth:Sphere = new Sphere(earth, 1);  3 Earth.z=-40;  4 TweenMax.to(Earth, 4, {scaleX:40, scaleY:40, scaleZ:40, z:"200", delay:4}); 

What I've done here is alter the Earth's starting point and size so that it's very small and inside the box. TweenMax is a great animation class. Here we set up which variable to animate (Earth), how long it will take in seconds (4), by how much it should scale up the variable, its z point and finally how long it should wait before animating. This is set to 4 so that we can animate other things first.

## Step 11: Animating the Stars

Replace all your star setup code with the following:

 1 2 var star1:Sphere = new Sphere(star, 1);  3 star1.z=-40;  4 TweenMax.to(star1, 4,{scaleX:1, scaleY:1, scaleZ:1, x:"84", y:"164", z:"65", delay:4});  5 dispObj.addChild(star1);  6   7 var star2:Sphere = new Sphere(star, 1);  8 star2.z=-40;  9 TweenMax.to(star2, 4,{scaleX:3, scaleY:3, scaleZ:3, x:"32", y:"64", z:"246", delay:4});  10 dispObj.addChild(star2);  11   12 var star3:Sphere = new Sphere(star, 1);  13 star3.z=-40;  14 TweenMax.to(star3, 4,{scaleX:2, scaleY:2, scaleZ:2, x:"78", y:"98", z:"163", delay:4});  15 dispObj.addChild(star3);  16   17 var star4:Sphere = new Sphere(star, 1);  18 star4.z=-40;  19 TweenMax.to(star4, 4,{scaleX:4, scaleY:4, scaleZ:4, x:"164", y:"157", z:"120", delay:4});  20 dispObj.addChild(star4);  21   22 var star5:Sphere = new Sphere(star, 1);  23 star5.z=-40;  24 TweenMax.to(star5, 4,{scaleX:2, scaleY:2, scaleZ:2, x:"-164", y:"-157", z:"148", delay:4});  25 dispObj.addChild(star5);  26   27 var star6:Sphere = new Sphere(star, 1);  28 star6.z=-40;  29 TweenMax.to(star6, 4,{scaleX:3, scaleY:3, scaleZ:3, x:"-36", y:"-156", z:"46", delay:4});  30 dispObj.addChild(star6);  31   32 var star7:Sphere = new Sphere(star, 1);  33 star7.z=-40;  34 TweenMax.to(star7, 4,{scaleX:5, scaleY:5, scaleZ:5, x:"-16", y:"-84", z:"40", delay:4});  35 dispObj.addChild(star7);  36   37 var star8:Sphere = new Sphere(star, 1);  38 star8.z=-40;  39 TweenMax.to(star8, 4,{scaleX:5, scaleY:5, scaleZ:5, x:"-84", y:"30", z:"59", delay:4});  40 dispObj.addChild(star8);  41   42 var star9:Sphere = new Sphere(star, 1);  43 star9.z=-40;  44 TweenMax.to(star9, 4,{scaleX:4, scaleY:4, scaleZ:4, x:"-134", y:"84", z:"87", delay:4});  45 dispObj.addChild(star9);  46   47 var star10:Sphere = new Sphere(star, 1);  48 star10.z=-40;  49 TweenMax.to(star10, 4,{scaleX:2, scaleY:2, scaleZ:2, x:"10", y:"18", z:"49", delay:4});  50 dispObj.addChild(star10);  51   52 var star11:Sphere = new Sphere(star, 1);  53 star11.z=-40;  54 TweenMax.to(star11, 4,{scaleX:5, scaleY:5, scaleZ:5, x:"-84", y:"41", z:"94", delay:4});  55 dispObj.addChild(star11);  56   57 var star12:Sphere = new Sphere(star, 1);  58 star12.z=-40;  59 TweenMax.to(star12, 4,{scaleX:3, scaleY:3, scaleZ:3, x:"91", y:"-46", z:"54", delay:4});  60 dispObj.addChild(star12);  61   62 var star13:Sphere = new Sphere(star, 1);  63 star13.z=-40;  64 TweenMax.to(star13, 4,{scaleX:2, scaleY:2, scaleZ:2, x:"88", y:"-130", z:"180", delay:4});  65 dispObj.addChild(star13);  66   67 var star14:Sphere = new Sphere(star, 1);  68 star14.z=-40;  69 TweenMax.to(star14, 4,{scaleX:4, scaleY:4, scaleZ:4, x:"134", y:"-13", z:"102", delay:4});  70 dispObj.addChild(star14);  71   72 var star15:Sphere = new Sphere(star, 1);  73 star15.z=-40;  74 TweenMax.to(star15, 4,{scaleX:1, scaleY:1, scaleZ:1, x:"-35", y:"145", z:"61", delay:4});  75 dispObj.addChild(star15); 

This will animate your stars from a hidden start point to their final positions. Again, I've altered the stars' z-positions so that they start inside the box.

## Step 12: Test it!

Let's test the Flash movie again. You should have a nice animated transition from nothing to the earth and stars exploding out of the box we trapped them in.

## Step 13: It's all a Cover up!

For the box lid that will open up and have the Earth and stars explode out, we will use four images. I use a wooden panel look for my box but feel free to create your own. Here are the images I made, download these and save them into deploy > assets.

Save as top.png

Save as bottom.png

Save as left.png

Save as right.png

## Step 14: Create the Box Lid

Beneath where we set up the stars, add the following code:

 1 2 var top:Cube = new Cube( new MaterialsList( {all: Top} ) , 80 , 0 , 80 );  3 top.z=0;  4 top.y=40;  5 TweenMax.to(top, 2,{rotationX:-180, delay:2});  6 dispObj.addChild(top);  7   8 var bottom:Cube = new Cube( new MaterialsList( {all: Bottom} ) , 80 , 0 , 80 );  9 bottom.z=0;  10 bottom.y=-40;  11 TweenMax.to(bottom, 2,{rotationX:180, delay:2});  12 dispObj.addChild(bottom);  13   14 var left:Cube = new Cube( new MaterialsList( {all: Left} ) , 80 , 0 , 80 );  15 left.z=0;  16 left.rotationZ=90;  17 left.x=-40;  18 TweenMax.to(left, 2,{rotationX:180, delay:2});  19 dispObj.addChild(left);  20   21 var right:Cube = new Cube( new MaterialsList( {all: Right} ) , 80 , 0 , 80 );  22 right.z=0;  23 right.x=40;  24 right.rotationZ=90;  25 TweenMax.to(right, 2,{rotationX:-180, delay:2});  26 dispObj.addChild(right); 

This sets up all the parts that make up the box lid, positions them correctly and animates them. Great stuff.

Each "cube" is set to have a depth of zero (the third argument in the constructor), so they appear to be flat planes.

## Step 15: Lid Material

In the material section, add the following code:

 1 2 var Top : BitmapFileMaterial = new BitmapFileMaterial("assets/top.png");  3 var Bottom : BitmapFileMaterial = new BitmapFileMaterial("assets/bottom.png");  4 var Left : BitmapFileMaterial = new BitmapFileMaterial("assets/left.png");  5 var Right : BitmapFileMaterial = new BitmapFileMaterial("assets/right.png"); 

Now the lid will look the part. I'm sure you noticed that the left and right images are rotated and then I rotate them back again in the code in Step 14. You are probably wondering why I did that instead of just leaving the images as they were. Well, there's a reason for that. They won't work correctly unless you rotate them. It's an odd bug, but the images turn up backwards when rendered and when you apply an animation to them they go the wrong way. Rotating them first and then back in the code makes them work as they should. It's quite odd, but there you go.

## Step 16: Testing!

Now all the parts are ready for another test, it's all coming into place. I'm sure you're making all sorts of oohing noises right now. Marvelous.

## Step 17: This Needs Some Drama

If you're like me, you're looking at this thinking, wow that's great, but it really needs some more drama. Well we're going to do just that. Let's add some dramatic music when the box opens.

Go to the Hollywood Edge sound effects library and download BrightPad.wav. (It's not included in the Source download.) I've converted this to an MP3 called "dramatic.mp3" but you can follow these instructions while keeping it as a WAV.

Save it to the folder deploy > assets.

## Step 18: Adding in the Sound

Open up the .fla file and go to File > Import > Import to Library. Import dramatic.mp3.

Open up your library and you should see your newly imported file sitting right there.

Right-click on it and click on Properties. Check the "Export for ActionScript" box. The class box should now become active; type in it "dramatic" without the quotes.

## Step 19: More Coding

Return to the .as file. At the top of the file, find the import code. Add the following import code:

 1 2 import flash.media.SoundMixer;  3 import flash.media.SoundChannel; 

A little further down, there are some private vars. You may remember this from last time when we set up the green cube. Add the following code to the private vars:

 1 2 private var drama:dramatic = new dramatic();  3 private var dramaChnl:SoundChannel = new SoundChannel(); 

Now what we have done here is to set up the sound file "dramatic" and a sound channel. The sound channel allows starting and stopping of the sound through code.

## Step 20: Playing the Sound

Since we only want the sound to play once (when the box opens and not every time we show the marker) you need to put the following code with all the shape set up code. I put mine just after I set up the box lid.

 1 2 dramaChnl = drama.play(0,1); 

This plays the sound once when the lid opens.

## Step 21: Testing!

The sound should now play and hopefully it will feel very dramatic indeed.

You should feel very pleased with yourself right now, you have created something you can impress your friends with.

## Step 22: Cows!

Now we get to the part where we render a 3d cow that you can hold in your hand. First of all, you need to download the following two files, save them in the usual place:

The texture, which you should save as Cow.png and the cow model file which you need to save as cow.dae.

## Step 23: Setting up the Cow

 1 2 import org.papervision3d.objects.parsers.Collada; 

Find the private vars a little further down and add the following:

 1 2 private var cowSkin: BitmapFileMaterial;  3 private var cowMat: MaterialsList;  4 private var cow: Collada; 

## Step 24: More Setting Up

We are going to load the cow up onto a second marker. This is to show you that this method of loading different objects onto different markers is fairly robust and can handle not only shapes but 3d complex objects too.

Remember in the previous part of the tutorial, we had four markers on a single sheet of paper, and created different colored cubes for each marker? We're going to re-use that code to let us use a separate marker for the cow and the Earth.

Navigate through your file until you find else if (id ==1){ - the check for the second marker.

Replace everything within the two curly brackets with the following code:

 1 2 cowMat = new MaterialsList();  3 cowSkin = new BitmapFileMaterial("assets/Cow.png");  4 cowMat.addMaterial(cowSkin,"all");  5   6 //Create the new Collada Object with cowMat  7 cow = new Collada("assets/cow.dae",cowMat);  8 cow.rotationX = 90;  9 cow.scale = 0.5;  10 dispObj.addChild(cow); 

## Step 25: Testing!

Yes, more testing already, wasn't that quick. Download and print out the second marker. If all goes well, you should be the proud owner of a new little 3d cow. Congratulations!

## Step 26: Is That Cow on Moo-t?

It's great that we have such a nice looking cow, but wouldn't it be better if the cow were to moo?

Go to this directory of files from a CD called, The Best Of Tucows, Volume 2 - and download MOO.WAV. (It's not included in the Source download.)

Save it in the usual place as moo.wav.

Head on over to your .fla file and import the file to your library. Just like you did with the last sound file you imported, open up its properties, tick Export for ActionScript and change its class to mooSnd.

## Step 27: Moo-sic to my Ears

Navigate to the private vars and add the following lines of code:

 1 2 private var moo:mooSnd = new mooSnd();  3 private var mooChnl:SoundChannel = new SoundChannel(); 

Now, for this marker, I want the sound to play every time the cow appears but to only play once. To do this, you need to add the code to play the sound in a slightly different place than we did last time.

Find this line in your code:

 1 2 private function _addCube( id:int , index:int ) : void { 

We want to add our code just after this. By taking the following out of the shape set up code, it will play every time that ID is found rather than when the shape is set up, which only happens once.

 1 2 if(id==1){  3  mooChnl = moo.play(0, 1);  4 } 

## Conclusion

Test the file for the last time and the cow should moo. You can even use both markers at the same time and be super swish.

I hope you've enjoyed this two part tutorial on Augmented Reality and hope that you can go off now and create some very cool things.