Free rotation using Quaternions

Saturday, November 29th, 2008 | examples, requests


source

package
{
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
 
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.core.math.Plane3D;
	import org.papervision3d.core.math.Quaternion;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class QuaternionExample extends BasicView
	{
		private const XYPLANE:Number3D = new Number3D(0, 0, 1);
		private const RADIUS:Number = 400;
		private const SPEED:Number = .05;
 
		private var plane3D:Plane3D  = new Plane3D(XYPLANE, Number3D.ZERO);;
		private var sphere:Sphere;
 
		[Embed(source="assets/lake_640.jpg")]
		private var materialAsset:Class;
 
 
		public function QuaternionExample() 
		{
			var light:PointLight3D = new PointLight3D();
			var bitmap:Bitmap = new materialAsset() as Bitmap;
			var material:BitmapMaterial = new BitmapMaterial(bitmap.bitmapData);
			sphere = new Sphere(material, RADIUS, 30, 30);
 
			scene.addChild(sphere);
 
			startRendering();
 
			stage.addEventListener(MouseEvent.CLICK, clickHandler);
		}
 
		private function clickHandler(event:MouseEvent):void
		{
			//quick reset
			sphere.copyTransform(new DisplayObject3D());
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
			var ray:Number3D = camera.unproject(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);
			ray = Number3D.add(ray, camera.position);
 
			var cameraVertex3D:Vertex3D = new Vertex3D(camera.x, camera.y, camera.z);
			var rayVertex3D:Vertex3D = new Vertex3D(ray.x, ray.y, ray.z);
 
			var intersectPoint:Vertex3D = plane3D.getIntersectionLine(cameraVertex3D, rayVertex3D);
 
			var velocityX:Number = intersectPoint.x * SPEED;
			var velocityY:Number = intersectPoint.y * SPEED;
			var velocityZ:Number = intersectPoint.y * SPEED;
 
			var difference:Number3D = new Number3D(-velocityX, -velocityY, -velocityZ);
			var distance:Number = Math.sqrt(difference.x * difference.x + difference.y * difference.y);
 
			var rotationAxis:Number3D = Number3D.cross(difference, XYPLANE);
			rotationAxis.normalize();
 
			var rotation:Quaternion = Quaternion.createFromAxisAngle(rotationAxis.x, rotationAxis.y, rotationAxis.z, distance/RADIUS);
			rotation.normalize();
 
			sphere.transform.calculateMultiply3x3(rotation.matrix, sphere.transform);
 
			renderer.renderScene(scene, camera, viewport);
		}
	}
 
}

I hope to be able to do a write-up explaining Quaternions next week.

Tags: ,

  • Spiros
    The rotation axis computation could use some comments....
  • T
    Introductory Papervision Tutorials for Actionscript 3 - using Great White 2.0 beta, can be found at this blog, I recomend you start with the simple step by step tutorials here:

    http://thierryzoghbi.blogspot.com/2009/03/loading-3d-objects-created-in-max-using.html
  • Shaun
    I have the same question as the last two people unfortunately! This method works great for more natural mouse-controlled rotation, but I want to be able to rotate to a specific rotation from the current (angular) position. I know I could probably do this if I was able to calculate the current rotation, and obtain a delta from that and my desired rotation, but rotationX, rotationY, localRotationX, localRotationY etc. all seem to be zero. Is the only way to obtain single rotation values in this case to extract them from the Matrix3D at DisplayObject3D.transform? I was hoping to find a slightly more high-level solution before delving into that kind of thing!
  • Is there anyway to translate the quanternion into rotationX,Y, and Z?
  • JB
    Is there a way to get the rotation X,Y or Z from the sphere?
  • John Lindquist
    The faq section explains how to use each example.
  • cslam
    Your site is great with alot of examples in Papervision 3D. However, I find it hard to apply the source to my papervision 3D version....so in the end, I don't get to enjoy the examples....Is it possibel to attach the appropriate link to the effective version of papervision 3D ?
  • John Lindquist
    Would you care to elaborate?
  • Edgard
    no work on new papervision release.
blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

November 2008
M T W T F S S
    Dec »
 12
3456789
10111213141516
17181920212223
24252627282930

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...