How do you find the X and Y velocities required to draw a straight line from one point to another at a desired speed

geometry

I'll try to break this up in case the title is too much to digest.

  1. Given two sets of coordinates, how do you find the required velocities of X and Y for one point that will draw a straight line to the other point? I believe this is simple subtraction of the X's and Y's, which gives you the velocities needed to move X1 and Y1 to X2 and Y2. From there, you can find the length of the straight line traveled with Pythagorean's theorem.

  2. More importantly, how do you find the velocities if there is a desired speed across the path of the straight line? The coordinates should be able to change randomly and the speed should stay constant. For the sake of this discussion, let's say the speed should be a length of 6 per one tick of time.

I'm trying to solve this to modify a 2d video game to make the player "shoot" a fireball to where the mouse is pointing. Due to limited functions provided, I need to make this "shoot" functionality myself. The function I am able to leverage only accepts one set of coordinates and velocities for X and Y.

I have #1 down above, but using this function means the farther my mouse is away, the faster the fireball travels. After all, (5,5) and (10,10) shoot at the same angle, but (10,10) travels twice as fast since we are setting velocity here. I need to normalize this across all possible values but I don't know how.

Edit: For others with this same question related to programming, save some time and see if your language has a Vector2.Normalize function as it does the same!

Best Answer

Let ${\bf D}=(x_2,y_2)-(x_1,y_1)=(x_2-x_1,y_2-y_1)$ be the direction vector pointing from $(x_1,y_1)$ to $(x_2,y_2)$.

You want a velocity vector ${\bf v}$ with magnitude $c$ in the direction of (ie: parallel to) ${\bf D}$.

Simply decompose the total speed $c=\|{\bf v}\|$ into vertical and horizontal components: $$ v_x= \|{\bf v}\|\cos\theta = c\cos\theta \qquad\text{and}\qquad v_y=\|{\bf v}\|\sin\theta = c\sin\theta $$ where $\theta$ is the angle ${\bf D}$ makes with a line parallel to the positive $x$-axis. If you draw a triangle with base parallel to the $x$-axis, height parallel to $y$-axis, and taking the hypotenuse as the line connecting the points $(x_1,y_1)$ and $(x_2,y_2)$, you will easily see using Pythagoras' Theorem and the definition of $\sin$ and $\cos$ that $$ \cos\theta = \frac{x_2-x_1}{\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}} \qquad\text{and}\qquad \sin\theta = \frac{y_2-y_1}{\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}} $$

So, $$ v_x=\frac{c(x_2-x_1)}{\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}} \qquad\text{and}\qquad v_y=\frac{c(y_2-y_1)}{\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}} $$

It is easy to verify that the velocity vector defined by ${\bf v}=(v_x,v_y)$ has magnitude $c$ and is parallel to ${\bf D}$: you simply compute $\|{\bf v}\|=\sqrt{v_x^2+v_y^2}=\sqrt{c^2\cos^2\theta+c^2\sin^2\theta}=c$ and you notice that ${\bf v}$ is just a scalar multiple of ${\bf D}$ so is parallel to ${\bf D}$: $$ {\bf v} = \left(\frac{c}{\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}}\right){\bf D} $$