Calculate direction angle from two vectors?

ingh.am picture ingh.am · Apr 3, 2010 · Viewed 27.3k times · Source

Say I have two 2D vectors, one for an objects current position and one for that objects previous position. How can I work out the angular direction of travel?

This image might help understand what I'm after:

(image) http://files.me.com/james.ingham/crcvmy

Answer

Rob Lachlan picture Rob Lachlan · Apr 3, 2010

The direction vector of travel will be the difference of the two position vectors,

d = (x1, y1) - (x, y) = (x1 - x, y1 - y)

Now when you ask for the direction angle, that depends what direction you want to measure the angle against. Is it against the x axis? Go with Radu's answer. Against an arbitrary vector? See justjeff's answer.

Edit: To get the angle against the y-axis:

tan (theta) = (x1 -x)/(y1 - y)          

the tangent of the angle is the ratio of the x-coordinate of the difference vector to the y-coordinate of the difference vector.

So

theta = arctan[(x1 - x)/(y1 - y)]

Where arctan means inverse tangent. Not to be confused with the reciprocal of the tangent, which many people do, since they're both frequently denoted tan^-1. And make sure you know whether you're working in degrees or radians.