让我的敌人走向我的玩家C++

Jea*_*aul 0 c++ game-ai sfml

我想让我Enemy搬到我家Player.

我知道的事情:

  • 玩家的位置
  • Enemie`'的位置
  • 敌人的速度

我需要做的事情:

  • 了解玩家的方向,让敌人移动

所以我认为我需要做的是根据球员的位置"规范化"敌人的位置,这样我就知道要去哪里了,两者都有基于的位置Vector2f.

这就是我在敌人身上的代码:

void Enemy::Move()
{
    //cout << "Move" << endl;

    // Make movement
    Vector2f playerPosition = EntityManager::Instance().player.GetPosition();
    Vector2f thisPosition;
    thisPosition.x = xPos;
    thisPosition.y = yPos;
    //Vector2f direction = normalize(playerPosition - thisPosition);

    speed = 5;
    //EntityManager::Instance().enemy.enemyVisual.move(speed * direction);
}

Vector2f normalize(const Vector2f& source)
{
    float length = sqrt((source.x * source.x) + (source.y * source.y));
    if (length != 0)
        return Vector2f(source.x / length, source.y / length);
    else
        return source;
}
Run Code Online (Sandbox Code Playgroud)

错误是:

'normalize': identifier not found
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Cor*_*mer 6

您的定义normalize直到您使用它之后才会出现.要么之前移动定义Enemy::Move,要么在包含之后将函数声明放在文件的顶部:

Vector2f normalize(const Vector2f& source);
Run Code Online (Sandbox Code Playgroud)

这是相同行为的一个小例子.