12. Animation using Tweener


Hi,

In this tutorial we're going to use Tweener to move a cube around. This will teach you basics of animation, and how to use Tweener (which is awesome).

The example that I've produced is this:

So, in this example our solid red cube is trying to stay inside our green wireframe cube. Every time the red cube gets itself positioned completley inside the green cube, the green cube moves.

Tweener is the package which is smoothly moving the red cube around. The Tweener package is also firing the event to move the green cube when the tween is complete.

1: To start off, you'll need Tweener. You can download it here if you don't already have it: http://code.google.com/p/tweener/

Once you've got tweener, either add the source directory containing the "caurina" folder to your project's classpaths, or add the caurina folder into your current project.

2: Import tweener - Add this line to your imports:

import caurina.transitions.Tweener;

3:  Add a Tween. You can add tweens to any parameter of any object. The Tweener package will gradually change any parameter in your code to a new value over time, so only numeric values will work nicely. Anyway, you can read a load more about tweener at the google code page above.

To add a tween, we use the code

Tweener.addTween(object, {parameter1: value, parameter2: value2, time: X, transition: "transition", onComplete: functionname});

This may look a little bit confusing at first, but it's really simple. Let's show you a real example..

Tweener.addTween(cube, { x: 1000, z: 500, time: 2, onComplete: randomize } );

The line above will add a tween. This tween will change the values "x" and "y" on the object "cube" to 1000 and 500 respectively. It will smoothly change the values over a period of 2 seconds and once the tween is complete and the cube.x  is 1000 and cube.y is 500, it will trigger the "randomize" function.

Experiment with more ways to tween your objects. You can apply tweens to anything, including cameras, lights, rotations, sizes and camera zooms. Any numerical value you can think of.

Here is my source code (Uses latest version of my base class):

Actionscript:
  1. package  {
  2.     import org.papervision3d.lights.PointLight3D;
  3.     import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
  4.     import org.papervision3d.materials.utils.MaterialsList;
  5.     import org.papervision3d.materials.WireframeMaterial;
  6.     import org.papervision3d.objects.primitives.Cube;
  7.     import org.papervision3d.objects.primitives.Plane;
  8.     
  9.     import caurina.transitions.Tweener;
  10.     
  11.     public class Main extends PaperBase{
  12.       
  13.        private var cube:Cube;
  14.        private var cube2:Cube;
  15.        private var mat:FlatShadeMaterial = new FlatShadeMaterial(new PointLight3D(), 0xFFFFFF, 0xFF0000);
  16.        private var mat2:WireframeMaterial = new WireframeMaterial(0x00FF00);
  17.        private var plane:Plane = new Plane(null, 2000, 2000, 10, 10);
  18.       
  19.        public function Main() {
  20.          init(600, 300);
  21.        }
  22.       
  23.        override protected function init3d():void{
  24.          cube = new Cube(new MaterialsList( { all: mat } ), 100, 100, 100);
  25.          cube.y = 0;
  26.          cube2 = new Cube(new MaterialsList( { all: mat2 } ), 100, 100, 100);
  27.          cube2.y = 0;
  28.          cube2.x = 1000;
  29.          cube2.z = 1000;
  30.          plane.material.lineColor = 0x777777;
  31.          plane.material.doubleSided = true;
  32.          plane.pitch(90);
  33.          plane.y = -50;
  34.          default_scene.addChild(plane);
  35.          default_scene.addChild(cube);
  36.          default_scene.addChild(cube2);
  37.          Tweener.addTween(cube, { x:1000, z:1000, time:2, onComplete: randomize } );
  38.          default_camera.x = 0;
  39.          default_camera.z = 1000;
  40.          default_camera.y = 1000;
  41.        }
  42.       
  43.        public function randomize():void {
  44.          var xp:Number = (Math.random() * 2000) - 1000;
  45.          var yp:Number = (Math.random() * 2000) - 1000;
  46.          Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
  47.          cube2.x = xp;
  48.          cube2.z = yp;
  49.        }
  50.       
  51.        override protected function processFrame():void{
  52.          default_camera.lookAt(cube);
  53.        }    
  54.     }  
  55. }



