0
\$\begingroup\$

I've been struggling to calculate the angle alpha between an object and a certain point M so that I can move that object to M.

To calculate alpha, I'm using trigonometry and more precisely the atan2 function from the python math module :

class Obj:
...
    @property
    def alpha(self):
        return atan2(self.center[1] - my, self.center[0] - mx)

The problem I'm struggling with is that alpha, the calculated angle is not accurate. Take a look at this image :

image

So as the representation shows, in a situation where the obj and the point M are aligned, and that obj is on top of M, alpha is equal to 90 degrees !!

I can't understand why this happens. I know in math that a straight line is 180° or pi radians but not 90° !

Furthermore, if the point M was situated on top of the object on the screen, alpha will be negative.

What I want to do, is to suppose that the center of the obj is center of an 'imaginary' circle. Then, calculate the angle between the obj and M just like the image below shows :

This means that I want alpha to be equal to zero degrees if M is on the right of the obj forming a horizontal line, and I want it to be equal to 90° if M is on top of the obj forming a vertical line and so on ..

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The problem turned out to be caused by flipped values in the atan2 function's parameters.

So the alpha angle should be calculated by this expression :

atan2(self.center[1] - my, mx - self.center[0])

This will fix the flipped location of the axes. Now, the only issue that remains is that there are negative values that we need to get rid of. This can be done by this simple calculation as @DMGregory mentionned :

(alpha+360) % 360

\$\endgroup\$

You must log in to answer this question.

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