Lerp for "tween"

I have it as a 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 this would lead to "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 I get an "intermittent twin", that is, instead of starting from the last point where lerp ends from A to B, it starts somewhere else, and some go for other lerps. What am I doing wrong?

+6
source share
1 answer

t should be between 0 and 1 in your interpolation function, but you pass values ​​from 0 to 100. Change your calls to lerp(i/100.0f, a, b) and so on. (It is important that you specify 100.0 as a floating point literal, not an integer literal!)

As DarenW correctly points out, you should cover the range from 0 to 1 for each segment for the desired effect, i.e. in your case lerp(i/30.0f, a, b) , lerp((i-30)/50.0f, a, b) etc.

+4
source

All Articles