# Rotating a vector

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-05-2005
Trauts
Rotating a vector
how can I rotate a 3D vector along an arbitrary plane?

for example... I have a vector that points straight up the wall, and one that points to the right along the wall, and I want to rotate it to the left, so that its pointing diagonally across the wall

The wall isn't necessarily going straight up.

The angle is arbitrary. So I might want to rotate the vector left or right 45 degrees, etc.

for convenience, the normal to the wall is called 'up', going up in the z axis is 'forward' and going to the right facing away from the wall is 'right'
• 01-05-2005
Darkness
Two assumptions you have to make:

The axis you are rotating about is unit length and its tail is centered at the origin. The vector you are rotating is not necessarily unit length and is also centered at the origin.

Rotating a vector centered at the origin about some arbitrary direction is the same as rotating a point about some arbtirary direction.

Rotating a point about some arbitrary direction is the same as plotting a polar coordinate about some arbitrary direction.

In general, when you plot a polar coordinate, you must have an orthogonal basis. An orthogonal basis is describe by two vectors P and Q which are perpendicular to each other. P and Q lie on the same plane, and the direction you are rotating about is the normal to this plane. P is sort of like your local 'x' axis, and Q is sort of like your local 'y' axis. To rephrase, P and Q are perpendicular to each other, and also are perpendicular to the direction you are rotating about. The general equation for plotting the polar coordinate (which, should be familiar to you otherwise you may need to study up on your math before doing anything more complex) is:

RotatedPoint = P cos (theta) + Q sin (theta)

The original vector you are rotating is vector W. Vector W has components parallel (in the same direction as) the normal and perpendicular to the normal (remember, the normal is the direction you are rotating W about). The problem is, P and Q must both be perpendicular to the normal. This poses a problem, because vector W has components parallel (in the same direction as) the normal. So, you decompose the vector W into components parallel and perpendicular to the normal, then you plus them into the equation above. Then, to complete it, you just add the parallel component back in (it never would have changed during the rotation).

Vector Parallel = Normal * (W dot Normal) //this bad boy doesn't change

Vector Perpendicular = W - Parallel

Vector P = Perpendicular //This is sort of like the local 'x' axis

EDIT: I had to change the order of this
Vector Q = CrossProduct ( Normal, Perpendicular) //This is sort of like the local 'y' axis

Vector Rotated = (P * cos(theta) + Q * sin(theta) ) + Parallel

It's hard to understand, especially if you don't have an inherent understanding of vector projections.

Also note, this is extremely confusing stuff and you likely won't understand it until you ask me questions. So, ask me questions, even if you think they are stupid questions or whatever.
• 01-05-2005
VirtualAce
Are you talking about performing an axis-angle rotation on a vector? If so, the matrix can be found all over the net. If not I can either give it to you or if you are using Direct3D you can use

D3DXMatrixRotationAxis(D3DXMATRIX *pOut,CONST D3DXMATRIX *pV,float Angle)

Angle is the number of radians to rotate vector pV by - this functions constructs the axis-angle rotation matrix. You would then use pOut to rotate vectors around pV by Angle.
• 01-05-2005
Darkness
Well, yeah. Use bubba's way if you just want to get it to work, which is okay. Use my way if you want to understand it (I basically proved how that matrix works).
• 01-05-2005
VirtualAce
The matrix is far more involved than the one equation you posted which is why I referenced that function.
• 01-05-2005
Darkness
What do you mean far more involved? Unless you're talking about a different matrix, I've shown *how* it works...how, exactly, do you get anymore involved?

EDIT:
I went into my own math libraries and checked one of my math books, and it turns out I *DID* prove the matrix you are talking about. To be more to the point, the 'one equation i posted', and the matrix you are talking about, are the same thing...one isn't more involved than the other, the matrix you are talking about is the matrix form of what I posted above.
• 01-05-2005
Trauts
Quote:

Originally Posted by Darkness
Two assumptions you have to make:

The axis you are rotating about is unit length and its tail is centered at the origin. The vector you are rotating is not necessarily unit length and is also centered at the origin.

Rotating a vector centered at the origin about some arbitrary direction is the same as rotating a point about some arbtirary direction.

Rotating a point about some arbitrary direction is the same as plotting a polar coordinate about some arbitrary direction.

In general, when you plot a polar coordinate, you must have an orthogonal basis. An orthogonal basis is describe by two vectors P and Q which are perpendicular to each other. P and Q lie on the same plane, and the direction you are rotating about is the normal to this plane. P is sort of like your local 'x' axis, and Q is sort of like your local 'y' axis. To rephrase, P and Q are perpendicular to each other, and also are perpendicular to the direction you are rotating about. The general equation for plotting the polar coordinate (which, should be familiar to you otherwise you may need to study up on your math before doing anything more complex) is:

RotatedPoint = P cos (theta) + Q sin (theta)

The original vector you are rotating is vector W. Vector W has components parallel (in the same direction as) the normal and perpendicular to the normal (remember, the normal is the direction you are rotating W about). The problem is, P and Q must both be perpendicular to the normal. This poses a problem, because vector W has components parallel (in the same direction as) the normal. So, you decompose the vector W into components parallel and perpendicular to the normal, then you plus them into the equation above. Then, to complete it, you just add the parallel component back in (it never would have changed during the rotation).

Vector Parallel = Normal * (W dot Normal) //this bad boy doesn't change

Vector Perpendicular = W - Parallel

Vector P = Perpendicular //This is sort of like the local 'x' axis

