A Flash Developer Resource Site

# Thread: Simple problem, how to get angle between two points?

1. ## Simple problem, how to get angle between two points?

I tried to do:
Code:
`Math.abs(Math.atan2((y1-y2),(x1-x2)))*(180/Math.PI)`
but i am getting 1-180 and 180 to 1 as i go around the pivoted point, instead of 1-360, what am i doing wrong??

2. ok, i solved it myself!

3. ## Please Help

Hi
I am facing the same problem.
Can you please help me , that how can i get the angle from 0-360 instead of 0-180.

my email is uzair@meragsm.com.

I am waiting for your reply.

Thanks

4. Hi,
You can easily fix this by doing the small changes. This one really help

var angle=Math.atan2((y1-y2),(x1-x2))
if (angle<0) {
angle +=3
}
angle=angle*(180/Math.PI)

5. Originally Posted by bluemagica
I tried to do:
Code:
`Math.abs(Math.atan2((y1-y2),(x1-x2)))*(180/Math.PI)`
but i am getting 1-180 and 180 to 1 as i go around the pivoted point, instead of 1-360, what am i doing wrong??
It would be better if you made two other varibles:

dy=y2-y1
dx=x2-x1

Your y's and x's are backwards. also. instead of having to do the 180/Math.PI you could use:

object.rotation= ATAN2(x,y)

this returns in degrees.

I will give you a free script that I used to make an Ipod dial:

var dial=element ("dial2");
var windowScroller=element ("window scroller");

stage.addEventListener(Event.ENTER_FRAME,rot)

function rot(evt:Event):void
{
var mx=(mouseX-dial.x);
var my=(mouseY-dial.y);
dial.rotation= ATAN2(mx,-my)

var y0= 5
var y1= dial.rotation/1.4

for(var i:Number = 0;i<=130;i++)
{
windowScroller.y= y1;
}
}

This makes a dial rotate to the position of the mouse (face it). It also makes the windowScroller change y position as you rotate the mouse around.

6. Suthers,

Welcome to flashkit, I see you've found all the old threads.

If you look at Adobe's documentation, the parameters for atan2 are actually (y,x).
http://livedocs.adobe.com/flash/9.0/main/00001821.html

It's supposed to look similar to the way they teach you to find the angle of vector <x,y> in math class:
atan(y/x) -- what normal people use
atan2(y,x) -- what programmers use

#### Posting Permissions

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

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap

Click Here to Expand Forum to Full Width