Public Group

# Collision Detection for Entities Chasing the Player

This topic is 2552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I created enemies that simply go straight towards the player with this:

 int dx = (player.xPos + player.width / 2 ) - ( xPos + width / 2 ); int dy = (player.yPos + player.height / 2 ) - ( yPos + height / 2 ); float length = sqrt((dx * dx) + (dy * dy)); xVel = (dx * 2) / length; yVel = (dy * 2) / length; 

It works fine, but the after chasing the player a while, they all merge together. I get why it happens and have tried fixing it by have collision between the enemies. I know how to check if enemies are colliding, but what I don't know is how to react to it. I don't need an exact description of what to do, but if someone knows how to get the enemies to just kind of be next to each other rather than in the same spot it'd be great. Thanks.

##### Share on other sites
Why not just set a check in your logic code, that if two enemies are touching, the one enemy will have: movable = false; until the other enemy moves? The end result is like this:

/// Logic
(Enemy_One) within bounds of (Enemy_Two)
Movable is false

ect...

if (Enemy_One.Movable())
{
Enemy_One.Chase_Player(values...);
}

##### Share on other sites

Why not just set a check in your logic code, that if two enemies are touching, the one enemy will have: movable = false; until the other enemy moves? The end result is like this:

/// Logic
(Enemy_One) within bounds of (Enemy_Two)
Movable is false

ect...

if (Enemy_One.Movable())
{
Enemy_One.Chase_Player(values...);
}

Well... that works to an extent, but it seems kind of odd having one enemy just stop while the other passes through. I wanted to more or less keep them from passing through each-other.

##### Share on other sites
Well... that works to an extent, but it seems kind of odd having one enemy just stop while the other passes through. I wanted to more or less keep them from passing through each-other.
It'll really deppend of how many cycles per second your game will run, but you can simply move them, you can simply put the overlaping one in a position where they're "touching" but not overlaping. If your game updates fast enough it will be supernatural for anyone to be able to see this happening. To be more precise, no one will be even able to see this happening (not even superman!), since this will always be done before the rendering.

When you check for collision and there is, in fact, a collision, you can get the entities and move them away from each other.
You can move the first while keeping the second in place, or you can move both away from each other, under 60fps there'll be no difference.

take a look at this link here:
http://www.metanetso...orialA.html#toc
the part called [font="Arial Narrow"]--= calculating the projection vector =-- [/font](CTRL+F it) under section 1.

##### Share on other sites
You could also look at a separate 2d physics library. In the last project I did (check my blog, in my sig), my enemies do something like your's (except they only change directions every x seconds). Since I'm using a 2d physics library to control my movement and collisions, they "automatically" just bump into each other.

FWIW, when I realized the power 2d physics libraries gave to 2d game programming, I was re-invigorated to write some games.

##### Share on other sites
Bounce back ?
When a collision happens, the enemies move in the exact opposite direction for 300 ticks ?

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013707
×