Page 1 of 1

How to rotate a vector around a custom vector (axis)?

Posted: Thu Dec 01, 2005 10:54 pm
by cr_itm
Hello,

I have a vector v1 (just a postition vector representing a point), and a vector v2, which represents an axis vector/direction vector.

How can I rotate v1 for e.g. 30 degrees around v2 ?

The vector class only has functions to rotate it around the world axis (XY, XZ, YZ). But I want to rotate v1 around a custom axis (v2).

(Maybe it does work with a rotation matrix, but the matrix4 class has only a method to set the three degrees, and not one degree with one rotation axis).

Can anyone help?

Thanks a lot

How to rotate a vector around a custom vector (axis)?

Posted: Fri Dec 02, 2005 6:01 pm
by peterparker
Here is a step-by-step instruction:
http://astronomy.swin.edu.au/~pbourke/geometry/rotate/

You can also look for the same here:
http://www.mines.edu/~gmurray/Arbitrary ... ation.html

hope that helps!
Peter

Posted: Fri Dec 02, 2005 7:22 pm
by Xaron
Wow. Thanks for that! :) I have a similar problem.

Regards - Xaron

Posted: Sat Dec 03, 2005 5:15 pm
by cr_itm
Thanks for the links. Meanwhile I tried the matrix for rotating a vector around a custom axis, I got it from wikipedia:

http://de.wikipedia.org/wiki/Rotationsmatrix

It'*s from the german wikipedia site, the rotation matrix is the fifth matrix on the page (and the biggest one). So I tried the following:

Code: Select all

	matrix4 m = new matrix4();
				
	float alpha = (float)((2 * Math.PI) / 360);  // 1 Degree
	float cos_a = (float)Math.cos( alpha );
	float sin_a = (float)Math.sin( alpha );
		
	float v1 = v.getX();
	float v2 = v.getY();
	float v3 = v.getZ();

	float[] rotm = new float[16];
	rotm[0] = cos_a + v1*v1*(1-cos_a);
	rotm[1] = v2*v1*(1-cos_a)+v3*sin_a;
	rotm[2] = v3*v1*(1-cos_a)-v2*sin_a;
	rotm[3] = 1.0f;
	rotm[4] = v1*v2*(1-cos_a)-v3*sin_a;
	rotm[5] = cos_a+v2*v2*(1-cos_a);
	rotm[6] = v3*v2*(1-cos_a)+v1*sin_a;
	rotm[7] = 1.0f;
	rotm[8] = v1*v3*(1-cos_a)+v2*sin_a;
	rotm[9] = v2*v3*(1-cos_a)-v1*sin_a;
	rotm[10]= cos_a+v3*v3*(1-cos_a);
	rotm[11]= 1.0f;
	rotm[12]= 1.0f;
	rotm[13]= 1.0f;
	rotm[14]= 1.0f;
	rotm[15]= 1.0f;

	m.setM( rotm );
				
	m.transformVect( target );
It would be great if someone may have a look at the code, because it just don't work. The target (the vector that I transform with the matrix) just goes to one direction on and on... there is no rotation at all, and I don't know why :(

Please, if someone has an idea, that would be great.

@Xaron: Does it work for you? Were you able to do a rotation around an axis vector?

Thanks a lot

Posted: Sun Dec 04, 2005 2:27 am
by bitplane
this gets asked so much it should be in the faq

http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=10142
(spintz's post)

Posted: Sun Dec 04, 2005 7:17 pm
by cr_itm
bitplane wrote:this gets asked so much it should be in the faq

http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=10142
(spintz's post)
Sorry, but after reading that thread, I don't think it has anything to do with the problem to rotate a point around a custom axis.