Rotating a vector

This is a discussion on Rotating a vector within the Game Programming forums, part of the General Programming Boards category; how can I rotate a 3D vector along an arbitrary plane? for example... I have a vector that points straight ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    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'

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    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.
    Last edited by Darkness; 01-05-2005 at 03:37 PM.
    See you in 13

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    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).
    See you in 13

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    The matrix is far more involved than the one equation you posted which is why I referenced that function.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    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.
    Last edited by Darkness; 01-05-2005 at 03:53 PM.
    See you in 13

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    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.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    Awesome! Glad I could help! And good luck with whatever it is you are working on
    See you in 13

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    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:

    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.

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    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

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.

  13. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    lol I got ranked up, with this comment:

    Stop being a whiny little spotlight hog.
    I love it lmao

  14. #14
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,804
    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
    Woop?

  15. #15
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    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.
    See you in 13

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21