+ Reply to Thread
Results 1 to 1 of 1

Trigonometry example 6 - rotatory movement and direction

This is a discussion on Trigonometry example 6 - rotatory movement and direction within the Tutorials forums, part of the Flash English category; Hi! We saw diverse techniques on how to apply the trigonometry to Actionscript 3.0 . With this tutorial, we will ...

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,762
    Rep Power
    11

    Trigonometry example 6 - rotatory movement and direction

    Hi!


    We saw diverse techniques on how to apply the trigonometry to Actionscript 3.0 .


    With this tutorial, we will see how to manage the rotatory movement of a MovieClip and the direction of movement.

    I will clearly make a purely didactic example and as always it will depend on ourselves to find a practical application.

    The more fantasy and the more graphics skills, the more you could use those simple trigonometric formulas with wonderful results.


    I create a FLA and I save it as "trigonometria6.fla".

    Into which I create a MovieClip of round shape, drag it on stage and I give it an instance name "clip_mc".

    On an another level, that I will call "code", I apply the following Actionscript:


    Code:
    stage.frameRate=24;
    
    clip_mc.addEventListener(Event.ENTER_FRAME,rotate);
    
    function rotate(evt:Event):void
    {
    	var angle:Number=(mouseX-stage.stageWidth/2)*.001;
    	var sine:Number=Math.sin(angle);
    	var cosine:Number=Math.cos(angle);
    	var xx:Number=evt.target.x-stage.stageWidth/2;
    	var yy:Number=evt.target.y-stage.stageHeight/2;
    	var x1:Number=cosine*xx-sine*yy;
    	var y1:Number=sine*xx+cosine*yy;
    	evt.target.x=stage.stageWidth/2+x1;
    	evt.target.y=stage.stageHeight/2+y1;
    }

    with the following result (move the mouse above the SWF):









    Let us analyse the code


    I impost the frame rate

    stage.frameRate=24;

    I add ENTER_FRAME to clip_mc

    clip_mc.addEventListener(Event.ENTER_FRAME,rotate) ;


    function rotate(evt:Event):void

    {

    I create a numerical variable named "angle" that will take the value of an angle given by the difference in between the X position of the mouse and half the stage width, multiplied by 0.001 otherwise it would be too fast. If the value results negative, the change of direction of the rotatory movement of "clip_mc" happens.

    var angle:Number=(mouseX-stage.stageWidth/2)*.001;

    I create two numerical variables to which I assign the respective value of sinus and co sinus of the above angle

    var sine:Number=Math.sin(angle);

    var cosine:Number=Math.cos(angle);

    I create a numerical variable to which I assign the value of the difference in between the X position of "clip_mc" and half the stage width

    var xx:Number=evt.target.x-stage.stageWidth/2;

    I create a numerical variable to which I assign the difference in between the Y position of "clip_mc" and half the stage height

    var yy:Number=evt.target.y-stage.stageHeight/2;

    Now, I use two formulas about the opposite angles found in trigonometry: the theorem of the sinus that says:

    The side of a triangle are proportional to the sinus of the opposite angles.

    I create a numerical variable to which I assign the value of the co sinus multiplied by xx (the difference in between the X position of "clip_mc" and half the stage height)

    var x1:Number=cosine*xx-sine*yy;

    Now, I do the opposite

    var y1:Number=sine*xx+cosine*yy;

    Next I feed to the X of "clip_mc" the value of half the stage width plus the value of the result for variable x1

    evt.target.x=stage.stageWidth/2+x1;

    And to the Y of "clip_mc", I pass the value of half the stage height plus the value of the result for the variable y1

    evt.target.y=stage.stageHeight/2+y1;

    }


    See you soon & stay tuned !


+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 17-07-08, 02:14
  2. Replies: 0
    Last Post: 29-09-07, 09:12
  3. Replies: 0
    Last Post: 27-09-07, 20:51
  4. Replies: 0
    Last Post: 27-09-07, 09:11
  5. Replies: 0
    Last Post: 25-09-07, 16:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts