3 Replies Latest reply: Oct 17, 2013 9:47 AM by sinious RSS

    3D rotation script stuck at 0,0

    Orithedor

      Hello,

       

      The context of this project was to create a square that followed the movement of the mouse on a 3D scale, it's hard to explain but I will post the script so feel free to test and experiment. If you have played it, it is the same style that the Crysis 3 menu uses. Unfortunately whenever I run it the centre offset (or whatever it is called) is stuck to the top left hand corner of the canvas.

       

      I'm sorry if this is blatantly obvious but I'm not experienced enough to see the problem.

       

      Below is the script, the objects name is "pnt":

       

      import flash.events.Event;

       

       

      addEventListener (Event.ENTER_FRAME,ongo);

       

       

      var mpx:Number=0;

      var mpy:Number=0;

       

       

      function ongo(e:Event):void

      {

                pnt.rotationX = -(pnt.rotationX-mouseY)/10;

                pnt.rotationY = (pnt.rotationY-mouseX)/10;

      }

       

       

      // Below block ensure symbol rotates at any canvas size

      var pp:PerspectiveProjection = this.transform.perspectiveProjection;

       

       

      onResize(null);

      stage.align = StageAlign.TOP_LEFT;

      stage.scaleMode = StageScaleMode.NO_SCALE;

      stage.addEventListener(Event.RESIZE, onResize);

      function onResize(e:Event):void

      {

                pnt.x = (stage.stageWidth) / 2;

                pnt.y = (stage.stageHeight) / 2;

                pp.projectionCenter = new Point(pnt.x, pnt.y);

      }

      // End of block

       

      Thank you for reading!

       

      (just realised that the two variables [mpx and mpy] serve no purpose, I should admit I have been using these codes from video tutorials so an explaination in less jargon would be beneficial!)

       

      Message was edited by: Orithedor

        • 1. Re: 3D rotation script stuck at 0,0
          sinious Community Member

          I'm not really sure whatever that Crysis menu does but if you're just trying to get the object to "look" toward the mouse cursor, slightly modding your code (paste into a new AS3 document), here's a full code example (I'm creating "pnt" in code as well, comment it out to use your object):

           

          import flash.events.Event;

          import flash.display.Sprite;

           

          stage.align = StageAlign.TOP_LEFT;

          stage.scaleMode = StageScaleMode.NO_SCALE;

          stage.addEventListener(Event.RESIZE, onResize);

          // only listen to the mouse moving on the stage

          stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);

           

          // save centered projection

          var pp:PerspectiveProjection = this.transform.perspectiveProjection;

           

          // generate pnt center registration

          var pnt:Sprite = new Sprite();

          pnt.graphics.lineStyle(5,0x0,1,true);

          pnt.graphics.beginFill(0xFF0000,1);

          pnt.graphics.drawRect(-100,-50,200,100);

          pnt.graphics.endFill();

          addChild(pnt);

           

          // center pnt

          onResize(null);

           

          function onMove(e:Event):void

          {

                    pnt.rotationY = ((stage.stageWidth / 2) - mouseX)/5;

                    pnt.rotationX = (mouseY - (stage.stageHeight / 2))/5;

          }

           

          function onResize(e:Event):void

          {

                    pnt.x = (stage.stageWidth) / 2;

                    pnt.y = (stage.stageHeight) / 2;

                    pp.projectionCenter = new Point(pnt.x, pnt.y);

          }

           

          Maybe that can get you started.

          • 2. Re: 3D rotation script stuck at 0,0
            Orithedor Community Member

            Thank you very much! Works brilliantly both with the script created object and one made seperately (with the name pnt of course )

             

            I will compare both scripts and learn from this. I plan to make it more impressive than squares looking around (perfect way to describe the animation btw) so all of this is really handy.

             

            Thank you again!

            • 3. Re: 3D rotation script stuck at 0,0
              sinious Community Member

              If you're all set please mark a correct answer so we can filter out unanswered questions. You're welcome and good luck!