#1 (permalink)  
Old 03-03-08, 07:28
Administrator
Living At The FlepStudio!
 
Join Date: Jul 2007
Location: Cesenatico
Posts: 4,917
Rep Power: 6
Flep is on a distinguished road
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 !

__________________
_________________________________________
VIDEO CORSI ACTIONSCRIPT 3.0 creati da FlepStudio
I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread please !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 05:39.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
sponsor links
Flashmint flash templates FlippingBook-PDF publisher Flash Media Server Hosting
sponsor links
Reply

Bookmarks

Tags
direction, movement, rotatory, trigonometry

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trigonometry with Actionscript 3.0 example 5 Flep Tutorials 1 17-07-08 02:14
Trigonometry with Actionscript 3.0 [example1] Flep Tutorials 0 29-09-07 09:12
Trigonometry with Actionscript 3.0 [example2] Flep Tutorials 0 27-09-07 20:51
Trigonometry with Actionscript 3.0 [example3] Flep Tutorials 0 27-09-07 09:11
Trigonometry with Actionscript 3.0 - example 4 Flep Tutorials 0 25-09-07 16:21



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
vBulletin Skin developed by: vBStyles.com
FlepStudio 2007-2009