# Create rotation matrix from 1 axis

## Recommended Posts

Hi Everyone, wondering if anyone has any insight to this problem. I have 2 points, p1 and p2. I calculate the vector from p2 - p1: z-axis = p2 - p1 I'm parsing an armature, and it only supplies points (offsets) to each bone. I calculate the distance as: distance = z-axis.magnitude() but Im wondering how I can construct a matrix so that it will rotate the object (in this case a cylinder from 0 to length on z axis) to the vector we got from p2 - p1 as the new z-axis. z-axis.normalize() if we calculate the y-axis from any perpendicular axis : y-axis = z-axis.cross(1,0,0) then calculate the x as : x-axis = y-axis.cross(z-axis) then recalculate the y axis : y-axis = x-axis.cross(z-axis) should the matrix as shown not give the correct rotation? [[x0], [y0], [z0] [x1], [y1], [z1] [x2], [y2], [z2]]

##### Share on other sites
With what you've posted, the x and y axes will most likely not be unit length, which means you won't have a pure rotation.

It should go something like this:
vector3 z = normalize(p2 - p1);vector3 y = normalize(cross(z, vector3(1, 0, 0)));vector3 x = cross(y, z);
Note that this may fail if the z axis is parallel or nearly parallel to the cardinal x axis. To work around this, instead of always crossing with the x axis, you can cross the z axis with the cardinal axis whose index corresponds to the element of the z axis with the least magnitude. (That may sound confusing, but basically it means that if |z.x| is <= |z.y| and |z.z|, you cross with the x axis; if |z.y| <= |z.x| and |z.z|, you cross with the y axis; and similarly for z.z.)

##### Share on other sites
Heya again,

Thanks for the reply. Yeah I knew I had to normalize. I think the problem was my library Im using in python. The Vector3.normalize() function didn't work in place so it was causing my problems :D

Mostly I was asking if the math made sense, and yes apparently it does! Hurrah!

Oh and PS I was already using the method you put at the end there and yes it makes perfect sense to me ;)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628346
• Total Posts
2982203

• 10
• 9
• 24
• 11
• 9