Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rotate vector by quaternion

Discussion in 'Scripting' started by rptb1_legacy, Apr 23, 2009.

Thread Status:
Not open for further replies.
  1. rptb1_legacy

    rptb1_legacy

    Joined:
    Apr 3, 2009
    Posts:
    15
    How do you rotate a vector by a quaternion?

    Apologies for this very simple question, but I just can't find the operation in the Unity scripting reference. I would have expected the '*' operator to work, or there to be a vector.rotateBy method, or something.

    As a simple example, I would expect to be able to do this:

    var turnRight = Quaternion.FromToRotation(Vector3.forward, Vector3.right);

    and then

    v * turnRight

    to rotate v by 90 degrees (clockwise, looking down) around the Y axis. I'm sure it must be there and I'm just missing it.
     
    MaxLohMusic, Xepherys, Finn_O and 2 others like this.
  2. dawvee

    dawvee

    Joined:
    Nov 12, 2008
    Posts:
    276
    You're on the right track. The only trick is that the order matters when multiplying vectors and quaternions (matrix multiplication is not comutative).

    So Quaternion * Vector3 will work, but Vector3 * Quaternion is undefined and will throw an exception.
     
  3. rptb1_legacy

    rptb1_legacy

    Joined:
    Apr 3, 2009
    Posts:
    15
    Well I think I found the answer. The '*' operator simply takes the quaternion on the left. This is not documented either in Vector or Quaternion class references. I'll drop Unity a note about it.

    (Edit: Simultaneous post with dawvee. Thanks dawvee!)
     
  4. dawvee

    dawvee

    Joined:
    Nov 12, 2008
    Posts:
    276
    It is documented, in the Quaternion class reference (to multiply both ways the * operator would need another overload for "Vector3 * Quaternion").

    Edit: haha, no problem! :D
     
    SaSha_K likes this.
  5. rptb1_legacy

    rptb1_legacy

    Joined:
    Apr 3, 2009
    Posts:
    15
    Aha! I see what's wrong. In the class reference itself is just says "operator * Combines rotations lhs and rhs." But if you click on "operator *" then it also mentions "quaternion * vector". Still needs fixing imho, but thanks again.
     
    BrainSlugs83 likes this.
  6. dmcclurg

    dmcclurg

    Joined:
    Sep 26, 2012
    Posts:
    1
    You can get Vector3 * Quaternion from Quaternion * Vector3 by using Quaternion.Inverse.

    Code (csharp):
    1.     v = Quaternion.Inverse(turnRight) * v; // same as v * turnRight
    So it's not really a drama.
     
    Last edited: Jan 2, 2013
    Bunny83, ysftulek, ig1r and 5 others like this.
  7. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    You just resurrected a post that was almost 4 years old to say there was no drama.
     
    drHogan, Trunksome, Filip8429 and 2 others like this.
  8. MaximusCZ

    MaximusCZ

    Joined:
    Jun 21, 2013
    Posts:
    2
    With a solution that I was looking for, and found it here, after another 4 months.
    Really why do it bother you that he resurrected this thread with a valid, actual information? Would you be rather if I created another thread asking another question to this topic?


    dmcclurg - Thank you
     
    twobob, ysftulek, Spemble and 6 others like this.
  9. RyuHayaboosa

    RyuHayaboosa

    Joined:
    Mar 8, 2015
    Posts:
    14
    I'm pretty sure he was just poking fun at the no drama line. No need to make drama.
     
  10. mtytel

    mtytel

    Joined:
    Feb 28, 2014
    Posts:
    84
    You just resurrected a post that was over 4 years old to make a dumb joke.
    Nice.
     
  11. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,960
    See you guys in 4 years!
     
  12. Wawwaa

    Wawwaa

    Joined:
    Sep 30, 2017
    Posts:
    164
    I love you guys. You solved my problem, and you made me smile.
     
    jojue likes this.
  13. ARealiti

    ARealiti

    Joined:
    Oct 4, 2013
    Posts:
    132
    This worked for me too!

    Jan 20, 2022
     
    TigerHix, dogzerx2, Spemble and 4 others like this.
  14. Pawn

    Pawn

    Joined:
    Jul 19, 2012
    Posts:
    1
    Just thought I'd check in to let y'all know that there's still no drama :D
     
  15. Wawwaa

    Wawwaa

    Joined:
    Sep 30, 2017
    Posts:
    164
    I am still using this information a lot and appreciat this forum topic. Well, yes, 4 years rule... Going back to work right now... :)
     
  16. hvent90

    hvent90

    Joined:
    Jun 18, 2018
    Posts:
    19
    Everything is looking OK in there. Keep on
     
    ysftulek likes this.
  17. Super_Simon

    Super_Simon

    Joined:
    May 12, 2017
    Posts:
    1
    This thread is so dramatic its amazing
     
  18. ysftulek

    ysftulek

    Joined:
    Aug 14, 2015
    Posts:
    41
    lol really funny thread, it helped me and made me smile at the same time.
     
  19. Clonkex

    Clonkex

    Joined:
    Jul 31, 2012
    Posts:
    31
    Thought I should just drop by now that it's 2020. Can confirm, still no drama.
     
  20. BrainSlugs83

    BrainSlugs83

    Joined:
    Jun 18, 2015
    Posts:
    38
    2020, and this bug (which has a one line of code fix) is still present... :rolleyes:
     
  21. MarkPeskir

    MarkPeskir

    Joined:
    Aug 19, 2017
    Posts:
    6
    It is nearly the end of 2020, and I assure you there is much drama.
     
    augustluhrs likes this.
  22. thesupersoup

    thesupersoup

    Joined:
    Nov 27, 2017
    Posts:
    70
    But there's no drama around Quaternion's * operator, that's for sure.
     
    Bunny83 likes this.
  23. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,531
    For the last time: THIS IS NOT A BUG. This is how quaternions work. Quaternion multiplication is NOT commutative.

    Actually to rotate a vector / point by a quaternion "q" you have to do

    p' = q * p * q^-1


    where
    q^-1
    is the complex conjugate of q. It has to be done is this order. Unity does this "sandwich multiplication" internally for you when you do
    q * p
    . I recommend to watch this 3blue1brown video on quaternions.
     
    owlrazum and Greenhapi like this.
  24. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,009
    My 5 cents.
    One thing that I have experience (prove me wrong i'm not 100% sure) is that quaternions representing rotation around the same axis are commutative.
     
  25. PiotrZdanowski

    PiotrZdanowski

    Joined:
    Jun 15, 2018
    Posts:
    4
    Helo there!
     
    dogzerx2 likes this.
  26. ArunasProk

    ArunasProk

    Joined:
    Mar 21, 2020
    Posts:
    13
    More or less. You can conceptualise quaternions as local rotations. You will get different results if you look up and then roll to the side, and if you roll and then look up. If you nail the rotation to a specific axis and just manipulate one Euler angle - it doesn't really matter, but it's not normally a property of multiplying quaternions and it's something to be aware of.

    As for drama, last year was wild, I'm not sure what qualifies anymore. (Search engines still love this thread though.)
     
Thread Status:
Not open for further replies.