如何让我的播放器向鼠标位置旋转? [英] How do I make my player rotate towards mouse position?

查看:53
本文介绍了如何让我的播放器向鼠标位置旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我需要让玩家面对鼠标指针,虽然我可以看到正在发生的事情,但这根本不是我需要的.

我知道以前有人问过这个问题,但是尝试实施这些答案似乎不起作用.因此,如果有人可以查看我的代码并告诉我我哪里搞砸了,那将不胜感激!

class Player(pygame.sprite.Sprite):def __init__(self, game, x, y):self._layer = PLAYER_LAYERself.groups = game.all_spritespygame.sprite.Sprite.__init__(self, self.groups)self.image = game.player_imgself.rect = self.image.get_rect()self.rect.center = (x, y)self.hit_rect = PLAYER_HIT_RECTself.hit_rect.center = self.rect.centerself.vel = vec(0, 0)self.pos = vec(x, y)self.rot = 0定义更新(自我):rel_x, rel_y = pygame.mouse.get_pos() - self.posself.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.posself.pos += self.vel * self.game.dt类相机:def __init__(self, width, height):self.camera = pygame.Rect(0, 0, width, height)self.width = 宽度self.height = 高度def 应用(自我,实体):返回 entity.rect.move(self.camera.topleft)def apply_rect(self, rect):返回rect.move(self.camera.topleft)定义更新(自我,目标):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)

将我的播放器放在左上角,那里没有相机偏移,可以使旋转工作,但是当放在其他地方时,它会搞砸.

解决方案

你想要做什么取决于玩家的哪个部分(顶部或右侧等)应该朝向鼠标.

不要计算和求和相对角度.计算从玩家到鼠标的向量:

player_x, player_y = # 玩家的位置mouse_x, mouse_y = pygame.mouse.get_pos()dir_x, dir_y = mouse_x - player_x, mouse_y - player_y

矢量的角度可以通过

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.

解决方案

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:

这篇关于如何让我的播放器向鼠标位置旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