• Create Account

## Collision Detection for Entities Chasing the Player

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Oatskits  Members

Posted 22 December 2011 - 06:29 PM

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.

### #2Black-Rook  Members

Posted 22 December 2011 - 07:57 PM

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...);
}
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

### #3Oatskits  Members

Posted 22 December 2011 - 08:30 PM

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.

### #4dejaime  Members

Posted 22 December 2011 - 09:51 PM

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 --= calculating the projection vector =-- (CTRL+F it) under section 1.

### #5BeerNutts  Members

Posted 22 December 2011 - 10:42 PM

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.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #6Code Fox  GDNet+

Posted 23 December 2011 - 01:12 AM

Bounce back ?
When a collision happens, the enemies move in the exact opposite direction for 300 ticks ?

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.