-4

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.

11
  • maybe PyGame has "angle 0" in different place as you expect. OR maybe you should use image rotated 90 degrees at start - horizontal arrow instead vertical arrow. Commented Aug 2 at 20:59
  • looks like it... I don't know how to rotate image, I tried using PIL and converting it into a pygame surface object by using to_string, but to no avail Commented Aug 2 at 21:02
  • I got the answer. Commented Aug 2 at 21:04
  • you could also use print() to check if your calculations are correct. Maybe it needs centerx - pos[0] instead of pos[0] - centerx (and the same with y) Commented Aug 2 at 21:04
  • its the image that's causing the problem Commented Aug 2 at 21:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.