# Pointing a quaternion

This topic is 4482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi folks, Rotating quaternions is all fine and dandy, but what if you want to determine the quaternion that *points at* one vector location from another? With Euler Angles one just does some trig, but I can't seem to find any info on the net about doing it with quats :( Any help greatly appreciated. Anthony

##### Share on other sites
Quote:
 Original post by Prospero999Hi folks,Rotating quaternions is all fine and dandy, but what if you want to determine the quaternion that *points at* one vector location from another?With Euler Angles one just does some trig, but I can't seem to find any info on the net about doing it with quats :(
Keep in mind that there's not actually a unique solution to the 'aim at' problem. By the Euler angle solution I assume you mean in terms of yaw and pitch, basically a Cartesian-to-spherical conversion. This works, but will fail when the target lies along the up axis, as will the algorithm used by the standard 'lookat' function. Another option is to construct an orthonormal basis from scratch; this is always guaranteed to produce a valid basis, but can exhibit 'popping' when moving from one basis to another. Finally, if you have an orientation that is being maintained incrementally, you can apply a rotation that will align it with a target. This can be done as many times as you need and will always succeed, although it won't preserve any particular concept of 'up'.

I realize that wasn't really your question, but I thought it might be useful to clarify some of the potential issues with various 'aim at' algorithms. As for applying these algorithms to a quaternion, there may be some shortcuts, but I'm not aware of them and haven't tried to figure them out myself. Conversions are fairly cheap though, so I'd just use the method of your choice and then convert to a quat as necessary. For example, with the Euler angle solution (which is well suited for, say, a gun turret), just compute the Euler angles as you normally would and then use them to construct a quaternion. For the lookat method, you would construct the 3x3 rotation matrix and then extract the quaternion from it.

Perhaps someone else can point to some nice shortcuts for constructing 'aim at' quaternions directly, but converting from another representation should work fine. Also, if you find yourself (for example) converting from Euler angles to a quat, and then from the quat to a matrix, you might consider whether the quat is actually serving a purpose, or is just an unnecessary intermediate step.

##### Share on other sites
If v2 is the vector that you want to coincide with v1, then the quaternion you are looking for is:
q = |v1|*|v2|*( cosφ + sinφ*n), where φ = acos( (v1 dot v2)/(|v1|*|v2|) ) and n = (v1 X v2)/|v1 X v2|. The scalar component is |v1|*|v2|*cosφ, and the rest, |v1|*|v2|*sinφ*n, should be "distributed" component-wise to the quaternion's imaginary part.
Post-multiplying the pure quaternion (0, {v2}) by q, as in q*{0, {v2}} will result to the pure quaternion (0, {v1}).

However if you want to use this quaternion as an orientation, there will a problem. Although "q*(0,{v2})" will yield a vector ((0, {v1}), a pure quaternion) this will not hold for arbitrary pure quaternions. As you probably know, in order for a pure quaternion to yield another pure quaternion, it must be multiplied by both another quaternion and its reciprocal. (For the special case of unit quaternions, this represents rotation)
I'm not sure what you want to do exactly, so I can't suggest anything...

It's funny you should ask though, because this is what quaternions were conceived as: as a factor, which when operated upon a given vector, it would make it coincide (both in length and direction) with another given vector.
The part of the quaternion, responsible for the change in length was what Hamilton called "a tensor", the scalar part, (since it caused stretching afterall), and the part responsible for the rotation was the "versor", the imaginary part.

Any quaternion q is merely the quotient u/v of two vectors u and v, and when it operates on the "denominator" v, it causes it to coincide with the "nominator" u. This can only be written as: q*v == u, because the notation u/v is generally wrong, since it can be interpreted both as u*v-1 and v-1*u, which are only equal for coplanar vectors. Also note that this multiplication has nothing to do with the usual inner and outer product (dot and cross). In fact, it is: u*v = (u dot v) - (u cross v) !!! (a scalar "plus" a vector! hmm...) = |u|*|v|*cosθ - |u|*|v|*sinθ*n, where θ is their angle: acos((u dot v)/(|u|*|v|)) , and n is a unit vector normal to both u,v; thus: n = (u X v)/|u X v|.

If you want to know more about quaternions and what they were supposed to do in the first place, (and no, it's not "avoid gimbal lock!" :) ) I suggest this excellent book -"Elements of quaternions"- by some old math professor (it was written around 1880!) It's not the original "Elements of quaternions" by Hamilton, but it's quite good.
Start from page 32, you'll only need the next 10-15 pages, and they are very important!
You shouldn't have trouble applying these in your program...

##### Share on other sites
Many thanks chaps.

I'm not sure though whether I'm less or more confused now ;)

Just a word on exactly what I'm trying to do, if there's any other advice you can give me:

1) Simulate pointing (and tracking) a directional antenna at a mobile vehicle (aircraft).

2) Simulate pointing a directional antenna on the aircraft at the ground station antenna.

Cheers,

Anthony

