# Quaternion and rotation about an origin and an arbitrary axis origin help

Greetings All

Thanks to James and Chas for helping me get this far btw Chas the language I wrote it in is in matlab. I tried to respond to your post but wasn't able to do it..I guess the gremlins are acting up on my computer. Also since I'm only able to post one link I'll post a link with to the text file which will have the animation link in it. Here's a link to the code as a text file / animation / other links in case the format gets messed up --> http://db.tt/4eYMMz7

I have the output of my quaternions in the format [w x y z]. My first vector point is [1x 0y 0z]
and the arbitrary point I want to rotate about is [0.5x 0y 0z]. The thing is when I plot the quaternion values for the x and y values they don't rotate about the origin [0x 0y 0z] or the arbitrary axis origin of [0.5x 0y 0z] can someone tell me what I'm doing wrong? I checked my axis-angle to quaternion conversion by checking it with the spincalcvi.m file located on matlab's file exchange. his format was in [x y z w] and mine is in [w x y z] see above link

I used the Multiplication formula for two quaternions from this site see above link and I tested it using this online quaternion calculator and it matches up see above link When I step through theta I don't get the circular rotations.

Ps: Yes I do want to plot points in 3D eventually but I'm trying to understand/get the 2D portion to work first. By first getting a point to rotate around the origin [0x 0y 0z] and a point to rotate around an arbitrary axis origin of [0.5x 0y 0z]

See code below Here's a link to animation/video of plot showing values as theta changes -->see above link

Here's a link to the code as a text file in case the format gets messed up --> see above link

    %Quaterion rotation - around orgin and arbitrary axis
%quaterions using Format [w xi yj zk] or [w x y z]

for ii=1:10:720
p1=[1,0,0] %[x y z] %point to rotate
%q=zeros(1,4);
theta=0+ii;
%normalize -p1- angle axis to pointing vector prior to quaternion construction
p1norm=p1(1:3)./norm(p1(1:3)) %norm is x^2+y^2+z^2
%Create Quaternion from p1norm
qp1 = [cosd(theta/2) sind(theta/2)*p1norm] % format [w, xi, yj, zk]
%normalize quaternion qp1
qp1mag=sqrt(qp1(1,1)^2+qp1(1,2)^2+qp1(1,3)^2+qp1(1,4)^2)
qp1norm=qp1(1:4)./qp1mag

%Arbitrary axis section------------------------
rp2=[.5 0 0] %[x y z]
%normalize -p2- angle axis to pointing vector prior to quaternion construction
rp2norm=rp2(1:3)./norm(rp2(1:3))
%Create Quaternion from p2norm
qrp2 = [cosd(theta/2) sind(theta/2)*rp2norm] % format [w, xi, yj, zk]
%normalize quaternion qrp2
qrp2mag=sqrt(qrp2(1,1)^2+qrp2(1,2)^2+qrp2(1,3)^2+qrp2(1,4)^2)
qrp2norm=qrp2(1:4)./qrp2mag

%Conjugate of qp2norm
qrp2normconj=qrp2norm.*[1 -1 -1 -1] % [w x y z] format

%1st Multiplication  (qrp2norm*qp1norm) full formula q*P*q' or qrp2norm*qp1norm*qrp2normconj
q1w=qrp2norm(1,1);q1x=qrp2norm(1,2);q1y=qrp2norm(1,3);q1z=qrp2norm(1,4); %rotated vector
p1w=qp1norm(1,1);p1x=qp1norm(1,2);p1y=qp1norm(1,3);p1z=qp1norm(1,4); %part being rotataed

qqw=(q1w*p1w - q1x*p1x - q1y*p1y - q1z*p1z)
qqx=(q1w*p1x + q1x*p1w + q1y*p1z - q1z*p1y)
qqy=(q1w*p1y - q1x*p1z + q1y*p1w + q1z*p1x)
qqz=(q1w*p1z + q1x*p1y - q1y*p1x + q1z*p1w)
qm1=[qqw qqx qqy qqz]

%2nd Multiplication  (qm1*qrp2normconj) full formula q*P*q' or qrp2norm*qp1norm*qrp2normconj
q1w=qm1(1,1);q1x=qm1(1,2);q1y=qm1(1,3);q1z=qm1(1,4); %rotated vector
p1w=qrp2normconj(1,1);p1x=qrp2normconj(1,2);p1y=qrp2normconj(1,3);p1z=qrp2normconj(1,4); %part being rotataed

qqw=(q1w*p1w - q1x*p1x - q1y*p1y - q1z*p1z)
qqx=(q1w*p1x + q1x*p1w + q1y*p1z - q1z*p1y)
qqy=(q1w*p1y - q1x*p1z + q1y*p1w + q1z*p1x)
qqz=(q1w*p1z + q1x*p1y - q1y*p1x + q1z*p1w)
qm2=[qqw qqx qqy qqz]

%plotting section

plot(0,0,'b',qp1norm(1,2),qp1norm(1,3),'-ro',qm2(1,2),qm2(1,3),'-bx')
grid on
hold on
legend('not used','ROT around origin','ROT around arbitrary axis');
title( { 'Quaternion Plot problems', ...
['theta = ', num2str(theta),' degrees'], ...
['Rot around orgin [w x y z] = ', num2str(qp1norm)], ...
['Rot around arbitrary axis [w x y z] = ',num2str(qm2) ] } )
axis([-1.5 1.5 -1.5 1.5 -1.5 1.5])
view([26 64]);
xlabel('x axis');ylabel('y axis');zlabel('z axis');

pause(.5)
M(ii)=getframe;
end


But I'm at a lost as to why I don't get a circular rotation

-
From your description, it sounds like you're conflating two different notions of 'around' in 'rotating around' - there's the notion of 'rotation around a point' (that is, linear transformations of a space that fix that point) and 'rotation around an axis' (which implicitly involves a point, usually the origin, and consists of linear transformations that fix that point and axis). From the description I would think you want to rotate around the origin and the X axis (i.e., the latter case), but you're producing two entirely different results for two different rotations. –  Steven Stadnicki Mar 18 '11 at 15:45
And on further reading of the text, it sounds like you may even be conflating 'axis' and 'point'. Though they can both use the same coordinates, the former represents a line (through the origin, generally) and the latter a point; rotating about the axis (0.5, 0, 0) (which would usually be normalized to (1, 0, 0) ) is dramatically different from rotating about the point (0.5, 0, 0). Can you be more specific about what you're trying to do? (And for those of us without Matlab, what you're seeing?) –  Steven Stadnicki Mar 18 '11 at 15:48