Search Unity

Rotate euler orientation by rotation axis.

Discussion in 'Scripting' started by koirat, Jan 15, 2022.

  1. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    Is there actually a simple way to rotate euler orientation by rotation axis and angle ?
    What I have is object that got current orientation in euler angles, world space rotation axis and angle.
    I would like to rotate this object by some angle around this axis and retrieve new euler angles.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    You can rotate another rotation with Quaternion multiplication.

    You can create a rotation around an axis with the Quaternion.AngleAxis() factory.

    Then you can multiply the other rotation by that.

    Be careful of order because Quaternion multiplication is NOT commutative.

    Remember that you don't want to use euler angles after they come out of a Quaternion: they are NOT reliable.

    https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html
     
  3. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    It is for VFX purpose so I cannot create Quaternion any easy way.
    Since VFX is using only Euler for orientation this generates a lot of problems.
     
  4. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    650
    I think it's

    eulerQuaternion * transform.rotation --> euler rotates in world space
    transform.rotation * eulerQuaternion --> euler rotates in local space

    (with eulerQuaternion I mean Quaternion.Euler(0,90,0) for example, and with "transform.rotation" I mean whatever quaterion you want to rotate)
     
  5. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    https://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html

    did you have a look at the second script down in this link? I had only just discovered it and it seems to provide a good understanding of how this whole game works. The second script looks like it gives you the angle you want in vector 3 at the end but I would go ahead and read the entire page for best understanding.
     
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,997
    For the extra rotation, you can use the builtin Quaternion.AngleAxis. It takes the angle in degrees, and just any axis.

    To add this to some other angle (is that what you're doing) you'd need to consider whether AngleAxis will use a world or local (to whatever you're adding it to) axis. I think you can pick whichever is easier and then combine them with * as appropriate.
     
  7. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    Thank you guys for all the effort, but I know how to do it in c#.

    Problem is that I don't have quaternions in VFX shaders and particle orientation is encoded as Euler angles there.
    I thought that there will be some simpler way to do this, but I got this very bad premonitions that I will have to construct rotation matrix than do the rotation and than convert it into euler angles.

    Just thinking how my vfx graph will look like gives me the chills.
     
  8. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,997
    Can't you cheat and have Unity hand-feed it to the shader each frame as a global? Often you can hand-feed some weird 1/2-done product that's just enough for the shader to adjust for each vertex or pixel.
     
  9. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    I have a lot of particles in VFX system, I want to rotate each of them separately.
    Each of them got initial rotation, and it's own global rotation axis.
    I just have to do it in shader.
     
  10. Are you looking for this?
     
  11. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,990
    No, there is not ^^. That's one of the major problems with euler angles that you can not simply rotate the euler angles representation around an arbitrary axis. That's the one of the main reasons why we actually use quaternions in the first place. As euler angles approach a gimbal lock orientation the 3 angles would need to change in a non linear way and certain transitions are not possible at all and would result in a strange arc.

    This video actually explains the issues with euler angles in general pretty well. Quaternions allow to apply arbitrary rotations around arbitrary axis and it always works. It's simply not possible with euler angles. At least you would not get a rotation around a single axis but one of those skewed arcs. Instead of Quaternions, if you want complete freedom of rotation you would need to use a 3x3 rotation matrix. Quaternions in essence represent such a rotation matrix, but in a much more compact format.

    If you don't want to carry out a quaternion - vector multiplication in a shader, an alternative could be to use a 3x3 matrix instead. Those are natively supported in shaders. Of course you could always do the quaternion rotation yourself in the shader. Some time ago I derived the equation manually just from the quaternion rules over here. It's essentially the same what Unity does in the Quaternion - vector multiplication operator.
     
    koirat likes this.
  12. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    And can I use it to rotate Euler angles (euler vector)?
    I tired to find some information on the internet, and everything I could find converted Euler to rotation matrix.
     
  13. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    Thank you for answer I kind of expected this will be a problem.
    If I ever decide to make VFX shader graph for this, it's going to be painful.
    There are some matrix operations in VFX but converting back to euler is another pain.