C++ 如何翻转图像,同时将枪对准鼠标?(SFML)

C++ 如何翻转图像,同时将枪对准鼠标?(SFML),c++,rotation,sfml,image-rotation,C++,Rotation,Sfml,Image Rotation,我正在制作一个射击游戏,玩家可以向鼠标射击,所以我让枪指向鼠标,但我不知道如何在图像向左翻转时正确翻转图像。我的意思是,我确实翻转了图像,但它没有对齐。我不知道该怎么解释,这是我的代码 void PortalGun::update(Vector2i mPos) { float pi = 3.14159265359; float rotation = atan2(sprite.getGlobalBounds().top - mPos.y,sprite.getGlobalBoun

我正在制作一个射击游戏,玩家可以向鼠标射击,所以我让枪指向鼠标,但我不知道如何在图像向左翻转时正确翻转图像。我的意思是,我确实翻转了图像,但它没有对齐。我不知道该怎么解释,这是我的代码

void PortalGun::update(Vector2i mPos) {

    float pi = 3.14159265359;

    float rotation = atan2(sprite.getGlobalBounds().top - mPos.y,sprite.getGlobalBounds().left - mPos.x) * 180 / pi;

    int x = player->sprite.getGlobalBounds().left + 16;
    int y = player->sprite.getGlobalBounds().top;

    if (rotation > -90 && rotation < 90) {
        player->dir = -1;
        sprite.setTextureRect(IntRect(0, 32, 64, -32));
    } else {
        player->dir = 1;
        sprite.setTextureRect(IntRect(0, 0, 64, 32));
    }


    sprite.setPosition(x, y + 15);
    sprite.setRotation(rotation + 170);

}
void PortalGun::更新(Vector2i mPos){
浮点数pi=3.14159265359;
浮点旋转=atan2(sprite.getGlobalBounds().top-mPos.y,sprite.getGlobalBounds().left-mPos.x)*180/pi;
intx=player->sprite.getGlobalBounds().left+16;
int y=player->sprite.getGlobalBounds().top;
如果(旋转>-90和旋转<90){
player->dir=-1;
setTextureRect(IntRect(0,32,64,-32));
}否则{
player->dir=1;
sprite.setTextureRect(IntRect(0,0,64,32));
}
设定位置(x,y+15);
设置旋转(旋转+170);
}
当鼠标位于枪的左侧时,它会翻转图像,但会一直向上旋转,使鼠标高出20像素。旋转时我不能改变位置,那我该怎么办?抱歉,听起来有点神秘,有点难以解释。

  • 首先,您应该将精灵的原点设置为您希望旋转枪的点(通常是手柄或安装点)。为此,请使用
    sf::Sprite::setOrigin()
    。传递的坐标相对于精灵的左上角

  • 要获取或设置精灵在世界(您的原点所在地)中的位置,可以使用
    sf::sprite::getPosition()
    sf::sprite::setPosition()

  • 你的旋转可以保持原样。它将围绕设置的原点旋转

  • 要镜像您的精灵,只需使用负因子缩放(
    sf::sprite::setScale()
    )它:
    sprite.setScale(-1,1)负因子将在设定的原点镜像/翻转图像,而不强制更新纹理坐标


您使用
sf::Sprite::getGlobalBounds()有什么具体原因吗