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?