Quaternions for rotating Vectors

Hey C++ Community,

i got a problem here, i try to use quaternions to rotate one(and later more!) 3d-vectors in a general manner.

i read in wikipedia, that the general calculation goes with:

vector_rot = quaternion * vector * quaternion_compl.conj.

i used the class of irrlicht http://irrlicht.sourceforge.net/docu/classirr_1_1core_1_1quaternion.html
to do the calculations.

there is no premade function in this class to rotate vectors through this quaternions, so i just tried to it this way:

1
2
3
4
5
	irr::core::vector3df v1(1,0,0);
	irr::core::quaternion a1(2,3,4,6);
        a1.normalize();
        irr::core::vector3df result2(a1.X*v1.X*(-a1.X),a1.Y*v1.Y*(-    a1.Y),a1.Z*v1.Z*(-a1.Z));


result2 should then be the rotated vector, but it does not work. i dont know how to explicitely write down the rotation specification in this topic, i hope you can help me!

best wishes

beinando
Your operation is incorrect, ¿why don't simply use the class interface?
a1*v1*a1.inverse();
v1*a1.inverse is not defined somehow , while a1*v1 seems to be no problem here.

why is the operation incorrect?
Last edited on
i tried to write the to-be-rotated vector as a quaternion with real-part of 0 now. code looks like this:

1
2
3
4
5
6
7
8
9
10
11
	irr::core::quaternion v1(1,0,0,0);
	irr::core::quaternion a1(1,0,0,3);
	irr::core::quaternion a2(0,0,0,0);

	a1.normalize();

	a2=a1.makeInverse();

	v1=a1*v1*a2;




the problem is that, after the rotation of the to-be-rotated- vector, the real part does not stay zero. generally, i do not understand the line:

v' -> q*v*q^(compl. conj.) since q has 4 entries and the vector only 3.

i hope you can help me! thanks in advance

beinando
> why is the operation incorrect?
1
2
3
a1.X*v1.X*(-a1.X),
a1.Y*v1.Y*(-a1.Y),
a1.Z*v1.Z*(-a1.Z)
you were multiplying component by component.
Suppose that v1.Y = 0, then in the result would be 0 too, regardless of how the rotation is made; which is clearly wrong.

> the problem is that, after the rotation of the to-be-rotated- vector, the real part does not stay zero
`makeInverse()' modifies the calling object
1
2
3
4
5
a1.normalize();
a2 = a1;
a2.makeInverse();

v1 = a1*v1*a2;
If optimum performance is not required (and you're project does not require the math), I would separate this into pieces:

Rotate x, rotate y, and rotate z. You can create a function to rotate a function to rotate each axis,and then all you have to do is rotate the 3D array one axis at a time accordingly.

Of course, this approach would be cpu-intensive, and if you're doing this for class, you're professor may not want you to do this.
well, i am not sure if performance issues could appear later on. i am doing this for a gran-canonical monte-carlo simulation so i think it should be better to make a fast rotation of my "molecules" in the simulation.

also, i thought that rotations via quaternions are kinda easy to implement if you started understand it once.

what i don't understand is the calculation:

v' -> q*v*q^(compl. conj.)

q is a quaternion, and the real part of this quaternion includes the information of the rotation angle, while the imaginary parts i,j,k define the rotation axis.

is this correct?

so if your vector is 3-dimensional, like v=(x,y,z); and the quaternion is 4-dimensional: q= (X,Y,Z,W) this multiplication makes no sense. all i could do is write the 3d-vector as v=(x,y,z,0) but then i get nonzero entries in the 4th dimension, which is what i want to avoid, of course.
i did not really find a good example how this calculation is ment to be done in practise, if you know it i would be very happy if you could give me an example.

best wishes
I don't know if there is a simpler way, but you could take one of the forms
http://en.wikipedia.org/wiki/Quaternion#Scalar_and_vector_parts
http://en.wikipedia.org/wiki/Quaternion#Matrix_representations
to know the operations on each element

> but then i get nonzero entries in the 4th dimension, which is what i want to avoid, of course.
your code was wrong. `a2' an `a1' were the same quaternion.
Topic archived. No new replies allowed.