# How to: Check if angle is between two other angles

Get help using Construct 2

### » Tue Sep 25, 2012 8:10 pm

Hi

After spending several hours trying to figure out how to check if one object is within a certain angle limit of another object. I finally found a solution, and thought i would share it here as i could imagine others might run into the same problem.

To quickly explain where I ran into the problem, and so its easier to understand what im talking about. I am working on a tower defence game, and i made a simple tower that track the closest enemy as they come within range. To make sure that the tower doesnt shoot before its actually aiming at an enemy, i wanted to test if an enemy was within 15 degree of the towers cannon angle.

Since Angle(X1,Y1,X2,Y2) return a value in degree between the 2 objects, and the tower cannon angle is an clockwise degree value (think thats correct ). I ran into problems with towers not wanting to shoot, based on where they were placed in comparison to the enemy. Hope it make sense so far as im not really good at these math things.)

And therefore the credit of this doesnt go to me, im just sharing information, and i wont be able to explain exactly what it means, but I will link to the original page in the end for those that want to know the details.

However to implement it in construct 2 you can do the following:

To the object that need to have a limited angle of firing (My case cannon):

- Add "is within angle" conditions.

- In the "within" field add your limit.
- In the "Angle" field you add:
(360 + Angle(X1,Y1,X2,Y2)% 360)% 360

(As far as i understand it, it will make sure that the Angle(X1,Y1,X2,Y2) result will always be in positiv degree from [0 to 360], matching return value of the object.Angle that you want to test.....however might be completely wrong )

Source: Is an angle between two other angles? nimos1002012-09-25 20:41:26
B
39
S
9
G
2
Posts: 1,124
Reputation: 6,312

### » Tue Sep 25, 2012 9:33 pm

A very nice guide from a new user! Excellent!

@Kyatric, if something similar does not already exist, this would be nice in the FAQ.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,390

### » Wed Sep 26, 2012 3:57 am

You should post this in the tutorial section :D
B
18
S
9
G
5
Posts: 77
Reputation: 4,936

### » Wed Sep 26, 2012 9:59 pm

It shouldn't be necessary to make the angle be in the range of [0,360). Using just Angle(X1,Y1,X2,Y2) should be enough.
B
75
S
21
G
48
Posts: 4,667
Reputation: 37,199

### » Wed Sep 26, 2012 10:53 pm

[QUOTE=R0J0hound] It shouldn't be necessary to make the angle be in the range of [0,360). Using just Angle(X1,Y1,X2,Y2) should be enough.[/QUOTE]

You might be right, I just couldnt get it to work correctly.

If you have two object:

y->

x->

(-> = object.Angle)

Angle(x1,y1,x2,y2) give you the angle in degree between X and Y, in this case lets say its around 40 degree, then Angle will return -40 degree, since X is below Y and we put X as the x1, y1, and Y as x2,y2.

Let say i wanted to check if X angle was within 10 degree of Y, when X is rotated.

So after rotating X with "Set angle towards" so its pointing at Y, X.Angle would be around 320 degree since its calculated clockwise, so it will never be within 10 degree of Angle(x1,y1,x2,y2) which is -40 degree.

If you use the above math instead (360 + Angle(x1,y1,x2,y2)% 360)% 360 the degree returned is the same as x.Angle in this case instead of -40 its around 320. And will therefore return true, if you rotate X so its 11 degree of pointing at Y you will get a false, when we assume that the we want to check if its within 10 degree.

nimos1002012-09-26 23:04:40
B
39
S
9
G
2
Posts: 1,124
Reputation: 6,312

### » Thu Sep 27, 2012 7:00 pm

I'm pretty sure C2 does the conversion internally so it doesn't matter if the angle given by angle() is -40, 320, -400, 680... etc.

Here is my test:
http://dl.dropbox.com/u/5426011/examples14/angle_in_range.capx
B
75
S
21
G
48
Posts: 4,667
Reputation: 37,199

Return to How do I....?

### Who is online

Users browsing this forum: Yahoo [Bot] and 9 guests