# How do I linearly interpolate between two vectors?

I have a velocity vector where my client is at and where its going, and I have the same vector that comes from the server telling where the client should be. Sometimes its a bit different, so I want to interpolate between my current position to the server correct position. The black arrow is the client velocity vector, the red arrow is the client velocity vector on the server and the blue arrow is the one that I want to calculate and interpolate to.

How do I calculate the blue vector? Then, how can I linear interpolate between them?

Blue vector can be calculated easily: red - black (the sign between vectors is minus). But if you want just to interpolate between black and red vector, you don't have to calculate it. Linear interpolation is just linear combination. So you can just take: alpha * black + (1 - alpha) * red, where alpha has to be from interval <0,1>. If alpha will be 1, then you will get black vector, when alpha is 0, you will get red vector.

And if I understood it right, you will interpolate between these vectors in time. So just choose right increment of alpha in time.

Did I understand you right? Or did you meant something completelly diffent?

• Yes, you got me right. Just one more question, how do i multiply the number for the vector? Will that operation return me a new vector? For instance, red is a vector (0, 10) with length of 8 and black is (-2, 5) with length of 4. How do i do alpha * black or (1 - alpha) * red? Thank you. – gmemario Oct 17 '11 at 13:42
• You just multiply each vector coordinate separately. If alpha is for example 0.5, then alpha * black + (1 - alpha) * red = (0,5) + (-1, 2.5) = (-1, 7.5) - if you will draw this to some paper, you will see it's really exactly between those two vectors. – zacharmarz Oct 17 '11 at 13:55

Taking this picture: AB is the red vector from A to B.

Say P is 25% of the way from A to B. The basic way to get to P from the origin is

A + ( B - A ) / 4
= 3/4 A   +   B / 4


So 3/4 A and 1/4 B.

Another way to find that is saying you want a vector 75% "close" to A, and 25% "close" to B. (A vector that is 100% "close" to A is just the A vector.)

So you would also be able to find P as:

A*t + (1-t)*B


With t=0.75 (to say 75% "close" to A)

0.75A + 0.25B


So a simple LERP function in C for a Vector3f class would be:

static Vector3f lerp( const Vector3f& A, const Vector3f& B, float t ){
return A*t + B*(1.f-t) ;
}