Finding out a rotation matrix between two vectors
Posted 16 June 2007 - 02:41 PM
What I am currently doing is to find out the angles (more accurately: the cosines of the angles) by projecting one of the vectors onto the yz-plane (which becomes vector a) and the xz-plane (vector b), respectively, and then find out the angles via:
cos(x-rotation) = (a * z) / (|a| * |z|)
cos(y-rotation) = (b * z) / (|b| * |z|)
This works quite well and I can use the resulting cosines in my rotation matrix (and the sines aren't difficult from here either), but I was wondering if there is a less computationally intensive procedure to manage this.
Posted 16 June 2007 - 10:18 PM
I guess you won't get any faster here, without using some less intuitive (and probably numerically quirky) method.
The common practice without resorting to projections would probably be to calculate the angle from the first vector to the second as in (v dot w) / (||v|| * ||w||), then build an axis perpendicular to both vectors as in (v cross w), then build a rotation matrix from the given axis and the given angle, which in total should boil down to nearly the same amount of operations you already have.
Generally, you should first see if a) optimization is required here and b) if there is a better algorithmic (i.e. higher level) optimization you can perform, before moving on to some sort of hack here.
Posted 17 June 2007 - 11:32 AM
As I recently found out, this also gives me a few other neat advantages. Since the y-component of the z-axis vector will always be zero, I don't even have to calculate a real dot product and can reduce the calculation even further.
I don't really understand your way of doing it, though I've seen it in a few of the papers I've read. I'll have a look into it once I am done with this thing.
Posted 18 October 2007 - 04:10 PM
I'm not at all sure what this method that has been proposed to me a few posts up really does and why it works, though I'm confident it does. The paper I'm reading only says the following when going to find the rotation between two vectors: "Next, a transformation defining the relation of the new axis to the other segment's axis must be found. This can be done using techniques described in standard graphics texts for rotating a given vector into another arbitrary vector. I.e., find a local coordinate system for which the vector is the z-axis by taking the cross-product of the vector with another non-parallel vector, and then taking the cross-product of the vector with this new vector. Normalize axes and use them as rows in a rotation matrix."
To me this seams like another way of doing it (somehow involving two cross products and no dot product). Right now, I have no idea how either of the two ways works, so I'd be happy about any help.
Posted 18 October 2007 - 04:14 PM
Posted 18 October 2007 - 08:22 PM
Posted 20 October 2007 - 01:10 PM
All of these are good proposals, so don't get me wrong now, since I'm still trying to understand this approach I posted. I found it via a Google search as well, so it must be working. I'm having a few difficulties understanding it, though.
First of all, here's what I know of how they do it in the paper: You take a vector you want to have a rotation matrix to in your current coordinate frame and assume that it is to be the z-axis of its new own coordinate system. Then, they build a cross product of this vector and ANY non-parallel vector (which is exactly the big deal for me). Then they take a cross product between the original vector and the new vector, normalize all of them and have the three unit vectors for the new coordinate frame. Then they simply enter them row-by-row into a matrix and they're done.
I have a fairly good geometrical understanding of this now. When doing the first cross product, what is simply done is that on any arbitrary plane on which the original vector is located, another vector is constructed. This is then orthogonal to the original vector. The arbitrary non-parallel vector is just to make this plane. Then, they can just calculate the third axis by making a cross product of the other two.
But here's the big question, or rather, the problem: If you take a different arbitrary non-parallel vector, of course all the axes are different, and the entire matrix will be a completely different one. In the paper, this goes uncommented and seems to be alright. But what good is this, if you, using this method, could possibly construct any number of different transformations? I mean, you want to have it definite, don't you?
I find this very illogical at this point, so I'd be happy to be told what kind of fallacy I'm making here.
Posted 20 October 2007 - 03:07 PM
Posted 20 October 2007 - 03:09 PM
Posted 22 October 2007 - 07:17 PM
All these different ways usually boil down to the same number of operations in the end anyway.
Posted 22 October 2007 - 08:58 PM
quaternions: 25 muls, 20 adds, 1 div
cross-product: 18 muls, 8 adds, 1 div, 1 sqrt
Also, quaternions don't have unstability problem when both axis are pointing to the same direction, though they have unstability like cross-product when the axes point to opposite directions. You might get away of that with quaternions though by doing d*=1.0001f; ;)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users