# How to Prevent Enemy Overlap

I am working on a 2D side-scrolling action platformer.

I am currently working on the state logic for my enemies. These are humanoid enemies that walk on the ground

They currently have idle, patrol, chase, engage, and attack states.

The default state is patrol, where they walk back and forth on a platform. If they encounter a wall or cliff, they idle for a few seconds then turn around and patrol in the other direction.

If the player comes within a chase_radius, they start chasing the player. If the player comes within an attack_radius, they enter an engaged state where they attack every few seconds.

Currently, I don't have enemies colliding with one another. I allow them to overlap each other. This is nice in the event that two enemies are patrolling in opposite directions. They simply walk passed each other.

The problem is that when two enemies are chasing a player, when one enters the engaged state, they stop and idle until they attack. The second enemy continues to walk until they too are within attack range. They then stop directly on top of the previous enemy and idle until they attack.

In this case, the enemies pretty much completely overlap and it turns an interesting situation with two enemies into a situation with essentially one enemy.

What are some techniques or design choices I can make to prevent this ugly overlap? Ideally, I'd like the enemies to be able to walk passed each other when they are not engaged, but to do something different during combat so that they don't end up overlapping.

I have access to all the normal stuff (for example, raycasting, collision boxes, etc.).

Similar Questions:

1. Enemies overlapping (involves pathfinding, and isn't applicable to 2D sidescroller where enemies walk on ground.)
2. How to avoid enemies overlapping each other when chasing player in Unity? (solution of gradually pulling enemies apart when they overlap won't look natural with humanoid enemies that do melee attacks).

Have them collide with each other using collision geometry such as boxes and circles, but only when they are in chasing or even engaged state. You definitely need some collision detection mechanic to prevent overlap.

if you only want them to not overlap visually, then you can just do this:

private void avoidOverlap(Enemy other) {
if(this.x == other.x) { //or in some radius of this
this.renderx += 3; //renderx is the position where it is rendered
}
}


or you can substitute renderx with x to actually change the position of the enemy. note that this could bring somee problems such as not being reachable with short - ranged weapons or, in the case of other objects that deal damage to the enemy (e.g. spells, falling objects, pets...) it may pervent them from actually hitting the enemy

In my game, I use a couple of techniques, and they are fairly simple and quick to implement.

Each round, as I loop through my list of enemies. I have them register with a collision manager which basically places them into buckets. These buckets are regions based upon the co-ordinates of the enemy in question. As they register, I check that their is nothing else in the bucket, if there is I know there is an overlap of some sort. I then tell the enemy unit to change course for a predetermined period of time. This is one mechanism for removing the overlap, and it works well for my game.

Hope this helps.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.1511659324169159, "perplexity": 1700.650116884466}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2022-05/segments/1642320303356.40/warc/CC-MAIN-20220121101528-20220121131528-00033.warc.gz"}