0
$\begingroup$

A velocity encompasses both speed and direction in a single vector. I'm a little bit confused about how to separate the two.

I have 2 creatures. The first is located at position (x1, y1). The second is located at (x2, y2).

I would like the first creature to move towards the second creature, so I get the vector from creature1 to creature2 as so:

velocity = (x2 - x1, y2 - y1) 

Then I normalize the vector using the distance between the 2 points like so:

velocity.x = velocity.x / distance; velocity.y = velocity.y / distance; 

If I use this as my velocity, the creature will move in the correct direction but it will be moving too fast. How can I control the speed of the creature without changing the direction? I would like for creature1 to move in the direction of creature2 at a constant speed which I choose.

  • 0
    Thanks, David. That information combined with$a$typo fix solved my issue.2012-05-07

2 Answers 2

0

What you have written is not strictly speaking the velocity vector, but rather the displacement vector: $d=(x_2-x_1, y_2-y_2)$. If you normalize this, though, you'll get a unit vector in the same direction as the velocity vector:

$\ \ \ v=(v_1,v_2 )$ where $v_1={x_2-x_1\over\sqrt{(x_2-x_1)^2+(y_2-y_1)^2} }$ and $v_2={y_2-y_1\over\sqrt{(x_2-x_1)^2+(y_2-y_1)^2} }$.

You can impose any speed you wish by multiplying $v$ by a positive constant: $v_a=(av_1,av_2)$ will still have the direction of $v$, but the speed now is $a$.

0

In one dimension, the average velocity as an idealized object (or idealized reference point on an object) moves from $x_1$ to $x_2$ between timepoints $t_1$ and $t_2$ is $ v=\frac{\Delta x}{\Delta t}=\frac{x_2-x_1}{t_2-t_1} \,. $ You just need to generalize this to two dimensions: $ v=\frac{\sqrt{\Delta x^2+\Delta y^2}}{\Delta t} =\frac{\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}}{t_2-t_1} $ As a vector, you could write $\vec{x_1}=(x_1,y_1)$ $\vec{x_2}=(x_2,y_2)$ $\vec{\Delta x}=\vec{x_2}-\vec{x_1}=(x_2-x_1,y_2-y_1)$ and the average velocity vector would be the componentwise one-dimensional formula we started with above: $ \vec{v}=\frac{\vec{\Delta x}}{\Delta t} =\left(\frac{\Delta x}{\Delta t},\frac{\Delta y}{\Delta t}\right) $ The average speed would be the norm of this vector: $ |\vec{v}|=\frac{|\vec{\Delta x}|}{|\Delta t|} =\left|\left(\frac{\Delta x}{\Delta t},\frac{\Delta y}{\Delta t}\right)\right| =\sqrt{ \left(\frac{\Delta x}{\Delta t}\right)^2 +\left(\frac{\Delta x}{\Delta t}\right)^2} =\frac{\sqrt{\Delta x^2+\Delta y^2}}{|\Delta t|} $