SFML community forums

Help => General => Topic started by: snowf1ake on December 15, 2017, 06:28:09 am

Title: TDS Shooter Enemy Programming issues
Post by: snowf1ake on December 15, 2017, 06:28:09 am
Hi. I've got an idea of creating a mini-shooter with a top-down perspective.

Everything went smoothly till i came to the enemy programming. I wanted the enemies to move towards the player, and i knew in theory how i am to do it. I used this piece of code in order to determine the direction at which the enemy will chase the player.

float m_angle = atan2(target.getPosition().y - enemy.getPosition().y, target.getPosition().x - enemy.getPosition().x);
        sf::Vector2f direction(std::cos(m_angle), std::sin(m_angle));

        enemy.move(direction * 190.f * deltatime);
 

I placed it into the enemy::update function- same function where the window.draw(enemy); was. Although it worked, and the enemy was chasing the player around the map, it has shown different results when i had several enemies. I made a scene where 7 enemies were supposed to chase the player. And they did, in fact. However, while running towards him, the enemies all took the same position and were like in each other. So instead of 7 circles chasing my player, i saw 1 circle that included all those 7( screenshots provided ).

As it can be seen at screenshot 1, in the beginning, all seven circles are separate. However, after a short chase, they come to the same position and start moving synchronically. How do i make it look like a normal chase, without my circles coming into one-another(keep distance)?

Thanks in advance.
Title: Re: TDS Shooter Enemy Programming issues
Post by: achpile on December 15, 2017, 07:37:47 am
just make them not to overlap. use collision detection etc.
Title: Re: TDS Shooter Enemy Programming issues
Post by: snowf1ake on December 15, 2017, 11:00:34 pm
But they will still form a row of stupid shooting targets, wont they?
Title: Re: TDS Shooter Enemy Programming issues
Post by: fallahn on December 15, 2017, 11:26:17 pm
Perhaps you want to look at flocking behaviour

https://gamedevelopment.tutsplus.com/tutorials/3-simple-rules-of-flocking-behaviors-alignment-cohesion-and-separation--gamedev-3444
Title: Re: TDS Shooter Enemy Programming issues
Post by: Arcade on December 15, 2017, 11:32:13 pm
To expand on fallahn's answer a bit, the best solution depends on what kind of game you are making and how smart the enemies should be. If you don't want your enemies to end up in a line then you will need to implement something a little more advanced than just taking the direct angle towards the player.

For example, if you need your enemies to be kind of dumb (zombies?) then you can just give them a random "reaction delay" such that they don't react instantly to a player's position. Different enemies can have different delays so that they don't end up all moving together. Or maybe they don't take the perfect angle towards the player.

If you need your enemies to be smart you can try to predict where the player is moving to and try to cut them off. Or, as fallahn mentioned, you can try to implement a more advanced "flocking" behavior for your group of enemies.

You can google these terms and probably find good resources on how to implement different types of moving AIs. These aren't really SFML related questions, so you may not get very detailed help here.
Title: Re: TDS Shooter Enemy Programming issues
Post by: snowf1ake on December 16, 2017, 02:39:17 am
Thanks very much, i was starting to believe that i will never get a good fat answer :)