In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

PV3D Quaternion rotation

@author: ram64
@website: www.ram64.com
@name: Drogeanu Florentin-Alexandru
/**
 * Copyright ram64 ( http://wonderfl.net/user/ram64 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/tmDM
 */

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import org.papervision3d.lights.PointLight3D;
    import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
    import org.papervision3d.materials.ColorMaterial;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.view.BasicView;
    import org.papervision3d.materials.utils.MaterialsList;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.core.math.Quaternion;
    import org.papervision3d.core.math.Matrix3D;
    /*
     * @author: ram64
     * @website: www.ram64.com
     * @name: Drogeanu Florentin-Alexandru
     */
    [SWF(frameRate = 30, backgroundColor = 0x333333)]
    public class FlashTest extends BasicView {
        
        protected var cube:Cube;
        protected var light:PointLight3D;
        
        public function FlashTest() {
            // write as3 code here..
            
            init();
            startRendering();
        }
        
        protected function init():void{
            
            light = new PointLight3D()
            scene.addChild(light);
            cube = createCube();
            scene.addChild(cube);
            cube.x = 200;
            
            quaternionRotate(cube, 90);
            quaternionRotate(cube, 0, 90);
            quaternionRotate(cube, -90);
        }
        
        override protected function onRenderTick(e:Event = null):void
        {
            // cube.rotationX += 1;
            // cube.rotationZ += 1;
            quaternionRotate(cube, 1,2,1);
            super.onRenderTick(e);
        }
        
        protected function quaternionRotate(obj:DisplayObject3D, rX:Number = 0, rY:Number = 0, rZ:Number = 0):void {
            // Get the object's transformation matrix as a Quaternion
            var p1:Quaternion = Quaternion.createFromMatrix(obj.transform);
            // Create a new Quaternion with the rotation parameters
            // Note: Change X rotation with Y, Y with Z and Z with X
            var p2:Quaternion = Quaternion.createFromEuler(rY, rZ, rX, true);
            // Get the position of the object before transformation
            // so we can translate it back to the original position after rotation.
            var ox:Number = obj.x;
            var oy:Number = obj.y;
            var oz:Number = obj.z;
            
            p2.normalize();
            // Multiply the two quaternions in order to get the rotation
            p2.mult(p1);
            p2.normalize();
            // Copy the transform results in the object
            obj.copyTransform(Matrix3D.quaternion2matrix(p2.x, p2.y, p2.z, p2.w));
            // Translating the object back to it's original position
            obj.x = ox;
            obj.y = oy;
            obj.z = oz
        }

        
        protected function createCube():Cube 
        {
            // Create the materials for the cube
            var ml:MaterialsList = new MaterialsList();

            var mat:FlatShadeMaterial = new FlatShadeMaterial(light, 0xFFD700, 0xFF0000);
            ml.addMaterial(mat, 'front');
            mat = new FlatShadeMaterial(light, 0xFFD700, 0xFF3333);
            ml.addMaterial(mat, 'back');
            mat = new FlatShadeMaterial(light, 0xFFD700, 0x00FF00);
            ml.addMaterial(mat, 'top');
            mat = new FlatShadeMaterial(light, 0xFFD700, 0x33FF33);
            ml.addMaterial(mat, 'bottom');
            mat = new FlatShadeMaterial(light, 0xFFD700, 0x0000FF);
            ml.addMaterial(mat, 'right');
            mat = new FlatShadeMaterial(light, 0xFFD700, 0x3333FF);
            ml.addMaterial(mat, 'left');

            var C:Cube = new Cube(ml, 200,200,200);
            
            // Create three cylinders to represent the directions of the cube's 
            // local rotation axes
            
            // Y axis
            var cm:ColorMaterial = new ColorMaterial(0x00FF00);           
            var cy:Cylinder = new Cylinder(cm, 10, 100);
            cy.y += 150;
            C.addChild(cy);
            // X axis
            cm = new ColorMaterial(0xff0000);
            cy = new Cylinder(cm, 10, 100);
            cy.x += 150;
            cy.rotationZ += 90;
            C.addChild(cy);
            // Z axis
            cm = new ColorMaterial(0x0000FF);
            cy = new Cylinder(cm, 10, 100);
            cy.z += 150
            cy.rotationX += 90;
            C.addChild(cy);
            
            return C;
        }



    }
}