How to get the direction of movement?

366 Views Asked by At

I am trying to get the general direction of a moving object. So far my idea is very simple:

degree = (y_new - y_old) / (x_new - x_old)

where y_old and x_old both refer to the x,y coordinates of the center of the object from for example 5 frames before. But this doesn't work and it gives a lot of nan or 0 as the direction of movement. How can I get the degree of the movement vector properly?

1

There are 1 best solutions below

0
On BEST ANSWER

$$\theta=\text{atan}_2({y_{n}-y_{o}},{x_{n}-x_{o}})$$

The function $\text{atan}_2$ is found in most numerical libraries, and returns radians. See its definition on Wikipedia.