I have this as my lerp function:
Vec2f lerp(float t, Vec2f a, Vec2f b){
return (1-t)*a + t*b;
}
And I have the following code below, which I was hoping that would result in a "tween":
Vec2f a(0,0);
Vec2f b(3,4);
Vec2f c(5,4);
Vec2f d(5,0);
if( i < 100 ){
if(i <= 30){
ball.pos = lerp(i, a, b);
}
else if(i > 30 && i < 80){
ball.pos = lerp(i, b, c);
}
else {
ball.pos = lerp(i, a, d);
}
} i += 1;
But what I get is a "discontinuous tween", that is instead of starting at the last point where the lerp from A to B ends, it starts somewhere else, and some goes for the other lerps. What am I doing wrong?
t
has to be between 0 and 1 in your interpolation function, but you are passing values between 0 and 100. Change your calls to lerp(i/100.0f, a, b)
and so on. (It is important that you specify the 100.0 as a floating point literal, NOT an integer literal!)
As DarenW correctly points out, you have to cover the range from 0 to 1 for every segment for the desired effect, i.e. in your case, lerp(i/30.0f, a, b)
, lerp((i-30)/50.0f, a, b)
, etc.