How to convert an arbitary axis to a matrix?

Started by
2 comments, last by Zakwayda 18 years, 7 months ago
Hi, i'm using arbitary axises for the orientation of my objects, and I want to convert the arbitary axis to a matrix for drawing in direct3d. Does anyone know how I would begin to do this? Thanks.
Advertisement
If you're using DirectX, you can use D3DXMatrixRotationAxis(). If you have to construct the matrix yourself, a google for 'axis angle matrix' should turn up the info you need.

If by 'arbitrary axis' you mean an axis with the angle of rotation encoded in the length, you'll have to extract the angle first and normalize the axis. (I'm not sure whether the DirectX function requires the input axis to be unit length.) Note that at identity there is no axis to extract, so you'll have to handle that case. Lastly, you might consider a different fundamental representation for your object orientation that is a little easier to work with.
Thanks, I think I'll just use the d3dx function, I did find something on the dr math site.

I'm using arbitrary axises for orientation, so i don't get gimble lock, it seems the easiest way to do it so far.


I was kind of looking forward to using the quote below to add to my matrix functions, but I think i am already spending too much time on this :)

thanks alot.
Quote:
Converting a Vector to a Transformation Matrix

Date: 03/19/98 at 03:24:36
From: Harvey
Subject: Vector-angle to Matrix

If I have an arbitrary vector and an angle of rotation around that
vector, how can I convert that to a transformation matrix for a left-
handed coordinate system? Is there an easy/fast way?


Date: 03/19/98 at 14:34:30
From: Doctor Rob
Subject: Re: Vector-angle to Matrix

I would rotate the system of coordinates to make the z-axis the vector
of interest. Then I would convert to cylindrical coordinates, add the
angle in question to theta, convert back to rectangular coordinates,
and rotate back to the original coordinate system.

Let the vector be a = (a1,a2,a3) and the angle be alpha. Then let u be
the unit vector in the direction of a, so that:

u = (u1,u2,u3) = a/sqrt(a1^2 + a2^2 + a3^2).

Then let v be a unit vector perpendicular to u, such as:

v = (-a2,a1,0)/sqrt(a1^2+a2^2),

and w be a unit vector perpendicular to both, such as:

w = u X v (cross-product of u and v).

Then, to translate into the (u,v,w) coordinate system,

[u1 u2 u3][x] [x]
[v] = [v1 v2 v3][y] = U [y]
[w] [w1 w2 w3][z] [z].

Now:

theta = arctan(w/v),
r = sqrt[u^2 + v^2],
v = r*cos(theta),
w = r*sin(theta).

Now replace theta by theta - alpha to do the rotation.

u' = u,
r' = r,
theta' = theta - alpha.

Expand the sine and cosine using the addition formulas.

u' = u,

v' = r'*cos(theta'),
= r*cos(theta - alpha),
= r*cos(theta)*cos(alpha) + r*sin(theta)*sin(alpha),
= cos(alpha)*v + sin(alpha)*w.

w' = r'*sin(theta'),
= r*sin(theta - alpha),
= -r*cos(theta)*sin(alpha) + r*sin(theta)*cos(alpha),
= -sin(alpha)*v + cos(alpha)*w.

[u'] [1 0 0 ]
[v'] = [0 cos(alpha) sin(alpha)][v],
[w'] [0 -sin(alpha) cos(alpha)][w]


= R [v]
[w].

Now:

[x'] [u']
[y'] = U^(-1) [v'],
[z'] [w']


= U^(-1) R [v],
[w]

[x]
= U^(-1) R U [y].
[z]

This means that the rotation matrix is given by U^(-1) R U, where U
and R are defined above.

Is this a simple way? That's a matter of opinion.

-Doctor Rob, The Math Forum
Check out our web site! http://mathforum.org/dr.math/


Quote:I'm using arbitrary axises for orientation, so i don't get gimble lock, it seems the easiest way to do it so far.
Gimbal lock is only a problem with Euler angles. Neither matrices nor quaternions suffer from gimbal lock, and both are easier to work with than axis-angle form. So you might consider switching to one of those representations at some point.

This topic is closed to new replies.

Advertisement