Unit vectors V and D lie on a 3D space. They have the same starting point. I want to rotate the vector V towards to vector D, but only by angle θ.
Given that we know:
We now want to calculate vector Z, which will also be a unit vector. Is it possible to calculate the coordinates of vector Z, with this information given above?
Do you have any ideas how this problem can be solved?
What you need to do is to define a third axis in the plane of the V and D vectors such that a 90 degree rotation from V points in this direction. I'll call the unit vector that points in this direction D'. With this, your Z vector is easy:
Z = cos(theta)*V + sin(theta)*D_tick;
So how to compute D'? That, too is easy. First compute a vector orthogonal to V and D using the cross product. Call this W: W = V×D. Next compute a vector orthogonal to W and V: D' = W×V = (V×D)×V. This points in the right direction, but it will only be a unit vector if your V and D are orthogonal. So normalize: D' = D'/||D'||, where ||D'|| is the magnitude of the vector D'. If you have a vector math package, you can do this via
D_tick = ((V.cross(D)).cross(V)).normalize();
One caveat: What if ||D'|| is zero? That happens if and only if your Δφ is a multiple of pi radians (or 180 degrees). Alternatively, it happens when V and D are parallel or anti-parallel to one another. Your question is ill posed in this special case. You should check for this special case.
Addendum
My (V×D)×V and comingstorm's D - V*((D·V)/(V·V)) are one and the same for vectors in three dimensional space. Because V is a unit vector, his D-V*((D·V)/(V·V)) reduces to D-V(D·V). My (V×D)×V is equal to D(V·V)-V(D·V) per the vector triple product identity (http://mathworld.wolfram.com/VectorTripleProduct.html), and this reduces to D-V(D·V), again because V is a unit vector.
One way to do this is to find the component of D
perpendicular to V
, scale it up to equivalent length, and do a vector sum using sin()
and cos()
:
D_perp = D - V * ((D . V)/(V . V))
D_perp_scaled = D_perp * (|V|/|D_perp|)
result = cos(theta) * V + sin(theta) * D_perp_scaled
This is well-defined unless D
is parallel to V
, which will make |D_perp| == 0
and cause problems with the division. This isn't really surprising: in that case your plane of rotation is ill-defined -- it isn't clear which direction you should rotate!
Mathematically, this method for finding the perpendicular is equivalent to the cross-product method cross(cross(V,D),V)
mentioned in other answers, but is perhaps a bit simpler, and works for any vector space, (e.g., 2-D and 4-D vectors, not just 3-D).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With