sábado, 9 de enero de 2010

Getting Started with PV3D(Papervision 3D) Part 2

Part 1: Getting Started with PV3D (Papervision 3D)
So on the previous tutorial i explained the basics of Papervision 3D, but we just set a static cube on the 3d scene and PV3D do a lot more things. On this tutorial i will explain how you can add Movement to the objects you added.


Firs let's see what we have:

package {
   import org.papervision3d.materials.ColorMaterial;
   import org.papervision3d.materials.utils.MaterialsList;
   import org.papervision3d.objects.primitives.Cube;
   import org.papervision3d.view.BasicView;
   public class Hello3DWorld extends BasicView
   {
       private var cube:Cube;
       private var matList:MaterialsList;
       private var color:ColorMaterial;
       public function Hello3DWorld()
       {
           color = new ColorMaterial(0x000000);
           matList = new MaterialsList();
           matList.addMaterial(color, "all");
           cube = new Cube(matList);
           scene.addChild(cube);
           startRendering();
       }
   }
} 
Ok now the new stuff, Inside the BasicView class, there's a functioin called "onRenderTick" wich is called with the event Enter_Frame, so basically it is called arround 30 times per second (obviously this depends on your frame rate). This function calls the render method, so it takes a photo of how the scene is looking, so if we want to change something, for example the X position, we have to put there the change we want to do.

override protected function onRenderTick(event:Event=null):void
{
   cube.x++
   super.onRenderTick(event);
}

Let me explain each part of the code:

  • override: This means that we will be using a function contained on the class we are extending (BasicView), or in other words, that we will use a function of BasicView, but here comes the big difference, bye using override we can add new stuff to the function, in this case changin the x value of "cube", but it can be anything. Another important fact of using the override atribute, is that you must use the same signature of the function your are overriding.
  • protected:  This is a scope definition, which means who can see this function. Protected applies for function or variables who would be seen just by this class or classes extending this class (subclasses).
  • function: well I think this is obvis =P
  • onRenderTick: Name of the function
  • (event:Event=null): The parameters of the function, because it is a Event listener, it need the event as a parameter.
  • void: Because it doesnt return anything.
Ok that was just the fisrt part, i hope everything is clear until now, next whats inside the function.

  • cube.x++: This is the change we are making on the cube, we are just moving it along the x value.
  • super.onRenderTick(event): First, by using "super" we are accesing the original function, the function that BasicView has. This is very important, because with out this we wouldn't call te renderer and the other stuff that comes with this function, so never forget it!, also it has an Event parameter, which is the same of our function, so just pass it.
Ands thats all basically, the other stuff is up to you and your imagination, you decide what to do on every enter frame as any other project =).

This example just rotates the cube by using the yaw and roll functions.

            cube=new Cube(matList);
            scene.addChild(cube);
            startRendering();
        }

        override protected function onRenderTick(event:Event = null):void {
            cube.yaw(2);
            cube.roll(2);
            super.onRenderTick(event);
        }
    }
} 
 




Hope you like it =), see you in another Actionscript Tutorial.

Saludos, Lalo Glz

Signature: It referrs to the name, parameter, and return value of a function.
E.g.: myFunction (name:String):Number

    No hay comentarios.:

    Publicar un comentario