Homing/Chasing Algorithm.
I’ve been working on this algorithm, for my game.
the only chase i know is the standard Follow the X/Y Coordinates.
Enemy = Chaser
Hero = Being Chased
both Enemy and Hero move at the same time.
the problem is that Enemy the will go in straight line but eventually will not go directly to the Hero. i think its because of the constant value that i use,
Ex:
x = x - 5; or y = y + 5;
how do i determine what value i Add/subtract to make the Enemy chase directly to my hero?
is there a Formula to do that? or conditions?
Thank you.
If there are post that are the same as this, please direct me there. Thank you.
If your Hero is at, say, (99,50), and your Enemy is at (102, 55), it will move with 5 complete pixels, so it will end up at (97, 50), at the other side of your hero.
To solve this, you can check the difference between Hero.X and Enemy.X before the move. If the differency is less then the step you’re about to take (5), don’t move the whole 5 pixels, but move only that difference. Repeat that logic for Y.
I’m not sure what exact algorithm you’re using, but it seems to me that using this logic, the enemy can move Sqrt(2) faster when they move diagonally. Maybe you should consider using directions (angles), combined with a speed. It will be a little harder, using a little goniometry, but movement is more realistic.
You can take it a step further and try to predict at which point the Hero might be at a certain point in time, so the Enemy doesn’t move to the point where the Hero is now, but tries to cut the corner a little.