In the previous tutorial we showed you how to load a 3D model into Papervision. Now we are just going to show you how to rotate that model based off of your mouse movements.
The process is very simple. Since our render function is like the engine of our animations that is where we will add the code for the rotation. Later we'll cover animating objects via TweenLite / TweenMax.
-
override protected function onRenderTick(event:Event = null):void
-
{
-
super.onRenderTick(event);
-
cow.rotationY = stage.mouseX / sceneWidth * 360
-
}
When you first start moving objects in Papervision you may notice that rotationY and rotationX do not act as you might think they would. In flash you would think of something on a Y axis as going up and down and X as left and right. With Papervision RotationY rotates and object on its Y axis (left /right) and RotationX rotates an object on it's X axis (top / bottom).
Here is the full code:
-
package
-
{
-
import flash.events.Event;
-
import org.papervision3d.materials.BitmapFileMaterial;
-
import org.papervision3d.materials.utils.MaterialsList;
-
import org.papervision3d.objects.DisplayObject3D;
-
import org.papervision3d.objects.parsers.Collada;
-
import org.papervision3d.view.BasicView;
-
-
public class Main extends BasicView
-
{
-
protected var cow:DisplayObject3D;
-
protected var materialList:MaterialsList;
-
protected var bitmapFileMaterial:BitmapFileMaterial;
-
-
protected var sceneWidth:Number;
-
protected var sceneHeight:Number;
-
-
public function Main()
-
{
-
super();
-
init();
-
createChildren();
-
startRendering();
-
}
-
protected function init():void
-
{
-
sceneWidth = stage.stageWidth
-
}
-
public function createChildren():void
-
{
-
//Setup the materials manually (sometimes the dae handles this without issue)
-
materialList = new MaterialsList();
-
bitmapFileMaterial = new BitmapFileMaterial("daeModel/Cow.png");
-
materialList.addMaterial(bitmapFileMaterial,"all");
-
-
//Create the new Collada Object with materialList
-
cow = new Collada("daeModel/cow.dae",materialList);
-
-
///Set some properties
-
cow.moveDown(150);
-
cow.scale = 3;
-
cow.pitch( -10);
-
-
//Add to scene
-
scene.addChild(cow);
-
}
-
-
override protected function onRenderTick(event:Event = null):void
-
{
-
super.onRenderTick(event);
-
cow.rotationY = stage.mouseX / sceneWidth * 360
-
}
-
}
-
}






