0

Possible Duplicate:
Finding Signed Angle Between Vectors

I'm in need of help with a little math issue.

So, I got a vector v, representing an orientation, and two points s and t. What I what to do, is to find the rotation to apply to my vector v, in order to make it parallel with the vector defined by the two given points.

currently I'm somewhat achieving this, that is, I'm able to find the angle, just not the way to apply it (clockwise or counter clockwise).

Currently I'm just calculating the acos to the dot product of the vectors.

Any input is welcome.

Community
  • 1
  • 1
Skeen
  • 4,614
  • 5
  • 41
  • 67

1 Answers1

3

Let's say acos gives you a value between 0 and pi.

Let's also say the vector from s to t is called u. As you have already computed,

acos((v . u)/(|v| * |u|))

gives you an angle alpha. Now in truth, v could be u rotated by alpha to one or the other direction.

You probably need this in 2D, but I'll go on in 3D first.

The rotation should be around a vector that is perpendicular to both v and u. This vector is of course the cross product of the two: u x v

Let's see an example:

   / v
  /
 /\  alpha
/  )
------------ u

In this case, u x v gives a vector towards the outside of your monitor. At the same time, you can see that the ration alpha should take place counterclockwise to make v parallel to u.

That is, in 3D, you have to compute w = u x v and always rotate v by alpha counterclockwise with respect to w. Alternatively, you can rotate v by alpha clockwise with respect to -w (which is v x u).

In 2D, I assume you want to rotate around z and you don't know which direction. You can apply the same method as above:

  • Compute w = u x v
  • If w has positive z (the x and y will be zero)
    • then, v should be rotated counterclockwise.
    • else, v should be rotated clockwise.
Shahbaz
  • 46,337
  • 19
  • 116
  • 182