How to: Check if angle is between two other angles

0 favourites
  • 6 posts
From the Asset Store
Footsteps SFX Two contains 450 sounds of steps and jumps on different surfaces.
  • 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 <img src="smileys/smiley9.gif" border="0" align="middle">). 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 <img src="smileys/smiley36.gif" border="0" align="middle"> 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 <img src="smileys/smiley4.gif" border="0" align="middle">)

    Source: Is an angle between two other angles?

  • A very nice guide from a new user! Excellent!

    Kyatric, if something similar does not already exist, this would be nice in the FAQ.

  • You should post this in the tutorial section :D

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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. 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.

  • 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:

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)