Vanguard Vanguard - 1 year ago 66
C# Question

Attack/Follow my player

I want to create a game where you try to avoid objects(circles) that are "attacking" you. The way I did this:

  • First calculate the x_and y difference between the object and me (in this code AI_x is the object's x-location (AI_y: y-location) and x, y the player's location)

  • Calculate the angle the object has to make in order to go straight towards the player (with Math.Atan(difference between x-axis' / difference between y-axis'))
    -I then use these angles to calculate the x and y distance it has to move per tick (For Example distancePerTick_x = Math.Cos(angle) * speed)

However it doesn't work when my distanceToPlayer_x (see code) is positive or both distanceToPlayer_x and distanceToPlayer_y are negative so the devided number becomes positive again.

I know why this occurs, but I have tried 2 hours to solve it but can't handle the pain anymore :P.

I hope you guys can help me.


namespace AI
public partial class mainClass : Form
int health;
double startPosition_x, startPosition_y, angle;
float x, y, speed, AI_x, AI_y, distanceToPlayer_x, distanceToPlayer_y;

Timer gameLoop;

Bitmap bmp;

//player color
Pen playerColor;

Random random_x, random_y;

public mainClass()

//on startup do this:
private void Initialization()
//initializing variables
health = 100;
speed = (float) 0.75;

startPosition_x = pbField.Width / 2;
startPosition_y = pbField.Height / 2;

//set x and y value to start position
x = (float) startPosition_x;
y = (float)startPosition_y;

random_x = new Random();
random_y = new Random();

AI_x = 0;//random_x.Next(1, 550);
AI_y = 0;//random_y.Next(1, 320);
//player color
playerColor = new Pen(Color.Red, 5);

gameLoop = new Timer();
gameLoop.Tick += new EventHandler(gameMethode);
gameLoop.Interval = 5;

//the gameloop
private void gameMethode(object sender, EventArgs e)
bmp = new Bitmap(550, 320);
pbField.Image = bmp;

using (Graphics g = Graphics.FromImage(bmp))
//key manager
if (InputManager.pressed(Keys.W))
y -= speed;
else if (InputManager.pressed(Keys.S))
y += speed;
else if (InputManager.pressed(Keys.A))
x -= speed;
else if (InputManager.pressed(Keys.D))
x += speed;

g.DrawEllipse(playerColor, AI_x, AI_y, 5, 5);
g.DrawEllipse(playerColor, x, y, 5, 5);


//attack player method
private void attackPlayer()
//the distance to the player
distanceToPlayer_x = AI_x - x;
distanceToPlayer_y = AI_y - y;

angle = Math.Atan(distanceToPlayer_y / distanceToPlayer_x);

Console.WriteLine("Radiants: " + angle);
Console.WriteLine("Degrees: "+ angle * (180 / Math.PI));

AI_x += (float) Math.Cos(angle) * (speed/(float)1.25);
AI_y += (float) Math.Sin(angle) * (speed/(float)1.25);

//if keydown
private void mainClass_KeyDown(object sender, KeyEventArgs e)
//if escape button is pressed
if (e.KeyCode == Keys.Escape)

InputManager.changeState(e.KeyCode, true);

//if keyup
private void mainClass_KeyUp(object sender, KeyEventArgs e)
InputManager.changeState(e.KeyCode, false);


Answer Source

When computing the entire range [0..2 * pi] angle, the standard pattern involves Atan2 method:

 angle = Math.Atan2(distanceToPlayer_y, distanceToPlayer_x);

see for details

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download