# ATAN2()

Discussion and feedback on Construct 2

### » Wed Aug 17, 2011 11:37 pm

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.Fimbul2011-08-17 23:44:48
B
35
S
8
G
8
Posts: 532
Reputation: 6,868

### » Wed Aug 17, 2011 11:49 pm

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).
Scirra Founder
B
355
S
208
G
71
Posts: 22,832
Reputation: 176,750

### » Thu Aug 18, 2011 1:34 pm

[QUOTE=Fimbul] It appears the expression atan2(y,x) isn't available in construct 2.[/QUOTE]

[code]var a = Math.atan2(y, x);[/code]

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 Kyatric2011-08-18 13:37:28
New to Construct ? Where to start

Please attach a capx to any help request or bug report !
Moderator
B
243
S
85
G
39
Posts: 6,943
Reputation: 56,930

### » Thu Aug 18, 2011 2:24 pm

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.
B
45
S
16
G
14
Posts: 832
Reputation: 11,642

### » Thu Aug 18, 2011 6:27 pm

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.
B
35
S
8
G
8
Posts: 532
Reputation: 6,868

### » Sun Sep 14, 2014 12:00 pm

yep, angle(0,0,x,y) works fine - using this to aut-rotate a physics object according to current X,Y velocity.
B
42
S
11
G
4
Posts: 505
Reputation: 7,267

### » Sat Jan 31, 2015 4:33 pm

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:
Code: Select all
`// returns the smallest difference between two anglesanglediff(a1, a2)`

Unfortunately, I don't have enough reputation to post url to "System expressions" Construct 2 reference page
B
7
Posts: 15
Reputation: 307

### » Tue Sep 29, 2015 5:20 pm

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:

B
16
S
2
Posts: 80
Reputation: 1,445

### » Tue Sep 29, 2015 6:07 pm

@hmott
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
B
75
S
21
G
48
Posts: 4,668
Reputation: 37,201

### » Wed Sep 30, 2015 4:53 pm

@R0J0hound

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.
B
16
S
2
Posts: 80
Reputation: 1,445

Next