制作平滑滑行动画 SFML

Make smooth gliding animation SFML

本文关键字:动画 SFML 平滑      更新时间:2023-10-16

所以,我有一个不允许在屏幕外移动的精灵。我做了一个函数,将播放器放回屏幕,它应该在的位置(我将播放器的原点设置在中间,这就是 *0.5 代表的意思):

    if (playerSprite.getPosition().y + playerSprite.getGlobalBounds().height*0.5 > VideoMode::getDesktopMode().height)
        playerSprite.setPosition(Vector2f(playerSprite.getPosition().x, VideoMode::getDesktopMode().height - playerSprite.getGlobalBounds().height*0.5));
    if (playerSprite.getPosition().x + playerSprite.getGlobalBounds().width*0.5 > VideoMode::getDesktopMode().width)
        playerSprite.setPosition(VideoMode::getDesktopMode().width - playerSprite.getGlobalBounds().width*0.5, playerSprite.getPosition().y);
    if (playerSprite.getPosition().y - playerSprite.getGlobalBounds().height*0.5 < 0)
        playerSprite.setPosition(Vector2f(playerSprite.getPosition().x, playerSprite.getGlobalBounds().height*0.5));
    if (playerSprite.getPosition().x - playerSprite.getGlobalBounds().width*0.5 < 0)
        playerSprite.setPosition(Vector2f(playerSprite.getGlobalBounds().width*0.5, playerSprite.getPosition().y));

有了这几段代码,我的播放器不仅会在碰到屏幕边缘时停止(当然,当它不垂直时),而是在屏幕边缘平滑地滑动。但是当我试图为另一个精灵做同样的事情时,比如说一个矩形精灵,无论碰撞角度如何,玩家都会停下来。谁能向我解释这种行为,并建议我如何让它沿着边缘滑动?这将是我已经尝试过的(这里仅用于向上移动,它是左、右、向下的模拟):

    if (Keyboard::isKeyPressed(Keyboard::W)
sprite.movePlayer('W', speed);
    if(playerSprite.getGlobalBounds().intersects(entity.getGlobalBounds()))
sprite.movePlayer('S', speed);

问题可能是,对于每个更新帧,您都在对精灵在其所命中的边界方向上的速度执行硬否定,同时仍然在它仍在边界内的方向上应用速度。 当精灵到达边界时,冻结所有移动,而不仅仅是向边界移动。