Pv3d Quaternion test
オリジナルのcodeはこちら
http://pv3d.org/2008/11/29/free-rotation-using-quaternions/
/**
* Copyright dizgid ( http://wonderfl.net/user/dizgid )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/7udh
*/
// オリジナルのcodeはこちら
// http://pv3d.org/2008/11/29/free-rotation-using-quaternions/
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.*;
import org.papervision3d.view.BasicView;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.materials.ColorMaterial;
import net.hires.debug.Stats;
[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:Cube;
public function QuaternionExample()
{
var material:MaterialsList = new MaterialsList();
var colorMat:ColorMaterial = new ColorMaterial(0xff0000, 1.0);
material.addMaterial(colorMat, "all");
sphere = new Cube(material, 400, 400, 400);
scene.addChild(sphere);
startRendering();
this.addChild(new Stats());
stage.addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(event:MouseEvent):void
{
//quick reset
//sphere.copyTransform(new DisplayObject3D());
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);
}
override protected function onRenderTick(event:Event=null):void
{
renderer.renderScene(scene, camera, viewport);
}
}
}