# Expression returns negative angles...?

Get help using Construct 2

### » Wed May 04, 2016 8:02 am

I can use angle(x1,y1,x2,y2) to get an object to point towards another. This works fine; the object rotates between 0 and 359 as expected.

However, if I'm using this expression to set a variable instead of an object's angle, it returns negative angles. For example, 180 becomes -180 / 225 becomes -135, and so on.

What's the deal with that?

To clarify on what I'm doing...I'm using a sprite object with 8 animations that represent 8 different angles it can face. They are named a0, a45, a90, a135, and so on. I'm using the angle() expression to get the angle between this object and my player, but storing it in an instance variable. I then use this instance variable to tell the object which animation to play (which angle it should be facing). Why? Because the perspective of this object doesn't allow me to simply rotate it - I have to use a different sprite for each 45 degree increment.

I could solve this by putting these negative angles in the animation names, but C2 won't allow the "-" character, so that's not an option.
B
251
S
32
G
13
Posts: 1,794
Reputation: 19,193

### » Wed May 04, 2016 8:17 am

I've run into this issue before. I ended up using an intermediate function:
On angle360(x1,y1,x2,y2)
If angle(params) < 0 | return 360+angle(params)
Else | return angle(params)

This will give you the standard 360 degree angles starting from 0 at 3oclock going counter clockwise.
B
59
S
29
G
122
Posts: 1,816
Reputation: 69,034

### » Wed May 04, 2016 4:04 pm

im usually using abs(angle(x1,y1,x2,y2)) that solves it for me most of times.

abs-olute basically takes "-n" and shows u "n" takes "n.nnnnnnnnnnnnn" and shows "n" absolute values to the rescue lol
Edited: however my suggestion it might create problems giving only positive values... R0j0's formula should work better.. makes more sense in mathematical way.
Last edited by gamecorpstudio on Wed May 04, 2016 4:27 pm, edited 3 times in total.
B
83
S
25
G
70
Posts: 1,372
Reputation: 44,859

### » Wed May 04, 2016 4:16 pm

It's a mathimatical reason. The angle() function uses atan() to do the calculation, which has a range of [-180,180). Also that range is often referred to as a normalized angle and is usuful at times. Having the angle in the range of [0,360) is mainly useful in the case you stated above, and I usually convert it over in a expression with:
(angle(x1,y1,x2,y2)+360)%360
B
98
S
36
G
131
Posts: 5,521
Reputation: 83,505

### » Wed May 04, 2016 5:53 pm

R0J0hound wrote:(angle(x1,y1,x2,y2)+360)%360

I forgot modulo! That is a much more elegant solution.
B
59
S
29
G
122
Posts: 1,816
Reputation: 69,034

### » Wed May 04, 2016 7:17 pm

Thanks guys! R0j0hound's looked the simplest so I ended up going with that. To be exact I used int(((angle(self.X,self.Y,player.X,player.Y)+360)%360)/45)*45 to get the 0-360 range as an integer in 45 degree increments. Yeesh.
B
251
S
32
G
13
Posts: 1,794
Reputation: 19,193