Search Unity

Inverse array of vector3 and quaternions

Discussion in 'Scripting' started by yellowyears, Sep 13, 2020.

  1. yellowyears

    yellowyears

    Joined:
    Feb 14, 2020
    Posts:
    36
    Quaternion.Inverse doesn't work on an array, and there is no afaik to inverse a Vector3, nontheless an array of them. Anyone got an idea?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Anything can "work on an array" but you have to iterate the array yourself.

    Both Quaternions and Vector3s are structures, which are value types, not reference types. This means means if you copy them out of the array you have made a copy. If you change that copy you have not changed the array contents. You need to then assign it back into the array.

    Is that your problem?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Of course not. I f you have an array of Quaternions that you want to invert, you would iterate through the array and invert each individual Quaternion.

    Multiplying by -1 is what I imagine "inverting" a Vector to be.

    What's your actual goal here? You want to invert things but to what end?
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,992
    Right. Maybe he was under the impression that Quaternion.Inverse is something like "reverse" the order or something? I'm also confused if this is a question about reversing elements in an array or if it's actually about quaternions and their inverse.

    A unit quaternion is used to represent rotations in 3d space. The inverse of a quaternion represents the same rotation just in the opposite direction. Since quaternions essentially are an extension of complex numbers they can also be seen as an extension of a Vector. Actually the inverse of a unit quaternion is equal to its complex conjugate. So the vector part of the quaternion is inverted (multiplied by -1).
     
  5. yellowyears

    yellowyears

    Joined:
    Feb 14, 2020
    Posts:
    36
    I have found a solution. Rather than using the builtin function to inverse a quaternion, I get a Quaternion and return the inverse through code. Then in a for loop I get the array of quaternions and iterate through each of them, setting the i index to InverseBoneRotations(quatArray). For some reason I can't seem to put in the i array element into the reply so you'll have to take my word that it is the array's i index