Home » Code Samples, Flash, Tutorials

Papervision3d – Simple Mouse Controlled Interactive Sphere

21 August 2008 | 22,756 views | 6 Comments


Here’s a class that renders a simple wireframe sphere in Papervision3d, and allows the mouse to control the pitch and yaw. This example can be applied to any simple interactive environment.

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import org.papervision3d.cameras.*;
	import org.papervision3d.objects.*;
	import org.papervision3d.scenes.*;
	import org.papervision3d.materials.*;

	public class PaperVisionTest extends Sprite {
		/** setup the container */
		private var container: Sprite = new Sprite();

		/** setup the scene */
		private var scene: MovieScene3D = new MovieScene3D(container);

		/** setup the camera */
		private var camera: Camera3D = new Camera3D();

		/** setup the material */
		private var material:WireframeMaterial = new WireframeMaterial(0xFF0000);

		/** setup the sphere */
		private var sphere:Sphere = new Sphere(material, 300, 30, 30);

		/**
		* constructor
		*/
		public function PaperVisionTest() {
			//add the container
			container.x = stage.stageWidth/2;
			container.y = stage.stageHeight/2;
			addChild(container);

			//camera parameters
			camera.z = -500;
			camera.zoom = 5;

			//give the sphere to the scene
			scene.addChild(sphere);
			
			stage.addEventListener(Event.ENTER_FRAME, update);
		}

		/**
		* Triggered by the enter frame event
		* @param pEvent Event object passed by the dispatcher
		*/
		private function update(pEvent:Event):void {

			//initialize the camera
			sphere.lookAt(camera);

			//mouse movement control
			sphere.pitch(this.mouseY);
			sphere.yaw(this.mouseX);

			//render the scene
			scene.renderCamera(camera);
		}
	}
}

The final result:

Click here to download the source.

  • Me

    The errors occur with the latest zip version Papervision3D_2.1.920.zip which doesn't include MovieScene3D.

  • Me

    The errors occur with the latest zip version Papervision3D_2.1.920.zip which doesn't include MovieScene3D.

  • @Ken

    The latest available build from their SVN should do just fine.

    http://code.google.com/p/papervision3d/

  • @Ken

    The latest available build from their SVN should do just fine.

    http://code.google.com/p/papervision3d/

  • Ken

    What version of PV3D do you use?
    I have met a lot of difficulties when trying to execute this program.
    Thanks!

  • Ken

    What version of PV3D do you use?
    I have met a lot of difficulties when trying to execute this program.
    Thanks!