EDIT: I had to change the order of this
Vector Q = CrossProduct ( Normal, Perpendicular) //This is sort of like the local 'y' axis

Vector Rotated = (P * cos(theta) + Q * sin(theta) ) + Parallel

It's hard to understand, especially if you don't have an inherent understanding of vector projections.

Also note, this is extremely confusing stuff and you likely won't understand it until you ask me questions. So, ask me questions, even if you think they are stupid questions or whatever.

Heh. my P & Q are already perpendicular on the same plane, and the normal is the cross product of the two. Thank you, very much!

I was trying to use polar coordinates storing it as

x = cos( theta ); // polar coordinates
y = sin( theta ); // forms a cirle

But I didn't know what to do from there, as that didn't factor in Z.

You REALLY helped me out.
• 01-05-2005
Darkness
Awesome! Glad I could help! And good luck with whatever it is you are working on :)
• 01-05-2005
VirtualAce
Quote:

EDIT:
I went into my own math libraries and checked one of my math books, and it turns out I *DID* prove the matrix you are talking about. To be more to the point, the 'one equation i posted', and the matrix you are talking about, are the same thing...one isn't more involved than the other, the matrix you are talking about is the matrix form of what I posted above.
I never attacked you in this post and yet you continue to act as if I'm a threat to you. You got issues.

The matrix is this:
N here is the vector pV in the D3DXMatrixRotationAxis function prototype and matrix is of course the pOut member of the prototype. Theta is Angle.

Code:

```... matrix[0][0]=(n.x*n.x) * (1 - cos(theta)) + cos(theta); matrix[0][1]=(n.x*n.y) * (1 - cos(theta)) + n.z * sin(theta); matrix[0][2]=(n.x*n.z) * (1 - cos(theta)) - n.y * sin(theta); matrix[1][0]=(n.x*n.y) * (1 - cos(theta)) - n.z * sin(theta); matrix[1][1]=(n.y*n.y) * (1 - cos(theta)) + cos(theta); matrix[1][2]=(n.y*n.z) * (1 - cos(theta)) + n.x * sin(theta); matrix[2][0]=(n.x*n.z) * (1 - cos(theta)) + n.y * sin(theta); matrix[2][1]=(n.y*n.z) * (1 - cos(theta)) - n.x * sin(theta); matrix[2][2]=(n.z*n.z) * (1 - cos(theta)) + cos(theta); ...```
Of course this is 3x3 matrix but converting to 4x4 to allow for translation is a trivial matter.
• 01-06-2005
Darkness
Quote:

I never attacked you in this post and yet you continue to act as if I'm a threat to you. You got issues.
Then what is this supposed to mean:

Quote:

The matrix is far more involved than the one equation you posted which is why I referenced that function.

I'm probably the most passive person on these forums. Everything short of slapping me in the face won't generate a response from me. However, I'm actually trying to explain some of these nasty equations. Then, you made your comment which, to be honest, does come across as offensive...and, to top it all off, you were blatantly wrong...the matrix isn't any more involved than the equations I posted, because the matrix you reference to is the matrix form of the equations. Quite literally, when you do the equations out, it's exactly the same as the equations i posted.

Now, how is any sane person not supposed to be frustrated with you in this case? Keep in mind I actually had quite a bit of time invested in that response, and you just seemed quick to belittle it.
• 01-06-2005
Trauts
Quote:

Originally Posted by Darkness
Then what is this supposed to mean:

I'm probably the most passive person on these forums. Everything short of slapping me in the face won't generate a response from me. However, I'm actually trying to explain some of these nasty equations. Then, you made your comment which, to be honest, does come across as offensive...and, to top it all off, you were blatantly wrong...the matrix isn't any more involved than the equations I posted, because the matrix you reference to is the matrix form of the equations. Quite literally, when you do the equations out, it's exactly the same as the equations i posted.

Now, how is any sane person not supposed to be frustrated with you in this case? Keep in mind I actually had quite a bit of time invested in that response, and you just seemed quick to belittle it.

Score one for darkness
• 01-06-2005
VirtualAce
Quote:

What do you mean far more involved? Unless you're talking about a different matrix, I've shown *how* it works...how, exactly, do you get anymore involved?
And that's not offensive? Stressing the *how*? You and I know pretty much the same stuff bud and it would be a lot more helpful to the board members if we could combine forces instead of always bantering at each other in other people's threads.

I apologize if I came off as offensive to you but I was actually just expanding on what you wrote. I never stated that your equation was wrong or inferior to anything. Frankly I don't understand why you took offense to it because nothing negative was said about your post. Saying that a matrix is more involved is a fact not a derogatory statement. Just because you know how to rotate on x,y and z for instance, does not necessarily mean you know how to put all of that into matrix form. Since all of 3D is pretty much matrix concatentation (save for quaternions) I was putting your equation into matrix form. The derivation of that matrix is not simple and even though I understand it, it would be extremely difficult to go through each step here in a post.
• 01-06-2005
Darkness
lol I got ranked up, with this comment:

Quote:

Stop being a whiny little spotlight hog.
I love it lmao
• 01-06-2005
prog-bman
Come on now guys you both are freaking good with 3d stuff. There is no need to fight you should combo up on a game or something :)
• 01-06-2005
Darkness
This might (not) come as a shock to you, but I don't work well with other people. I end up taking control over everything, I I am sillyI am sillyI am sillyI am sillyI am silly and whine until I get other people doing exactly as I say, otherwise I quit and make fun of their mothers.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last