I'm creating a game where you, a little point, have to run away from other little points, and if they touch you, you die. Pretty standard game format, but I'm having a lot of trouble getting the little points to follow the player.
I've tried:
- if (x > targetX) x -= speed;
- if (x < targetX) x += speed;
- if (y > targetY) y -= speed;
- if (y < targetY) y += speed;
I then tried something like:
- x += (targetX - x) * speed;
- y += (targetY - y) * speed;
But then the speed of the particles is dependent on the distance between the particle and the player. When they're really far away, they're super fast, when they're really close, they're really slow. I need a constant speed.
Finally I tried trigonometry:
- xCalc = xTarget - x; //'bottom' of the triangle, adjacent to angle
- yCalc = yTarget - y; //'side' of the triangle, opposite angle
- angle = atan(yCalc/xCalc);
This works well along the x-axis, but not at all well along the y-axis. It also doesn't work from 3'o'clock to 6'o'clock, the bottom right quadrant - if the player is in this position compared to the particle, the particle ignores the player. I'm sure this last way is the right way to go but I can't get my head around the maths. Help?
1