Angles between the point on the top-right and the object

My player have a line of sight and all objects between the white lines, turns red. To compute this, the following conditions are met: if the angle between the yellow line and the red line are small or equal to the angle between the yellow line and the white line above, then the object is considered to be inside the line of sight. It also works if the object are below the white line. Here is the code:

def _getTeta(obj_1, obj_2):
    deltaX = (obj_1.centerx - obj_2.right)
    deltaY = (obj_2.top - obj_1.centery)
    return math.acos(deltaX / math.sqrt(deltaX**2 + deltaY**2))

Obs.: centerx and right are X values and centery and top are Y value; all of them representing values encountered under certain points of each object's rectangle.

Then, I do:

if _getTeta(platform.rect, self.rect) <= _getTeta(abovePoint, self.rect):
    return True

where above point is the point in which forms the white line on the top, and platform is the object to be checked.

Looking to the left is not working

However, as the second image shows, it doesn't work if the line of sight are inverted in the X axis. The problem is that I don't know how to set values right so it could also work when the player is looking to the left.

Let me know if I'm not being clear in my question! Thanks!


3 Answers 3


You don't need trigonometric functions for this. With fairly simple vector algebra, this can work for any viewing direction. Consider the normalised vector n (the yellow line) and a vector pointing to an object, x. The object is in the field of view if: n · x > a |x|, where a is a number between -1 and 1 indicating the view angle (cos φ/2 = a).

In components (2D), using n=(u,v) and x=(x,y):

if (u*x + v*y > a * sqrt(x²+y²)) return true;
  • \$\begingroup\$ ALso, just use Math.Abs(x) and perform the operation independently for each side of the Y-axis. \$\endgroup\$ Mar 2, 2013 at 23:42
  • \$\begingroup\$ So φ is the total angle that I want for the line of sight? \$\endgroup\$
    – Vinny
    Mar 4, 2013 at 2:51
  • \$\begingroup\$ @epiplon: Correct. In your drawing, it is the angle between the two white lines. \$\endgroup\$ Mar 4, 2013 at 10:43
  • \$\begingroup\$ It didn't work or I missed something. \$\endgroup\$
    – Vinny
    Mar 4, 2013 at 17:19
  • \$\begingroup\$ @epiplon: Could you post your code? \$\endgroup\$ Mar 4, 2013 at 17:23

You just nee to use the absolute value of x --> abs(x)


I figured out in a simple way.

First, I set the mouse position with a value relative to the player's position, making the last the center (0,0).

Second, what python was doing is to set a 180 angle degrees in a clock direction above the player and another 180 angle degrees in a clockwise direction below the player. As anyone can see, this makes a huge mess.

The solution was to make a simple calculation with angles:

if mouse.y < player.y:
    sightAngle = 2*pi - mouseAngle

This basically makes a true 360 angle degree around the player, making the sen() and cos() work properly. And then, the coordinate position of the points is set according to the window's coordinates.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .