• Advertisement
Sign in to follow this  

Collision Detection for Entities Chasing the Player

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement