1
$\begingroup$

I am programming a game for practice and I am trying to design a algorithm that finds the x and y vectors that the "bot" will need to follow in order to reach the players exact x and y coordinates. I already used Pythagorean's theorem to find the distance between the bot's coordinates and the player's coordinates. So from here I could easily find all the degrees of the triangle but I have no clue how to convert that into x and y vectors.

as an example if I set the bot to move at vector x = -5 then it would move -5 pixels (left) across the screen every time the monitor refreshes.

I know all x and y positions of the entities at all times.

So is there anyway to just take the degree from one corner of the triangle and convert it into vectors that the bot can move on?

Thanks.

  • 0
    If I understand you correctly you want $\tan \theta =\frac xy$ and $\theta = \arctan \frac xy$?2017-02-16

3 Answers 3

0

"So is there anyway to just take the degree from one corner of the triangle and convert it into vectors that the bot can move on? "

Angle = $\theta$. Total distance = $d$

Then Vector = $(x,y) = (d*\sin \theta, d*\cos \theta)$.

Is that what you were asking?

  • 0
    That probably could have probably worked also, but another answer showed a way to do it without even using angles. Thank you though!2017-02-16
0

I coded something like that once, i think that there is not way to make that bot move with an angle, what i did was check the x and y position difference between the bot and the player... and then at every time that the monitor refreshes the bot will move by certain amount of pixels to the left/right or up/down trying to reach the player position... the issue there was that sometimes the bot only get close ( sqrt(2) ) to the player but it doesn't get to the same position.

  • 0
    Hmm I do not believe that there is any method for doing this in the programming language I am using (vanilla javascript). I think the only way is for me to figure out how many pixels to move the bot up/down manually using math, but I could be wrong.2017-02-16
  • 0
    setInterval(function() { if (player.x > bot.x + bot.range) { bot.x += bot.mov_speed; } else if(player.x < bot.x - bot.range) { bot.x -= bot.mov_speed; } if (player.y > bot.y + bot.range) { bot.y += bot.mov_speed } else if(player.y < bot.y - bot.range) { bot.y -= bot.mov_speed; } console.log(bot.name+" ("+bot.x+"; "+bot.y+")"); },1000); I used this to make the bot go were the player is2017-02-16
0

You don't need angles.

If the robot is at $(x_r, y_r)$ and the player is at $(x_p, y_p),$ a vector that goes from the robot's position to the player's position is $(x_{rp}, y_{rp})$ where \begin{align} x_{rp} &= x_p - x_r, \\ y_{rp} &= y_p - y_r. \\ \end{align}

But this has the robot going to the player's position in one step. You will probably want to make the robot travel slower than that, so multiply both of the coordinates of your vector by some small number; for example, if you multiply by $0.05$ (which equals $1/20$) then it will take $20$ steps for the robot to reach the player.

  • 0
    bot.vx = (-xdifference * 0.1); bot.vy = (-ydifference * 0.1); I ended up making them negative because otherwise my player would repel the bot like a magnet but this works like a charm! wow thank you!2017-02-16