【问题标题】:SFML 2.1 how to make one sprite face another spriteSFML 2.1 如何使一个精灵面对另一个精灵
【发布时间】:2016-04-30 09:52:54
【问题描述】:

我正在尝试制作射击游戏,同时尝试对敌人进行编码 为了面向玩家,我尝试使用三角函数找到必要的旋转,但代码不起作用,并且敌人旋转不规律。这是代码:

void face(sf::Sprite& target, sf::Sprite& subject){
    int adjacent = subject.getPosition().x - target.getPosition().x;
    int opposite = target.getPosition().y - subject.getPosition().y;

    if (opposite == 0){
        opposite++;
    }
    if (adjacent == 0){
        adjacent++;
    }

    //if (adjacent < 0){
        //adjacent += 180;
    //}
    float result=atan(/*opposite / adjacent*/adjacent/opposite)*180/PI;

    subject.setRotation(result);
}

任何建议将不胜感激!

【问题讨论】:

  • adjacentopposite 应该是浮点数,因为 sf::Sprite 的位置是浮点数。

标签: c++ rotation sprite sfml trigonometry


【解决方案1】:

您必须使用浮动与相邻和相反。并以此改变结果:

float angle = atan(adjacent / opposite) * 180 / PI;

if (opposite > 0)
    angle += 180;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多