Update and Downloads Section


Hi,

I haven't written a post in a while so I felt it was time to share what I've been playing around with.

I've put up a new downloads section where you can download various pieces of code that I've been creating, including code for parsing Heightmaps (click here) and Sculpted Prims (click here).

I'll have a proper update soon, and another Tutorial, I promise :)

-Luke



11. Advanced Interactivity 2


This tutorial will show you how to handle full interactivity on an objects surface, just as if it's a normal movieclip.

For my example, I've made this. The red spot in the centre is a button. You can click it to toggle it's glow on and off. The other button will reveal how it works!

There are other ways to do this, but I think this way is the easiest and most hassle-free, and it's pretty fast.

I've super-commented the source code, so from reading the comments in the code you should be able to figure out exactly what's going on.

The basic idea is, you add the movie clip which is being used as the texture to the scene, but make it invisible. You then move it so that your mouse is over the correct part of the movieclip, when your mouse touches the texture on the 3d model. I think the "Show Movieclip" button on the cube above will explain better than I can :)

Actionscript:
  1. package  {
  2.     import flash.display.Bitmap;
  3.     import flash.display.MovieClip;
  4.     import flash.events.MouseEvent;
  5.     import flash.filters.GlowFilter;
  6.     import org.papervision3d.materials.MovieMaterial;
  7.     import org.papervision3d.materials.utils.MaterialsList;
  8.     import org.papervision3d.objects.primitives.Cube;
  9.     import org.papervision3d.events.InteractiveScene3DEvent;
  10.     
  11.     public class Main extends PaperBase {
  12.       
  13.        // This is the movieclip that we'll use as the texture.
  14.        private var movie:MovieClip = new MovieClip();
  15.       
  16.        // This movieclip will be completley transparent and will hold the
  17.        // texture movieclip, then move it to the correct loaction under the mouse.
  18.        private var movieParent:MovieClip = new MovieClip();
  19.       
  20.        // These are buttons that I'm going to add to the texture movieclip
  21.        private var button:MovieClip = new MovieClip();
  22.        private var showbutton:MovieClip = new MovieClip();
  23.       
  24.        // A "MovieMaterial" will use a movieclip as a texture.
  25.        private var mat:MovieMaterial;
  26.       
  27.        // The cube that we're going to texture..
  28.        private var cube:Cube;
  29.       
  30.        // This will import the file "E:/Papervision/images/showtex.jpg" to the project, and
  31.        // store the data in "ButtonIm". (This is the "Show Movieclip" Button)..
  32.        [Embed(source = "E:/Papervision/images/showtex.jpg")] private var ButtonIm:Class;
  33.       
  34.        public function Main() {
  35.          // Initiate, 400px wide, 400px tall..
  36.          init(400, 400);
  37.        }
  38.       
  39.        override protected function init3d():void {
  40.          // - See the prepareMovieclip function. This just builds the movieclip called "movie".
  41.          // You can use ANY movie clip for this..
  42.          prepareMovieclip();
  43.          
  44.          // Prepare out moviematerial. Make it animated and interactive.
  45.          mat = new MovieMaterial(movie, false, true);
  46.          mat.interactive = true;
  47.          
  48.          // Zoom in a bit..
  49.          default_camera.zoom = 5;
  50.          
  51.          // Prepare the cube.
  52.          cube = new Cube(new MaterialsList( { all: mat } ), 500, 500, 500, 4, 4, 4);
  53.          // Listen for when the mouse is moved over the cube.
  54.          // Trigger the mMove function when this happens.
  55.          cube.addEventListener( InteractiveScene3DEvent.OBJECT_MOVE, mMove);
  56.          
  57.          // Add the cube to the scene.
  58.          default_scene.addChild(cube);
  59.          
  60.          // The movieParent movieclip will hold the movieclip which is being used
  61.          // as the texture. It will then move depending on the mouse posirion.
  62.          movieParent.addChild(movie);
  63.          // Make it invisible.
  64.          movieParent.alpha = 0;
  65.          
  66.          // Add the movieParent movieclip to the stage.
  67.          addChild(movieParent);
  68.          
  69.        }
  70.       
  71.       
  72.        private function prepareMovieclip():void {
  73.          
  74.          // - This code will set up our movieclip which is going to be used
  75.          // as the texture for the cube.
  76.          // Draw Outline:
  77.          movie.graphics.beginFill(0xFFFFFF);
  78.          movie.graphics.drawRect(0, 0, 500, 500);
  79.          movie.graphics.endFill();
  80.          movie.graphics.beginFill(0x000000);
  81.          movie.graphics.drawRect(0, 0, 10, 500);
  82.          movie.graphics.drawRect(490, 0, 10, 500);
  83.          movie.graphics.drawRect(0, 0, 500, 10);
  84.          movie.graphics.drawRect(0, 490, 500, 10);
  85.          movie.graphics.endFill();
  86.          
  87.          // Draw Circular Button:
  88.          button.graphics.beginFill(0xBB0000);
  89.          button.graphics.drawCircle(0, 0, 50);
  90.          button.graphics.endFill();
  91.          button.x = 250;
  92.          button.y = 250;
  93.          button.buttonMode = true;
  94.          
  95.          // Load bitmap button texture:
  96.          var Bim:Bitmap = new ButtonIm();
  97.          // Draw "Show Movieclip" Texture:
  98.          showbutton.graphics.beginBitmapFill(Bim.bitmapData);
  99.          showbutton.graphics.drawRect(0,0,100,30);
  100.          showbutton.graphics.endFill();
  101.          showbutton.buttonMode = true;
  102.          showbutton.x = 380;
  103.          showbutton.y = 450;
  104.          
  105.          // Add the buttons to the movieclip
  106.          movie.addChild(showbutton);
  107.          movie.addChild(button);
  108.          
  109.          // -- Listen for the buttons to be clicked --
  110.          button.addEventListener(MouseEvent.CLICK, mClickButton);
  111.          showbutton.addEventListener(MouseEvent.CLICK, showHide);
  112.          // --
  113.        }
  114.       
  115.        private function showHide( e:MouseEvent ):void {
  116.          // The "Show Movieclip" button has been clicked
  117.          if (movieParent.alpha == 0) { // If it's invisible,
  118.               movieParent.alpha = 0.2; // Make it slightly visible.
  119.          }else {
  120.               movieParent.alpha = 0; // Or make it invisible again
  121.          }
  122.        }
  123.       
  124.        private function mClickButton(e:MouseEvent):void {
  125.          // The cental round button has been clicked.
  126.          if(button.name == "on"){
  127.               button.filters = null;
  128.               button.name = "off"
  129.          }else {
  130.               button.filters = [new GlowFilter(0x000000, 1, 15, 15, 10, 1)];
  131.               button.name = "on";
  132.          }
  133.        }
  134.       
  135.        private function mMove( e:InteractiveScene3DEvent ):void {
  136.          // This code is run when the mouse is moved on the cube.
  137.          // This will move the movieclip to the right place beneath
  138.          // the mouse.
  139.          movieParent.x = root.mouseX -e.x;
  140.          movieParent.y = root.mouseY -e.y;
  141.        }
  142.       
  143.        override protected function processFrame():void {
  144.          // Spin our cube a bit.
  145.          cube.yaw(0.25);
  146.          cube.pitch(0.25);
  147.        }
  148.       
  149.       
  150.     }
  151. }

