# ATAN2()

0 favourites
• 10 posts
• It appears the expression atan2(y,x) isn't available in construct 2. Could we get it implemented? It's already available in javascript, but not in traditional expressions. I could code my own extension, but it's better if it were implemented as a SYSTEM expression.

• It's used in the system angle() expression, which as far as I'm aware, is the only purpose for atan2 - calculating the angle between points.

If you really do need atan2 for something else, I'd be interested to know what it is! You can also get an atan2(y, x) anyway by doing angle(0, 0, x, y).

• It appears the expression atan2(y,x) isn't available in construct 2.

``var a = Math.atan2(y, x);``

I'm not sure if it is what you are looking for, but doesn't it work ?

If it doesn't, then the EightDir behavior where this is used is bugged <img src="smileys/smiley3.gif" border="0" align="middle" />

• atan2() exists in the Javascript Math object, but it can't be directly used in C2. But it seems Ashley proposed a working alternative using the already implemented angle function.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• The only known purpose of atan2(), as far as I know too, is to calculate the angle between two points, just like Ashley said. The proposed solution of using angle() works for me. Would be nice to have atan2() though, even as a hidden expression that must be typed, for when we're using premade formulas, but this is just a very minor inconvenience.

• yep, angle(0,0,x,y) works fine - using this to aut-rotate a physics object according to current X,Y velocity.

• I found this thread looking how to calculate smallest difference between two angles

### (like this atan2(sin(x-y), cos(x-y)) )

.

If you're looking for same thing just use awesome embedded construct function:

``````// returns the smallest difference between two angles
anglediff(a1, a2)
[/code:13gy8ef2]

Unfortunately, I don't have enough reputation to post url to "System expressions" Construct 2 reference page``````
• I'm using atan2 to find the distance between two points, following the code in this page:

http://www.movable-type.co.uk/scripts/latlong.html

The inputs for atan2 in the "Distance" section of that page can be used in C2 using the angle function, as mentioned by Ashley above. The syntax is angle(0,0,sqrt(1-a),sqrt(a)). On the website, the syntax is atan2(sqrt(a),sqrt(1-a)).

I've been able to get the function working, but the output is off by 10-20%. Not sure what's happening.

Has anyone used this successfully?

Here's my code:

• In C2 sin() and cos() take angles not degrees, so if the value is in radians you need to convert it to degrees inside sin and cos. Also in the context of this equation it expects angle() to give a value in radians instead of degrees, so you'll need to convert it.

So literally it would be this:

r = 6371000

phi1 = lat1*pi/180

phi2 = lat2*pi/180

deltaPhi = (lat2-lat1)*pi/180

deltaLambda = (lng1-lng2)*pi/180

a = sin(deltaPhi/2*180/pi)^2 + cos(phi1*180/pi)*cos(phi2*180/pi)*sin(deltaLambda/2*180/pi)^2

c = 2*angle(0,0,sqrt(1-a),sqrt(a))*pi/180

d = r*c

But you can cut out most of the radian conversions and get just this:

r = 6371000

a = sin((lat2-lat1)/2)^2 + cos(lat1)*cos(lat2)*sin((lng1-lng2)/2)^2

c = 2*angle(0,0,sqrt(1-a),sqrt(a))*pi/180

d = r*c

• That did the trick. I used your condensed method and got the exact results I needed.

Here's the calc done on the website:

Here's the code that calc's it:

Here's my app's calc:

What I wasn't able to get was the 0.1822 km result (or 182 m in my app's output). My app was generating an output of 211 m (or 0.211 km). Really close but wrong, not sure why. Anyway, I can move on thanks to you.

• 10 posts