0
$\begingroup$

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!

$\endgroup$

3 Answers 3

2
$\begingroup$

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} $$

$\endgroup$
1
$\begingroup$

A velocity is speed with direction. We find the direction by setting the (x,y) components of velocity to be proportional to $(x_2-x_1,y_2-y_1)$ and then the velocity will of course be found from these via Pythagoras, but notice: $$ x^2+y^2=z^2\implies (\sqrt{C}x)^2+(\sqrt{C} y)^2=(\sqrt{C}z)^2\\ \implies C\left(x^2+y^2\right)=C\left(z^2\right). $$ In particular, we can scale (x,y) by the square root of the amount we want to scale the speed by. So if you want to have a constant speed, you just normalise by dividing by this $z$ and then multiply by the square root of whatever speed you desire. Stay safe

$\endgroup$
1
  • $\begingroup$ Thank you! Super interesting to see how each answer tackles it. $\endgroup$ Apr 24, 2020 at 19:42
1
$\begingroup$

What i understand of your problem is that you want that the fireball you shoot gets to the position mouse points and that too moving with a constant speed. $ \\ $

If it is what you want then you can easily accomplish it, what you need to know using the given coordinates is the direction your straight line points to, then you can convert any speed to lie along the direction and have X and Y components correspondingly.

if $(x_1,y_1)$ and $(x_2,y_2)$ are the two end points of the straight line (beginning and ending respectively) and you have the distance between them as $d$ by pythgorean theorem

And you want your fireball to travel with speed c along the required line

then the X component of velocity would be $ c.\frac{x_2-x_1}{d} $ and Y component $c.\frac{y_2-y_1}{d} $

splitting the velocity vector along the line into components, using coordinates.

$\endgroup$
1
  • $\begingroup$ Thanks, this is a slightly different way to do it than the accepted answer because I mentioned I was already finding the distance via pythagorean separately. More importantly it's very readable. $\endgroup$ Apr 24, 2020 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .