XNA line movement in 2D

I am writing a simple XNA demo in which it is assumed that a sprite moves along a line (defined by two points Vector2 (Ax, Ay) and (Bx, By)) with a given speed V (understood as the distance to the line passed through a unit of time). I understand that I will need to calculate the position of x and y in 2D space using equations like these:

dx = V*dt*cos(alpha)
dy = V*dt*sin(alpha)

but given the negative V (for example, when moving "backward") But I wonder if I’m inventing a wheel, maybe xna offers a solution?

+3
source share
1 answer

2D- A B, A B. , X Y . X Y . (.. 20/1000 20 ). X Y .

:

A is (2, 5)  B is (1, 8)
Speed is 2 (move 2 units in one second)
Time slice is 20 milliseconds since last update

C will be (-1, 3)
Normalize C by dividing by distance (sqrt(-1 * -1 + 3 * 3) = 3.1622777)
Normalized is (-0.316277, 0.9486833) (to move 1 unit that direction)
Multiply by speed (2): (-0.632554, 1.8973666)
For movement this time period, multiply by 20 and divide by 1000:
(-0.01265108, 0.037947332), move sprite this far this frame
+4

Source: https://habr.com/ru/post/1795594/


All Articles