Find the point that is $n$ units closer to point x

84 Views Asked by At

NOTE: Everything in computer graphics is on a 2d grid, which is the computer screen. However, unlike a standard 2d graph, The origin is the top left of the screen, and the y axis is flipped, like this: enter image description here

please keep this in mind when answering.

I am developing a game. Assuming the player is at (px, py), and the player is facing in the direction of
(fx, fy), what is a formula that I could use to move the player $n$ units closer to point (fx, fy)? The player should move along the line [(px, py), (fx, fy)]

3

There are 3 best solutions below

4
On BEST ANSWER

You can think of them as vectors, but basically, you can use linear interpolation:

Consider distance $d$ between the player’s position and the point it is facing.

$$d=\sqrt{(f_x-p_x)^2+(f_y-p_y)^2}$$

$$\left(p_x+\frac{n}{d}(f_x-p_x), p_y+\frac{n}{d}(f_y-p_y)\right)$$

So for the coordinate above, $f_x-p_x$ is the difference in the $x$ coordinate between the two points and $f_y-p_y$ is the difference in the $y$ coordinate between the two points. The fraction $\frac{n}{d}$ is the percent of the distance relative to the entire distance. For example, let's say the $x$ difference between the two points is $5$ and you want to move $2$ units. The fraction would be $\frac{2}{5}$. And all that's left is to add that change to the original.

0
On

The vector from P to F is simply F-P. So if you take D = $(f_x-p_x,f_y-p_y)$, then that's the direction the player should be traveling. The length of D, or |D|, is $\sqrt{(f_x-p_x)^2+(f_y-p_y)^2}$. So if we want to move X, we want to multiply D by some number so that its length is X: t|D| = X therefore t = X/|D|. So our movement is (X/|D|)D. The movement is the change in position, so we need to add that to our original position.

So:

 displacement = F-P
 distance = sqrt((f[0]-p[0])^2+(f[1]-p[1])^2)
 movement_factor = X/distance
 movement_vector = [movement_factor*displacement[0],movement_factor*displacement[1]]
 new_position = P+movement_vector

Adjust the code for the language you're using (Unity?). You likely can replace the distance calculation and/or multiplying a vector by a scalar with built-in functions. And be careful about dividing by zero, if the player is already at position F.

0
On

I would personally use vectors to resolve this issue. It can be solved by creating a unit vector from the player's position $[\,p_x, p_y\,]$ to $[\,f_x,f_y\,]$, then multiplying it by your distance, $X$.

First start by subtracting the two positions as vectors in the format b-a such that: $$ \vec{d}=\begin{bmatrix}f_x\\f_y\end{bmatrix}-\begin{bmatrix}p_x\\p_y\end{bmatrix}=\begin{bmatrix}f_x-p_x\\f_y-p_y\end{bmatrix} $$ then we normalize the vector by dividing each component of each vector by the vector's magnitude. $$ |\vec{d}|=\sqrt{(f_x-p_x)^2+(f_y-p_y)^2} $$ Which from this point I'll denote as $n$ to keep things more clear. Now we divide each component of the original vector by $n$ to get the unit vector. $$ \hat{d}=\begin{bmatrix}\frac{1}{n}(f_x-p_x)\\\frac{1}{n}(f_y-p_y)\end{bmatrix} $$ From here, we can simply multiply each component of the unit vector $\hat{d}$ by the distance $X$, because a unit vector (or normalized vector) has a magnitude of $1$. So your total displacement $s$ is: $$ \vec{s}=X\begin{bmatrix}\frac{1}{n}(f_x-p_x)\\\frac{1}{n}(f_y-p_y)\end{bmatrix}=\begin{bmatrix}\frac{X}{n}(f_x-p_x)\\\frac{X}{n}(f_y-p_y)\end{bmatrix} $$ Or if you'd like, $$ \begin{align*} \Delta x=\frac{X}{n}(f_x-p_x),\\ \Delta y=\frac{X}{n}(f_y-p_y) \end{align*} $$