The image that's embedded in the code can be found here: showtex.jpg

Have fun! More Examples coming soon.



10. Advanced Interactivity


Today we're going to learn how to handle more advanced interactivity. We'll be making something like this:

Click on a face of the cube to zoom into it. Click it again to make the cube spin again.

So, in this tutorial I'm going to show you exactly how to work out which material has been clicked on the cube and act accordingly.

If you haven't read it already, I strongly suggest that you read the Basic Interactivity tutorial first or you'll probably miss something.

Ok, so on to the code.

Firstly we'll want six materials to apply to each face of the cube. Here are mine, although you can create whatever materials you like:

Actionscript:
  1.       private var frontMaterial:BitmapFileMaterial = new BitmapFileMaterial("http://papervision2.com/wp-content/pvTex/front.jpg");
  2.        private var backMaterial:BitmapFileMaterial = new BitmapFileMaterial("http://papervision2.com/wp-content/pvTex/back.jpg");
  3.        private var leftMaterial:BitmapFileMaterial = new BitmapFileMaterial("http://papervision2.com/wp-content/pvTex/left.jpg");
  4.        private var rightMaterial:BitmapFileMaterial = new BitmapFileMaterial("http://papervision2.com/wp-content/pvTex/right.jpg");
  5.        private var topMaterial:BitmapFileMaterial = new BitmapFileMaterial("http://papervision2.com/wp-content/pvTex/top.jpg");
  6.        private var bottomMaterial:BitmapFileMaterial = new BitmapFileMaterial("http://papervision2.com/wp-content/pvTex/bottom.jpg");

