How to determine rotation angles given an axial vector

Started by
6 comments, last by Galaximo 17 years, 5 months ago
I have an axial vector, (x,y,z), that differs from the vector (0,1,0) by some rotation. How can I determine the rotation in yaw-pitch-roll or in x-y-z-rotation? Here is a simple example:


(0,1,0)
/\  (0.3,0.75,0)
|   /\  
|   /
|  /
| /
|/


In Yaw-Pitch-Roll: Yaw: -90 deg Pitch: -35 deg Roll: 0 deg In XYZ Rotation: x-axis: 0 deg y-axis: 0 deg z-axis: -35 deg
Advertisement
A Cartesian-to-spherical coordinate conversion will give you two of the angles you're looking for (there isn't enough information present to compute the third angle).

Out of curiosity, what's the context? What do you need the angles for?
I am making a 3D space shooter. When your ship is hit by lasers I want the shields to 'flash' in the position the shields were hit. This requires rotating the flash animation to be where it was hit.

So you are saying this cannot be done with the information given? What other information would I need?
Quote:Original post by Galaximo
I am making a 3D space shooter. When your ship is hit by lasers I want the shields to 'flash' in the position the shields were hit. This requires rotating the flash animation to be where it was hit.

So you are saying this cannot be done with the information given?
Consider that the impact graphic can be 'spun' around the source vector while still remaining perpendicular to it. In other words, there is an infinite number of orientations that meet the given requirements.

In any case, I wouldn't approach this problem in terms of Euler or spherical angles, but rather in terms of constructing an appropriate basis for the animation directly from its position relative to the spaceship (this would be the 'axial' vector you mention, although I'm not sure what 'axial' is intended to mean in this context).

To construct the basis, you start with the (normalized) source vector as the first basis vector. A stable way to compute the second is to cross the source vector with the cardinal axis with which it is least aligned. The third basis vector is then the cross product of the first two.

Post back if you need any further clarification.
Ah, yes that makes sense, it can be rotated around the axis. I'm not worried about it's roll rotation so that can be ignored.

I see where you are going with the basis vectors but I'm not sure if that will help me to my goal. For the impact graphic I can't simply set its basis and draw it based on the new basis. The graphic is built on a basis that cannot be changed, however I can rotate the graphic when I render it into the world. So what I do is I know the basis that it is using, then rotate accordingly.

I think you were close with the spherical co-ordinates. I looked it up and found this:

angle-theta (yaw) = atan( x / z )
angle-phi (pitch) = acos( y )
Disregarding roll, as mentioned.

It appears to work as long as the Z co-ordinate is positive. As soon as it is negative the impact graphic goes behind the ship instead of infront of it.
Try using atan2(x,y) instead of atan(x/y) to get angles for all quadrants of the circle.
Yup, using atan2() for computing the spherical angles will most likely solve the problems you're running into (assuming you have an 'atan2' function available).

As for the basis method I mentioned, I don't know what API you're using, but e.g. in OpenGL you could construct the basis manually and submit it via glMultMatrix*().

Personally I would use the basis method if possible, as it's completely stable (whereas the spherical coordinate method has some failure cases that you have to work around).
ATan2 works perfectly. Thanks a lot for the quick feedback guys, very helpful. These forums never seize to amaze me.

I will look into the basis method, thanks for informing me on it.
I'm using DirectX. I havn't seen anything on it, what is the command in OpenGL to create a rotation from a basis?

Cheers,
Galax

This topic is closed to new replies.

Advertisement