c++ - How to find correct rotation from one vector to another? -
I have two objects, and each object has two vectors:
- Normal vector
- Vector up
Like this image:
The vector is vertical above normal vector. Now I have to find one rotation from one object to another, how to do it?
I have one way of finding rotation between another vector, and it works. The problem is that I need to take care of two vectors: normal vector and vector top. If I can move this vector from object to object from one object to another in general, the up vector may point out incorrectly, and they should be parallel to them.
Here is the code to find at least rotation:
Ge :: Quat GE :: Quat :: fromTo (Const Vector 3 and V1, Const Vector 3 And V2) {Vector 3A = Vector 3 :: Cross (V1, V2); Quat q; Float dot = vector 3 :: dot (v1, v2); If (dot> = 1) {q = quat (0,0,0,1); } And if (dot <-0.999 99) {vector 3 axis = vector 3 :: cross (vector 3 (1,0,0), v2); If (axis.length () == 0) // Choose one another if collinear axis = vector 3 :: cross (vector 3 (0,1,0), v2); Axis.normalize (); Q = quat :: axococcuit (axis, 180); } Else {float s = sqrt ((1 + dot) * 2); Boat invs = 1 / s; Vector 3C = Vector 3 :: Cross (V1, V2); Q.x = c.x * invs; Q.y = cyy * invs; Q.z = c.z * invs; Q.w = s * 0.5 f; } Q.normalize (); Return q; }
To find the correct rotation, what should I add / add to this code?
Before we start, I assume that both the UP vector and the normal vector Generalized and orthogonal (the dot product is zero) among them
We say that you have to combine with the rose (red?) Plate, its yellow plate Want to rotate? Therefore, our reference will be a vector from a yellow plate and we call our coordination system as XYZ, where Z - & gt; General yellow vector, w - & gt; Yellow vector and x - & gt; YxZ (cross product).
Similarly, for the rose plate, the rotated coordinate system will be called X-yy where 'z' - & gt; Normal rose vector, Y '- & gt; Up rose vector and x '- & gt; Y'xZ '(cross product).
It's ok to find rotation matrix, we just need to make sure that our normal yellow vectors will become normal rose vectors; That the yellow vector above us will be transformed into a rose vector, and so on, i.e .:
RyellowTOrose = | X'x Y'x Z'x | | X'y Y'y Z'y | | | Ax Yaz Zz |
In other words, after you have converted into a coordinate system of a generation system, any priorities change after applying this change, to combine it with the rose coordinate system
If your upper and normal vectors are not orthogonal, then you can easily correct one of them. Simply create cross product between normal and up (result in vector named C for convenience) and then up the cross product between C and Normal, to correct the vector.
Comments
Post a Comment