So, in this code I'm just loading six images from my server to use as each face.

Also, add the following declaration beneath the texture declarations to hold our Cube object:

Actionscript:
  1. private var cube:Cube;

Now we need to set up the 3d initiation code. In this code we will firstly set all of our materials as interactive, and then give each of our materials a name. This is VERY important to be able to easily find out which material has been clicked. If we didn't give each material a name then we wouldn't be able to easily work out which face has been clicked.

So, add the following code to your init3d() function:

Actionscript:
  1.       override protected function init3d():void {
  2.          // We need to be able to identify each side. We'll do this
  3.          // by asssigning names to each material. During this process
  4.          // we'll also make the materials interactive.
  5.          frontMaterial.interactive = true;
  6.          frontMaterial.name = "front";
  7.          backMaterial.interactive = true;
  8.          backMaterial.name = "back";
  9.          leftMaterial.interactive = true;
  10.          leftMaterial.name = "left";
  11.          rightMaterial.interactive = true;
  12.          rightMaterial.name = "right";
  13.          topMaterial.interactive = true;
  14.          topMaterial.name = "top";
  15.          bottomMaterial.interactive = true;
  16.          bottomMaterial.name = "bottom";
  17.          // ---------------------------------------------
  18.          
  19.          cube = new Cube(new MaterialsList( {
  20.               front: frontMaterial,
  21.               back: backMaterial,
  22.               left: leftMaterial,
  23.               right: rightMaterial,
  24.               top: topMaterial,
  25.               bottom: bottomMaterial
  26.               } ), 500, 500, 500, 3, 3, 3);
  27.          cube.addEventListener( InteractiveScene3DEvent.OBJECT_PRESS, onPress);
  28.          default_scene.addChild(cube);
  29.        }

So, In this code, we've set each material as interactive, given each one a sensible name, and initialised our cube with the materials assigned to the correct faces.

We then add an event listener to trigger the "onPress" event when the cube is clicked, then finally we add the cube to the scene.

We've now got a cube with six materials on it listening for a click event.

Now, the code which will let us work out which face has been clicked:

Actionscript:
  1. private function onPress( e:InteractiveScene3DEvent ):void {
  2.     switch(e.face3d.material.name) {
  3.        case "front":
  4.          // This code will be run when the front face is clicked
  5.        break;
  6.        case "back":
  7.          // This code will be run when the back face is clicked
  8.        break;
  9.        case "left":
  10.          // This code will be run when the left face is clicked
  11.        break;
  12.        case "right":
  13.          // This code will be run when the right face is clicked
  14.        break;
  15.        case "top":
  16.          // This code will be run when the top face is clicked
  17.        break;
  18.        case "bottom":
  19.          // This code will be run when the bottom face is clicked
  20.        break;
  21.     }
  22. }

Pretty self explanitary. The "e" variable in this code holds lots of data about the click event, including which material has been clicked, so, because we know which material is on each face, we can tell by the materials name which face has been clicked!

With a little bit of code, you can make a nice spinning cube gallery like the example above:

Here is my final code, have fun!

Actionscript:
  1. package  {
  2.