Forum Topic: Explain Code Please (7 Lines)

(161 views • 11 replies)

This topic is 1 page long.

<< < > >>
Shouting

Kwing

Reply To Post Reply & Quote

Posted at: 4/6/09 08:50 PM

Kwing DARK LEVEL 24

Sign-Up: 07/24/07

Posts: 1,923

myRadians=Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x); 
myDegrees=Math.round((myRadians*180/Math.PI)); 
_root.yChange=Math.round(_root._ymouse-this._y); 
_root.xChange=Math.round(_root._xmouse-this._x); 
_root.yMove=Math.round(_root.yChange/20); 
_root.xMove=Math.round(_root.xChange/20); 
this._rotation=myDegrees+90;

Questions:
What does atan2 mean in Flash? Give me an example of the first line and I should be able to (eventually) piece together how the rest comes in.


None

Shinki

Reply To Post Reply & Quote

Posted at: 4/6/09 08:56 PM

Shinki DARK LEVEL 10

Sign-Up: 02/14/05

Posts: 1,571

atan2 is an implemenation of arctan which takes 2 inputs, those being the difference in x and difference in y. It returns an angle in radians.

Look it up in Flash help for more info.

If a picture is worth a thousand words, a game is worth a play.

BBS Signature

None

Montycarlo

Reply To Post Reply & Quote

Posted at: 4/6/09 09:00 PM

Montycarlo LIGHT LEVEL 18

Sign-Up: 03/14/05

Posts: 698

myRadians=Math.atan2(_root._ymouse-this.
_y, _root._xmouse-this._x);
As you requested.

myDegrees=Math.round((myRadians*180/Math .PI));

Converting from radians to degrees. Flash uses degrees for rotation.

_root.yChange=Math.round(_root._ymouse-t his._y);

The difference between the mouse Y and the object's Y.

_root.xChange=Math.round(_root._xmouse-t his._x);

The difference between the mouse X and the object's X.

_root.yMove=Math.round(_root.yChange/20)

;

_root.xMove=Math.round(_root.xChange/20)

;
Divides both variables by 20 and rounds.

this._rotation=myDegrees+90;

Sets the rotation property of the object. 90 is added because the rotation property ranges from -180~180.

At 4/6/09 08:10 AM, Shinki wrote: In Flash the rotation is expressed from -180 to 180 because then the middle is zero, it's handy. However if you set _rotation to 360, 720 or something else outside the -180 to 180 range it will calculate where to rotate to. Technically _rotation covers all values, it just returns values with in a certain range.

If you want to convert a value between -180 and 180 to a value between 0 and 360, just add 180 to it.

None

Paranoia

Reply To Post Reply & Quote

Posted at: 4/6/09 09:14 PM

Paranoia DARK LEVEL 33

Sign-Up: 04/22/05

Posts: 9,692

At 4/6/09 08:56 PM, Shinki wrote: atan2 is an implemenation of arctan which takes 2 inputs, those being the difference in x and difference in y. It returns an angle in radians.

Look it up in Flash help for more info.

Of course, it's more of whatever the vector equivalent of arg is than a tan function... Specifically, when used as such:

atan2(y, x);

It returns the (anticlockwise) angle in radians between the x axis and the line joining the origin to the point (x, y).

Since Flash works with an inverted y axis, if you use it as:

atan2(x, y);

