简体   繁体   中英

SFML - Move object towards coord

so I am working on an AI system with C++ and SFML in which I want the enemies to follow the player. Im going to create it so the enemies are moving towards the point at which the player was 30 frames ago (so it will constantly updating). But my question is simple, what is the math behind getting the enemy to move to a specific point? Like let's say the player is at (230, 400) and the enemy is at (100, 200) how do I get the enemy (using the .move() function with a speed of 3) to move to that point? Any help would be fantastic!

---UPDATED BELOW WITH CURRENT CODE FOR HANDLING MOVEMENT-----

float angle = atan2(v2iEnemyPos[1].y - rsPlayer.getPosition().y, v2iEnemyPos[1].x - rsPlayer.getPosition().x);
    angle =angle * 180 / (atan(1) * 4);
   sf::Vector2f newpos((cos(angle))*2, (sin(angle))*2);
    std::cout << newpos.x << " " << newpos.y << std::endl;
    rsEnemy[1].move(newpos.x, newpos.y);
    rwWinOne.draw(rsPlayer);
    rwWinOne.display();
    rwWinOne.clear();

The direction to move your enemy into is simply the difference between the player position and the enemy position. However, you want the enemies to move with constant speed, so you need to normalize the result. This will give you the direction as a vector of length 1.

Vector2u direction = normalize(player.getPosition() - enemy.getPosition());

You can now multiply this direction by the speed constant of that enemy's type. The result is a vector with it's length depending on the speed factor instead of the distance to the player. Just use the result to move your enemy.

enemy.move(speed * direction);

However, you perform that once a frame and framerates can vary between machines and configurations. Therefore, you should add a the elapsed time since the last move call, which might be your frame time, as factor to the equation. This way, if a frame takes a longer than usual, the enemy will be moved further to comprehend this, and vice versa.

// Initialize a timer
sf::Clock clock;

// Get elapsed time at the beginning of a frame
// I multiply this by target framerate to get a value around one
float delta = clock.restart().asSeconds() * 60;

// Move all objects based on elapsed time
Vector2u direction = normalize(player.getPosition() - enemy.getPosition());
enemy.move(delta * speed * direction);

By the way, basic knowledge of linear algebra is needed very often in game development, so you might pay out to attend an online course.

You should consider the elapsed time (since last frame) - dt - in your calculation.

The enemy has a given speed: enemy_velocity.

I won't use the move() function, but the SetPosition().

You have to calculate the new position of the enemy:

enemy.x += dt * enemy_velocity * cos(angle);
enemy.y += dt * enemy_velocity * sin(angle);

And affect it with SetPosition().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM