Results 1 to 13 of 13

Thread: Rotate a 3D vector

  1. #1

    Thread Starter
    Addicted Member Cuallito's Avatar
    Join Date
    Apr 2001
    Location
    You know that chest that you could never get opened? If you ever do, I'm there.
    Posts
    136

    Arrow Rotate a 3D vector

    How do you rotate a 3D vector by XYZ corrdinates without using matrices or any of that junk?
    BTW, Thanks for all your help

    Member of the anti-gay cross-dressing trans-species wolves alliance.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It's not junk, it's a very fast way to do transformations. who limited you from not using them?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Addicted Member Cuallito's Avatar
    Join Date
    Apr 2001
    Location
    You know that chest that you could never get opened? If you ever do, I'm there.
    Posts
    136

    Red face OK

    Ok then, how do you rotate a vector by XYZ corridenates around a certain location (with matrices or not ;p)? Also, you do you rotate a XYZ vector by a quaternion? Hey, I've only had Algebra I! Thnx in advance
    BTW, Thanks for all your help

    Member of the anti-gay cross-dressing trans-species wolves alliance.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Addicted Member Cuallito's Avatar
    Join Date
    Apr 2001
    Location
    You know that chest that you could never get opened? If you ever do, I'm there.
    Posts
    136

    ff

    Ok... Let's see if I got this right: Take a matrix, rotate it by ur xyz corridinates, and then multiply it by ur original vector. It that right?
    BTW, Thanks for all your help

    Member of the anti-gay cross-dressing trans-species wolves alliance.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    that is if you need to do a transformation. X x Y x Z can be done in one step and then transform, any vector you multiply with the result will be rotated in respect to the transformation.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    This is in C but the theory's sound...
    Code:
    void BuildRotMatrix() {
    	m_pfMatrix[0][0] = 1.0 - 2.0 * (m_qTotal[1] * m_qTotal[1] + m_qTotal[2] * m_qTotal[2]);
    	m_pfMatrix[0][1] = 2.0 * (m_qTotal[0] * m_qTotal[1] - m_qTotal[2] * m_qTotal[3]);
    	m_pfMatrix[0][2] = 2.0 * (m_qTotal[2] * m_qTotal[0] + m_qTotal[1] * m_qTotal[3]);
    	m_pfMatrix[0][3] = 0.0;
    
    	m_pfMatrix[1][0] = 2.0 * (m_qTotal[0] * m_qTotal[1] + m_qTotal[2] * m_qTotal[3]);
    	m_pfMatrix[1][1]= 1.0 - 2.0 * (m_qTotal[2] * m_qTotal[2] + m_qTotal[0] * m_qTotal[0]);
    	m_pfMatrix[1][2] = 2.0 * (m_qTotal[1] * m_qTotal[2] - m_qTotal[0] * m_qTotal[3]);
    	m_pfMatrix[1][3] = 0.0;
    
    	m_pfMatrix[2][0] = 2.0 * (m_qTotal[2] * m_qTotal[0] - m_qTotal[1] * m_qTotal[3]);
    	m_pfMatrix[2][1] = 2.0 * (m_qTotal[1] * m_qTotal[2] + m_qTotal[0] * m_qTotal[3]);
    	m_pfMatrix[2][2] = 1.0 - 2.0 * (m_qTotal[1] * m_qTotal[1] + m_qTotal[0] * m_qTotal[0]);
    	m_pfMatrix[2][3] = 0.0;
    
    	m_pfMatrix[3][0] = 0.0;
    	m_pfMatrix[3][1] = 0.0;
    	m_pfMatrix[3][2] = 0.0;
    	m_pfMatrix[3][3] = 1.0;
    }
    Where m_qTotal is a rotation quaternion.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8

    Thread Starter
    Addicted Member Cuallito's Avatar
    Join Date
    Apr 2001
    Location
    You know that chest that you could never get opened? If you ever do, I'm there.
    Posts
    136

    Help

    Well I coded a little sub, but it won't rotate anything:
    Code:
    Function RotateVector(ByRef VectorIn As D3DVECTOR, X As Single, Y As Single, Z As Single) As D3DVECTOR
        Dim Matrix(1 To 2, 1 To 3) As Single
        
        
        'X rotation
        Matrix(1, 1) = VectorIn.X
        Matrix(2, 1) = 0
        Matrix(1, 2) = Cos(X) * VectorIn.Y
        Matrix(2, 2) = -Sin(X) * VectorIn.Z
        Matrix(1, 3) = Sin(X) * VectorIn.Y
        Matrix(2, 3) = Cos(X) * VectorIn.Z
        
        VectorIn.X = Matrix(1, 1) + Matrix(2, 1)
        VectorIn.Y = Matrix(1, 2) + Matrix(2, 2)
        VectorIn.Z = Matrix(1, 3) + Matrix(2, 3)
        
        'y rotation
        Matrix(1, 1) = Cos(Y) * VectorIn.X
        Matrix(2, 1) = -Sin(Y) * VectorIn.Z
        Matrix(1, 2) = VectorIn.Y
        Matrix(2, 2) = 0
        Matrix(1, 3) = -Sin(Y) * VectorIn.X
        Matrix(2, 3) = Cos(Y) * VectorIn.Z
        
        VectorIn.X = Matrix(1, 1) + Matrix(2, 1)
        VectorIn.Y = Matrix(1, 2) + Matrix(2, 2)
        VectorIn.Z = Matrix(1, 3) + Matrix(2, 3)
        'z rotation
        Matrix(1, 1) = Cos(Z) * VectorIn.X
        Matrix(2, 1) = -Sin(Z) * VectorIn.Y
        Matrix(1, 2) = Sin(Z) * VectorIn.X
        Matrix(2, 2) = Cos(Z) * VectorIn.Y
        Matrix(1, 3) = VectorIn.Z
        Matrix(2, 3) = 0
        
        VectorIn.X = Matrix(1, 1) + Matrix(2, 1)
        VectorIn.Y = Matrix(1, 2) + Matrix(2, 2)
        VectorIn.Z = Matrix(1, 3) + Matrix(2, 3)
        
        
        RotateVector = VectorIn
        
    End Function
    Whats Wrong? BTW, Thx Parksie for the sub, but is there a way I can rotate a vector by a quaternion(luv that word !)without having to make a matrix out of it first? That seems kind of slow...
    BTW, Thanks for all your help

    Member of the anti-gay cross-dressing trans-species wolves alliance.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    looks like you don't care about preserving the matrix, you could use this directly then:

    there's no need for quaternions if you don't have combinations of several rotation transformations.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    Addicted Member Cuallito's Avatar
    Join Date
    Apr 2001
    Location
    You know that chest that you could never get opened? If you ever do, I'm there.
    Posts
    136

    Angry Hmmmm......

    Well, I cam up with this:
    Code:
    Function RotateVector(ByRef VectorIn As D3DVECTOR, X As Single, Y As Single, Z As Single) As D3DVECTOR
        Dim Temp(10) As Single
        Dim xx As Single, yy As Single, zz As Single
        
        yy = VectorIn.Y
        xx = VectorIn.X
        zz = VectorIn.Z
        
        Temp(0) = Sin(X)
        Temp(1) = Cos(X)
        Temp(2) = Sin(Y)
        Temp(3) = Cos(Y)
        Temp(4) = Sin(Z)
        Temp(5) = Cos(Z)
        Temp(6) = yy * Temp(5)
        Temp(7) = xx * Temp(4)
        Temp(8) = Temp(5) * xx
        Temp(9) = Temp(4) * yy
        Temp(10) = Temp(3) * zz
        
        RotateVector.X = (Temp(3) * (Temp(8) - Temp(9)) + Temp(2) * Z)
        
        
        RotateVector.Y = (Temp(0) * (Temp(2) * Temp(8) - Temp(2) * _
            Temp(9) - Temp(10)) + Temp(1) * (Temp(7) + Temp(6)))
        
        RotateVector.Z = (Temp(1) * (-Temp(2) * Temp(8) + Temp(2) * _
            Temp(9) + Temp(10)) + Temp(0) * (Temp(7) + Temp(6)))
    
        
    End Function
    But it still doesn't work!? Thanks guys for your help.
    BTW, Thanks for all your help

    Member of the anti-gay cross-dressing trans-species wolves alliance.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    don't use arrays when unnessesary. replace that Z with ZZ for the X component expression.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The trigonometric functions in VB (actually, in nearly all programming languages) accept angles in radians. If you use degrees you'll be waaaay out .

    To convert from degrees to radians multiply by (pi / 180).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    so if the angle isn't specified by the user, screw degrees and store radians.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width