It will effectively return the angle clockwise from the y (vertical) axis to wherever, which is the form people seem to find most convenient (at least, judging from the fact that that's the form tutorials seem to give).

BBS Signature

None

Shinki

Reply To Post Reply & Quote

Posted at: 4/6/09 09:23 PM

Shinki DARK LEVEL 10

Sign-Up: 02/14/05

Posts: 1,571

At 4/6/09 09:14 PM, Paranoia wrote: Of course, it's more of whatever the vector equivalent of arg is than a tan function...

It just does arctan(y/x) but with division by zero protection. Though now you mention it, it is pretty much arg... :p

If a picture is worth a thousand words, a game is worth a play.

BBS Signature

Resigned

Kwing

Reply To Post Reply & Quote

Posted at: 4/6/09 09:49 PM

Kwing DARK LEVEL 24

Sign-Up: 07/24/07

Posts: 1,923

So if:

myRadians=Math.atan2(_y,_x);

and let's say x is 5 and y is 10, then what is myRadians? I'm trying to figure out what exactly it does.


None

Nano256

Reply To Post Reply & Quote

Posted at: 4/6/09 10:51 PM

Nano256 DARK LEVEL 13

Sign-Up: 02/12/05

Posts: 1,473

At 4/6/09 09:49 PM, Kwing wrote: So if:

myRadians=Math.atan2(_y,_x);

and let's say x is 5 and y is 10, then what is myRadians? I'm trying to figure out what exactly it does.

Here's a really quick sketch of what that will give you in visual terms.

Explain Code Please (7 Lines)

Move on to ActionScript 3.0 already!
The third post below this one is a lie.

BBS Signature

None

kiwi-kiwi

Reply To Post Reply & Quote

Posted at: 4/7/09 12:35 AM

kiwi-kiwi LIGHT LEVEL 08

Sign-Up: 03/06/09

Posts: 631

Atan2 is a consecrated mathematical function. It takes a y and a x value as parameters and returns the angle in radians that would give the tangent y/x.


Questioning

Kwing

Reply To Post Reply & Quote

Posted at: 4/7/09 12:59 AM

Kwing DARK LEVEL 24

Sign-Up: 07/24/07

Posts: 1,923

At 4/6/09 10:51 PM, Nano256 wrote:
At 4/6/09 09:49 PM, Kwing wrote: So if:

myRadians=Math.atan2(_y,_x);

and let's say x is 5 and y is 10, then what is myRadians? I'm trying to figure out what exactly it does.
Here's a really quick sketch of what that will give you in visual terms.

Really, really cool! But... How does 60 degrees convert to an obscure decimal?


None

henke37

Reply To Post Reply & Quote

Posted at: 4/7/09 01:26 AM

henke37 NEUTRAL LEVEL 23

Sign-Up: 09/10/04

Posts: 3,550

Arctan2 does one thing that arctan does not. It is able to put the angle in the right quarter of the unit circle. arctan can be ambiguous, due to multiple negative values canceling each other out, but arctan2 got some tricks to not get fooled.

Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.


None

Nano256

Reply To Post Reply & Quote

Posted at: 4/7/09 01:43 AM

Nano256 DARK LEVEL 13

Sign-Up: 02/12/05

Posts: 1,473

At 4/7/09 12:59 AM, Kwing wrote:
At 4/6/09 10:51 PM, Nano256 wrote:
At 4/6/09 09:49 PM, Kwing wrote: So if:

myRadians=Math.atan2(_y,_x);

and let's say x is 5 and y is 10, then what is myRadians? I'm trying to figure out what exactly it does.
Here's a really quick sketch of what that will give you in visual terms.
Really, really cool! But... How does 60 degrees convert to an obscure decimal?

Radians and degrees are two different ways of measuring angles.

180 degrees is equal to Pi radians (3.141592653...).

Hence, if you were to convert 60 degrees to radians, you would divide by 180 and multiply by Pi.

60 / 180 * Pi = 1.047197551...

Which is not the same radian I showed in my graphic. I made a mathematical mistake, in the graphic, the radians are correct, however that translates to 63.4349.

Simply, stick this in flash:

trace(Math.atan2(10, 5))

And that outputs 1.1071487177940904

If you divide that by Pi, and multiply by 180 degrees...

trace(Math.atan2(10,5) / Math.PI * 180)

That outputs 63.43494882292201, which is what that translates to degrees.

Move on to ActionScript 3.0 already!
The third post below this one is a lie.

BBS Signature

Happy

Denvish

Reply To Post Reply & Quote

Posted at: 4/7/09 03:07 AM

Denvish DARK LEVEL 45

Sign-Up: 04/25/03

Posts: 16,238

Senocular's explanation is a fairly good one

- - Flash - Music - Images - -

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 01:04 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!