189 views (last 30 days)

Hello everyone,

I would like to obtain the Euler angles needed to rotate a vector u = (0,0,1) to a vector v, defined between an arbitrary point (x,y,z) and the origin (0,0,0). So v = (x,y,z). I am currently working with a right-handed 3d coordinate system, where X is pointing to the right side of the current reader, Y is pointing upwards and therefore Z is pointing outside the screen of the computer.

I calculate the angles needed to rotate by taking as a reference axis Y -> X -> Z. I have tried to use Euler angles directly, but somehow the objects that I rotate end up in a different location that they should be. Searching on the internet, I have found loads of information about "rotation matrixes" and "quartenions", but I am lost about how to apply them to my case. Any help would be appreciated :)

EDIT: After some tinkering and searching on internet I managed to obtain an answer for a XYZ coordinated system. Still not useful for me, but maybe other people can profit from it.

a = [0 0 1].';

b = [0 5 0].';

% Method described in

% https://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d

a = a./norm(a);

b = b./norm(b);

v = cross(a,b);

vx = [0 -v(3) v(2) ; v(3) 0 -v(1); -v(2) v(1) 0 ];

c = dot(a,b);

I = eye(3);

R=I+vx+vx^2*(1/(1+c));

R = round(R,5);

% From Rot matrix to euler coordinates, follows XYZ, described in:

% http://www.gregslabaugh.net/publications/euler.pdf

if (R(3,1) ~=1) && (R(3,1) ~=-1)

theta_1 = -asin(R(3,1));

theta_2 = pi-theta_1;

chi_1 = atan2((R(3,2)/cos(theta_1)),(R(3,3)/cos(theta_1)));

chi_2 = atan2((R(3,2)/cos(theta_2)),(R(3,3)/cos(theta_2)));

phi_1 = atan2((R(2,1)/cos(theta_1)),(R(1,1)/cos(theta_1)));

phi_2 = atan2((R(2,1)/cos(theta_2)),(R(1,1)/cos(theta_2)));

theta = min(theta_1,theta_2);

chi = min(chi_1,chi_2);

phi = min(phi_1,phi_2);

else

phi = 0;

if R(3,1) == -1

theta = pi/2;

chi = phi+atan2(R(1,2),R(1,3));

else

theta = -pi/2;

chi = -phi+atan2(-R(1,2),-R(1,3));

end

end

theta = rad2deg(theta)

chi = rad2deg(chi)

phi = rad2deg(phi)

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 2 Comments

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/493771-euler-3d-rotation-between-two-vectors#comment_772612

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/493771-euler-3d-rotation-between-two-vectors#comment_772612

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/493771-euler-3d-rotation-between-two-vectors#comment_772623

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/493771-euler-3d-rotation-between-two-vectors#comment_772623

Sign in to comment.