How do I make my player rotate towards mouse position?

Basically I need to make the player face the mouse pointer, and though I can see something is happening, it’s not at all what I need.

I know this has been asked before, but trying to implement those answers doesn’t seem to work. So if someone could look over my code and maybe tell me where I mess up, that would be greatly appreciated!

class Player(pygame.sprite.Sprite):
    def __init__(self, game, x, y):
        self._layer = PLAYER_LAYER
        self.groups = game.all_sprites
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.image = game.player_img
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.hit_rect = PLAYER_HIT_RECT
        self.hit_rect.center = self.rect.center
        self.vel = vec(0, 0)
        self.pos = vec(x, y)
        self.rot = 0
    def update(self):
        rel_x, rel_y = pygame.mouse.get_pos() - self.pos
        self.rot = -math.degrees(math.atan2(rel_y, rel_x))
        self.image = pygame.transform.rotate(self.game.player_img, self.rot)
        self.rect = self.image.get_rect()
        self.rect.center = self.pos
        self.pos += self.vel * self.game.dt

class Camera:
    def __init__(self, width, height):
        self.camera = pygame.Rect(0, 0, width, height)
        self.width = width
        self.height = height

    def apply(self, entity):
        return entity.rect.move(self.camera.topleft)

    def apply_rect(self, rect):
        return rect.move(self.camera.topleft)

    def update(self, target):
        x = -target.rect.centerx + int(WIDTH / 2)
        y = -target.rect.centery + int(HEIGHT / 2)

        x = min(-TILESIZE, x)
        y = min(-TILESIZE, y)
        x = max(-(self.width - WIDTH - TILESIZE), x)
        y = max(-(self.height - HEIGHT - TILESIZE), y)
        self.camera = pygame.Rect(x, y, self.width, self.height)

Placing my player in the upper left corner, where there is no camera offset, makes the rotation work, however when placed elsewhere, it screws it up.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

See How to rotate an image(player) to the mouse direction?. What you want to do depends on which part of the player (top or right etc.) should be orientated to the mouse.

Don’t compute and sum relative angles. Calculate the vector form the the player to the mouse:

player_x, player_y = # position of the player
mouse_x, mouse_y   = pygame.mouse.get_pos()
   
dir_x, dir_y = mouse_x - player_x, mouse_y - player_y

The angle of the vector can be calculated by math.atan2. The angle has to be calculated relative to the base orientation of the player.

e.g.

The right side of the player is orientated to the mouse:

angle = (180 / math.pi) * math.atan2(-dir_y, dir_x)

The top of the player is orientated to the mouse:

angle = (180 / math.pi) * math.atan2(-dir_x, -dir_y)

A basic alignment can be set using a correction angle. For example 45 for a player looking at the top right:

angle = (180 / math.pi) * math.atan2(-dir_y, dir_x) - 45

The method update may look like this:

def update(self):
    self.pos += self.vel * self.game.dt

    mouse_x, mouse_y = pygame.mouse.get_pos()
    player_x, player_y = self.pos

    dir_x, dir_y = mouse_x - player_x, mouse_y - player_y
    
    #self.rot = (180 / math.pi) * math.atan2(-dir_y, dir_x)
    #self.rot = (180 / math.pi) * math.atan2(-dir_y, dir_x) - 45
    self.rot = (180 / math.pi) * math.atan2(-dir_x, -dir_y)
    
    self.image = pygame.transform.rotate(self.game.player_img, self.rot)
    self.rect = self.image.get_rect()
    self.rect.center = self.pos

Minimal example: How do I make my player rotate towards mouse position? repl.it/@Rabbid76/PyGame-RotateWithMouse

How do I make my player rotate towards mouse position?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x