Sure. The basic idea is that the vector (B-A)t+A will be A when t=0, and B when t=1. So as t varies, the result varies linearly, and you get a line from A to B. I've also seen it written as Bt+A(1-t). Whichever you find clearer to understand/rederive works.
So yeah, when t>1 (and it's moving positively) you stop it moving and reverse the speed's direction. When t<0 (and it's moving negatively), you do the same thing.
For the speed, basically the reason Lerp (linear interpolation) like this isn't always the best choice is because t always varies from 0 to 1, even though the actual distance travelled by the result vector may be huge or tiny. So basically, when you're stepping t, you want it to take distance/speed seconds for t to increase by 1.
So you have distance/speed=totaltime. Since you want t to take totaltime seconds to get to 1, you increase it by timestep/totaltime each time step (timestep=dt in the above function), so that you say t+=dt*speed/distance.
Also: The
entire point of using vectors (IMO) is to make it so you don't have to deal with their individual components. The nice thing about the interpolation is that you don't have to deal with the components explicitly! Yes, the equation:
P=(B-A)*t+A
is equivalent to:
P.x=(B.x-A.x)*t+A.x
P.y=(B.y-A.y)*t+A.y
P.z=(B.z-A.z)*t+A.z
but being able to not write it out is great. It becomes really important once you have dot products and other weird things in there, because then it's all interdependent:
A+=((B-A)*X)*A (The first * is dot product, the second * multiplies the scalar (B-A)*X with the vector A)
is NOT equivalent to:
A.x+=((B.x-A.x)*X.x+(B.y-A.y)*X.y+(B.z-A.z)*X.z)*A.x
A.y+=((B.x-A.x)*X.x+(B.y-A.y)*X.y+(B.z-A.z)*X.z)*A.y
A.z+=((B.x-A.x)*X.x+(B.y-A.y)*X.y+(B.z-A.z)*X.z)*A.z
Instead you have to use temporary values:
tmpx=((B.x-A.x)*X.x+(B.y-A.y)*X.y+(B.z-A.z)*X.z)*A.x
tmpy=((B.x-A.x)*X.x+(B.y-A.y)*X.y+(B.z-A.z)*X.z)*A.y
A.z+=((B.x-A.x)*X.x+(B.y-A.y)*X.y+(B.z-A.z)*X.z)*A.z
A.x+=tmpx
A.y+=tmpy
Plus, it's an eyesore and harder to interpret than "A+=((B-A)*X)*A". None of this is really relevant though. I'm just kinda talking at this point. How's the weather?
I ordered some books three days ago. They're still on the other side of the country