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

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
cr_itm
Posts: 57
Joined: Sun May 01, 2005 10:13 am

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

Post 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
peterparker
Posts: 11
Joined: Mon Nov 28, 2005 5:00 am
Location: N.Y.

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

Post 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
Xaron
Posts: 310
Joined: Sun Oct 16, 2005 7:39 am
Location: Germany
Contact:

Post by Xaron »

Wow. Thanks for that! :) I have a similar problem.

Regards - Xaron
cr_itm
Posts: 57
Joined: Sun May 01, 2005 10:13 am

Post 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
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

this gets asked so much it should be in the faq

http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=10142
(spintz's post)
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
cr_itm
Posts: 57
Joined: Sun May 01, 2005 10:13 am

Post 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.
Post Reply