I have read your post and decided to try it . Actually it was pretty
straightforward .... I even feel embarrassed of myself before Ralph Hauwert
because it was almost copy-paste stuff of his code  :(
Anyways here it is : (just change to cube instead of sphere):

//////////////////////////////////////////////////Arcball--->ported to
Away3D/////////////////////////////////////////////////////
package
{
    import away3dlite.cameras.Camera3D;
    import away3dlite.containers.Scene3D;
    import away3dlite.containers.View3D;
    import away3dlite.events.MouseEvent3D;
    import away3dlite.materials.ColorMaterial;
    import away3dlite.primitives.Sphere;

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Orientation3D;
    import flash.geom.Vector3D;

    [SWF(backgroundColor="#000000", frameRate="30", quality="MEDIUM",
width="800", height="600")]
    public class AwayLiteDemos extends Sprite
    {
        private var _view:View3D;
        private var _cam:Camera3D;
        private var _ball:Sphere;
        private var _scene:Scene3D;
        private var _startVector:Vector3D;
        private var _targetVector:Vector3D;
        private var startQuat:Vector3D;
        private var dragVector:Vector3D;
        private var dragQuat:Vector3D;
        private var newQuat:Vector3D;
        private var canDrag:Boolean=false;

        private var transformComponents:Vector.<Vector3D>;
        public function AwayLiteDemos()
        {
            _startVector=new Vector3D();
            //_targetVector=new Vector3D();
            dragVector=new Vector3D();
            startQuat=new Vector3D();
            dragQuat=new Vector3D();
            newQuat=new Vector3D();

            createAway();
            createMaterials();
            createGeom();

            if(_ball.transform.matrix3D == null){
                _ball.rotationX = 0;
            }
        }
        private function createAway():void{
            _view=new View3D();
            _scene=new Scene3D();
            //_view.width=600;
            //_view.height=400;
            addChild(_view);
            _cam=new Camera3D();

            _view.camera=_cam;
            _view.scene=_scene;
            _cam.z=-300;
            addEventListener(Event.ENTER_FRAME,renderScene);
            stage.addEventListener(Event.RESIZE, onResize);
            onResize();
        }
        private function createMaterials():void{

        }
        private function createGeom():void{
            _ball=new Sphere(new ColorMaterial(0xCC0000),500,8,6,false);
            _scene.addChild(_ball);
            _ball.z=0;
            //var pl:Plane=new Plane(new ColorMaterial(0x983487));
            //_scene.addChild(pl);
            //pl.rotationX=90;
            //pl.bothsides=true;
            //_cam.lookAt(_ball.position,Vector3D.Y_AXIS);
            stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouse3dDown);


        }

        private function onMouseUp(e:MouseEvent):void{
            e.stopPropagation();
            stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
            canDrag=false;
        }
        private function onMouse3dDown(e:MouseEvent):void{
            //trace(e.uvt.x+"/"+e.uvt.y+"/"+e.uvt.z);
            stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);

_startVector=coordinate2DToSphere(_scene.mouseX,_scene.mouseY,_startVector);
           startDragThis(_startVector);
           canDrag=true;

        }
        public function coordinate2DToSphere(x:Number, y:Number,
targetVector:Vector3D):Vector3D
        {
            targetVector.x = (_ball.x-x)/_ball.radius;
            targetVector.y = (_ball.y-y)/_ball.radius;
            targetVector.z = 0;
            if(targetVector.lengthSquared > 1){
                //targetVector.normalize();
            }else{
                targetVector.z = Math.sqrt(1-targetVector.lengthSquared);
            }
            return targetVector;

        }
        private function startDragThis(vector3D:Vector3D):void
        {

            transformComponents =
_ball.transform.matrix3D.decompose(Orientation3D.QUATERNION);
            var quat:Vector3D = transformComponents[1];
            startQuat.x = quat.x;
            startQuat.y = quat.y;
            startQuat.z = quat.z;
            startQuat.w = quat.w;
            dragQuat.x = dragQuat.y = dragQuat.z = dragQuat.w = 0;
        }
        protected function dragTo(vector:Vector3D):void
        {
            var v:Vector3D = _startVector.crossProduct(dragVector);
            dragQuat.x = v.x;
            dragQuat.y = v.y;
            dragQuat.z = v.z;
            dragQuat.w = _startVector.dotProduct(dragVector);
            multiplyQuats(dragQuat, startQuat, newQuat);
            transformComponents[1] = newQuat;
            _ball.transform.matrix3D.recompose(transformComponents,
Orientation3D.QUATERNION);
            trace(_ball.transform);
        }
        private function renderScene(e:Event):void{
            _view.render();
            if(canDrag){
                dragVector =
coordinate2DToSphere(_scene.mouseX,_scene.mouseY,dragVector);
                dragTo(dragVector);
            }
            trace(canDrag);

        }
        private function onResize(event:Event = null):void
        {
            _view.x = stage.stageWidth / 2;
            _view.y = stage.stageHeight / 2;
            //  SignatureBitmap.y = stage.stageHeight - Signature.height;
        }
        /////////////////////////////////////////
        public static function multiplyQuats(q1:Vector3D, q2:Vector3D,
out:Vector3D):void
        {
            out.x = q1.w*q2.x+q1.x*q2.w+q1.y*q2.z-q1.z*q2.y;
            out.y = q1.w*q2.y+q1.y*q2.w+q1.z*q2.x-q1.x*q2.z;
            out.z = q1.w*q2.z+q1.z*q2.w+q1.x*q2.y-q1.y*q2.x;
            out.w = q1.w*q2.w-q1.x*q2.x-q1.y*q2.y-q1.z*q2.z;
        }
    }
}

On Fri, Apr 30, 2010 at 3:15 PM, Dasde <[email protected]> wrote:

> Hi !
>
> I'm trying to rotate a cube with the mouse. Is there a way to use this
> implementation of Arcball (http://www.unitzeroone.com/blog/2009/09/08/
> source-better-flash-10-3d-interaction-arcball/<http://www.unitzeroone.com/blog/2009/09/08/%0Asource-better-flash-10-3d-interaction-arcball/>)
> with Away3D Lite ?
> I tried to (just add an event proxy (the view) to get
> MouseEvent.MOUSE_DOWN) but it didn't work as expected (rotate only one
> axis).
> Is there an other way to do that ?
>
> Any help would be most welcome
>
> Dasde
>
>


-- 
Michael Ivanov ,Programmer
Neurotech Solutions Ltd.
Flex|Air |3D|Unity|
www.neurotechresearch.com
Tel:054-4962254
[email protected]
[email protected]

Reply via email to