I writing a simple XNA demo in which a sprite is supposed to move along a line (defined with two Vector2 points (Ax,Ay) and (Bx,By) ) at a given speed V (understood as a distance on the line traveled in a unit of time).
I understand that i’d need to calculate x and y position in a 2D space using equations similar to these:
dx = V*dt*cos(alpha)
dy = V*dt*sin(alpha)
but taking negative V into consideration (like when moving “backwards”)
But i wonder if i am not reinventing the wheel, maybe xna offers solution to that?
Create a 2D vector from point A to point B by subtracting A from B. Normalize it by dividing the X and Y conponents by the length of the vector. Multiply the X and Y components by the speed. Multiply this by the time slice (i.e. 20/1000 for 20 milliseconds). Add these X and Y values to your position.
Example: