Everywhere online says to use atan2(pointA.y-pointB.y, pointA.x, pointB.x) but it just doesn't work.
import pygame
import sys
from math import *
WIDTH, HEIGHT = 1024, 576
screen = pygame.display.set_mode((WIDTH, HEIGHT))
def rot(surface, angle):
rotated_surface = pygame.transform.rotozoom(surface, angle, 1)
rotated_rect = rotated_surface.get_rect()
return rotated_surface, rotated_rect
surf = pygame.Surface((64, 32), flags=pygame.SRCALPHA)
surf.fill((255, 0, 0))
def map_range(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
mouse_pos = pygame.Vector2(pygame.mouse.get_pos())
center = pygame.Vector2(surf.get_rect().center)
angle = degrees(atan2(mouse_pos.y - center.y, mouse_pos.x - center.x))
rotated_surface = rot(surf, angle)
rotated_surface[1].center = (WIDTH/3, HEIGHT/3)
screen.fill((0,0,0))
screen.blit(rotated_surface[0], rotated_surface[1])
pygame.display.update()
I converted it in degrees and all, but it just doesn't rotate properly towards the cursor. Am I missing something?