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

Find the middle between 2 rotations

Discussion in 'Scripting' started by Hazneliel, Dec 4, 2022.

  1. Hazneliel

    Hazneliel

    Joined:
    Nov 14, 2013
    Posts:
    303
    Hello, I am trying to find the middle rotation between 2 rotations from one side only, I am using:

    Quaternion.Lerp(angleA.rotation, angleB.rotation , 0.5f);

    However the angle will jump to the shortest angle as soon as the difference is more than 180 as seen in the gif:


    I appreciate any help with this.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    General forum is not a support forum.
    Please post such question in scripting.
    You should know that very whell, since you are member of Unity forum since 2013.
    Please read rules of the forum, if you don't remember them.
     
  3. Hazneliel

    Hazneliel

    Joined:
    Nov 14, 2013
    Posts:
    303
    Sorry I didnt know this, thanks for the friendly and kind heads up
     
  4. VadanaPon

    VadanaPon

    Joined:
    Jul 5, 2022
    Posts:
    45
    Hi, this is for euler, so it may not be what you're after, but you can try getting the middle like this:
    rotA / 2 + rotB / 2
    Or maybe the gentleman's way
    (rotA - rotB) / 2 + rotB
    Or the smooth brain way
    (rotA + rotB) / 2
     
    Last edited: Dec 4, 2022
  5. Hazneliel

    Hazneliel

    Joined:
    Nov 14, 2013
    Posts:
    303
    Thanks, I tried, however because it uses euler it gets into a gimbal lock problem as you can see
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    Well, Quaternion slerp uses shortest rotation path. When rotation goes over 180, shortest path lies on another side of the object.

    So either you store objects in parametric fashion (euler angles), OR you use quaternions, in which case no angle can go over 180. You can't really have it both ways in 3d.

    If your half-angle has some meaning, you could try tracking previous and current value of half-angle vector and flip it if previous value is too far away from the current one (meaning it flipped). But you'll be just making your life difficult, as with convoluted rotations this can lead to unexpected behavior.
     
  7. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,792
    Looks like you would store the start base rotation and when the second rotation 180 degrees plus from this point is achieved by the second rotation then the rotated angle should have 180 degrees either added or subtracted.
     
  8. CityGen3D

    CityGen3D

    Joined:
    Nov 23, 2012
    Posts:
    680