pythonmathpygamedegreesradians

Pygame image not facing the mouse


Here is my rotating code:

pos = pygame.mouse.get_pos()
x_dist = pos[0] - self.rect.centerx
y_dist = -(pos[1] - self.rect.centery)
self.angle = math.degrees(math.atan2(y_dist, x_dist))
self.image = pygame.transform.rotate(self.original_image, self.angle)

Here is my image:

enter image description here

The problem is, when I rotate it, I have to subtract the angle by 90 to get the right result.

I have searched all over stack overflow but nothing works.


Solution

  • The problem is with the image, since it's facing up. When I face my mouse pointer directly right from the arrow, the console prints 0 degrees as the calculated angle, meaning the image will say the same, as in up. The proper solution is to rotate the image, but I just rotated it since I didn't want to waste time.

    self.image = pygame.transform.rotate(self.original_image, self.angle - 90)
    

    The - 90 makes the arrow rotate 270 degrees instead of 0 (360) which fixes this.