Collision Detection for Entities Chasing the Player

Started by
4 comments, last by RLS0812 12 years, 3 months ago
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.
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...);
}
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

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

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.
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.
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)

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

This topic is closed to new replies.

Advertisement