##### Share on other sites
Quote:
 Original post by Prospero999Many thanks chaps.I'm not sure though whether I'm less or more confused now ;)Just a word on exactly what I'm trying to do, if there's any other advice you can give me:1) Simulate pointing (and tracking) a directional antenna at a mobile vehicle (aircraft).2) Simulate pointing a directional antenna on the aircraft at the ground station antenna.Cheers,Anthony
I think you probably already have the answer with your original Euler angles solution, which seems perfectly appropriate for what you describe. If you need a quaternion for some reason, just construct it from the Euler angles.

##### Share on other sites
You definitely don't need quaternions for that. In such cases it easier to use a "look at" matrix.
If you don't have a MatrixLookAt() function, the following scheme can be used to derive it:
The goal is to define an orthonormal base of 3 vectors, that will represent the local axes of the object in its new orientation...
Assuming that the axis of the radar's antenna is Z, you obviously know the direction it should have: it will be the normalized position vector of the target, wrt the radar.
Given the local Z axis and the "global up" vector (usually {0,1,0}), you can find the radar's local X axis and then its local Y as the cross product of the first two. I can't be more specific since I'll have to know the handedness of your coordinates system. (let me know if you need help with this)

Normalize all axes, and apply the matrix:
[ sX*X.x   sY*Y.x   sZ*Z.x   P.x ][ sX*X.y   sY*Y.y   sZ*Z.y   P.y ][ sX*X.z   sY*Y.z   sZ*Z.z   P.z ][   0        0        0        1 ]

as the world matrix of your model. sX, sY, sZ are scaling coefficients for each axis (if needed) and P is the vector representing the position of the model.
If you use DX (or another API that transforms vectors as rows instead of columns, use the transpose matrix)

I believe that quaternions are a little tricky to use for this problem, and I'll explain what I mean... Despite the general opinion that they can represent orientations where other means fail, and although -as I showed in my previous post- it is trivial to create a unique quaternion that points one *vector* to another, one usually forgets that the problem is to actually find a quaternion that points one *set* of vectors to another. (I mean of course the regular base ({1,0,0}, {0,1,0}, {0,0,1}), to another prefined base {v1,v2,v3})

There are a few problems with that:
First, the formulas for such a quaternion are not trivial to derive.
(Personally I tried once, and as soon as I saw where it was going, I changed my mind)
The fact that I haven't even seen this problem adressed anywhere, makes me think that it's not trivial to handle, to the extent I thought myself.

Also, even if you had the formulae for such a quaternion, the new base that you wish to switch to, is not entirely known. Most "look at" problems only give you an initial vector and a target vector, asking you to come up with an orientation that points the first to the second.
What about the other two vectors that are needed to define an orthogonal base?
You would obviously need to take a random vector, project it on the plane normal to the target vector, normalize it, and find another one, perpendicular to it. Then you *would* have a base; a random one, but yet an orthonormal base.

As jyk already pointed out in his previous post, such an orientation is not unique. These two extra vectors need only be unit, normal, and lie on a plane normal to the target vector. Clearly there are infinite vectors with these properties.

I hope I didn't confuse you more (once more!...), I just wanted to point out that, quaternions are ok, but they don't just do magic on their own. A problem has to be well defined, for quaternions to provide a concise solution... And certainly, achieving the desired, up-right orientation with just two vectors is not a well defined problem.

edit:
I totally forgot to mention:
The "look at" matrix approach is not free of bugs either. Because of the way cartesian coordinates are expressed, there is an issue when trying to point the local Z axis straight up. The axis becomes colinear to the "global up" vector and this makes it harder for the cross product to define a unique direction normal to them both. Similarly, you could have used the "global right" vector to derive the matrix, but then again, the same issue would arise when trying to align the radar with (1,0,0)!
Coming to think of it, since a radar only needs 2 rotational degrees of freedom, why don't you use spherical coordinates instead?
You can find the normalized position vector of the target object from the radar in cartesian coordinates, express it in polar angles (longitude, latitude), and use them to orientate the model

##### Share on other sites
Quote:
 Original post by someusernameComing to think of it, since a radar only needs 2 rotational degrees of freedom, why don't you use spherical coordinates instead?
I agree with this - it's actually what I was getting at in my last post about the Euler angles solution (i.e. yaw and pitch). It should work just fine for a radar-like object. The only degeneracy is the straight-up-or-down case, which you (the OP) might check for just to be safe.

##### Share on other sites
I had read the specific part of your post jyk, but I hadn't understood that only two of the Y/P/R angles were supposed to be used, until a while ago!
I guess I need some sleep! :)

##### Share on other sites
Quote:
 Original post by someusernameI had read the specific part of your post jyk, but I hadn't understood that only two of the Y/P/R angles were supposed to be used, until a while ago!
Well, I didn't make it particularly clear :)

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 10
• 11
• 37
• 12
• 12
• ### Forum Statistics

• Total Topics
631414
• Total Posts
2999958
×