在鼠标周围旋转精灵

时间:2015-06-12 14:39:30

标签: c++ sfml

我很快就尝试了SFML 3周。我发现了关于在鼠标周围旋转精灵的事情。从长距离看起来很好。但它越紧密,它变得越不准确。我的图片是960 x 540

见下相关代码:

Game::Game()
    : mWindow(sf::VideoMode(WindowWidth, WindowHeight), WindowName, sf::Style::Close)
    , mPlayerTexture()
    , mPlayer()
    , mIsMovingDown(false)
    , mIsMovingLeft(false)
    , mIsMovingRight(false)
    , mIsMovingUp(false)
 {
    if (!mPlayerTexture.loadFromFile("Media/Texture/eagle.png"))
   { }
    mPlayer.setTexture(mPlayerTexture);
    mPlayer.setScale(sf::Vector2f(0.25, 0.25));
    mPlayer.setOrigin(sf::Vector2f(120.f, 135.f));
 }

 void Game::lookAtMouse(sf::RenderWindow &win)
 {
    sf::Vector2f curPos = mPlayer.getPosition();
    sf::Vector2i position = sf::Mouse::getPosition(win);

    const float PI = 3.14159265; 

    float dx = curPos.x - position.x;
    float dy = curPos.y - position.y;

    float rotation = (atan2(dy, dx)) * 180 / PI;

    mPlayer.setRotation(rotation + 180);
 }

0 个答案:

没有答案