how can I convert a triangle's degree into x and y vectors?

131 Views Asked by At

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.

3

There are 3 best solutions below

1
On BEST ANSWER

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.

2
On

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.

1
On

"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?