Search Unity

What is the difference of Quaternion Slerp and Lerp?

Discussion in 'Scripting' started by FelixIrnich, Aug 18, 2011.

  1. FelixIrnich

    FelixIrnich

    Joined:
    Apr 10, 2011
    Posts:
    19
    May seem like a stupid question but is it?
    I've already dug through google and unity forums but couldn't find a clear answer. So what is the real difference of Slerp and Lerp?
     
  2. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    SLERP is a spherical linear interpolation. The interpolation is mapped as though on a quarter segment of a circle so you get the slow out and slow in effect. The distant between each step is not equidistant.

    LERP is a linear interpolation so that the distant between each step is equal across the entire interpolation.

    A quick google search would have answered this for you. I know you say you dug through google but there are dozens of results when I enter either "SLERP" or "spherical LERP" that describes the difference.
     
    honor0102, Menikino, KasiAsa and 3 others like this.
  3. manimoor

    manimoor

    Joined:
    Aug 13, 2011
    Posts:
    3
    Hi..Quaternion.Lerp
    static function Lerp (from : Quaternion, to : Quaternion, t : float) : Quaternion
    Description

    Interpolates from towards to by t and normalizes the result afterwards.

    This is faster than Slerp but looks worse if the rotations are far apart.

    javascript code is here....
    // Interpolates rotation between the rotations
    // of from and to.
    // (Choose from and to not to be the same as
    // the object you attach this script to)

    var from : Transform;
    var to : Transform;
    var speed = 0.1;
    function Update () {
    transform.rotation =
    Quaternion.Lerp (from.rotation, to.rotation, Time.time * speed);
    }

    Quaternion.Slerp
    static function Slerp (from : Quaternion, to : Quaternion, t : float) : Quaternion
    Description

    Spherically interpolates from towards to by t.

    javascript code is here....
    // Interpolates rotation between the rotations
    // of from and to.
    // (Choose from and to not to be the same as
    // the object you attach this script to)

    var from : Transform;
    var to : Transform;
    var speed = 0.1;
    function Update () {
    transform.rotation =
    Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);
    }

    -------------------------------------------
    Cegonsoft
     
    honor0102 likes this.
  4. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    fuy likes this.
  5. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    To answer your question, the differences are performance and accuracy. Slerp interpolates two Quats accurately and precisely which requires more power, Lerp interpolates two quats unevenly. Basically if your in doubt use slerp and then if your game runs slower than you like try changing some Slerp calls to Lerp ideally where its not a big deal.

    But really you should just try each and see which gets the job done better, giving a little slack/advantage to lerp because it faster.
     
  6. OJ

    OJ

    Joined:
    Apr 17, 2013
    Posts:
    4
    I know this is an old thread but this video may be useful for future readers.



    Lerp = blue point
    Slerp = white point.
     
  7. mwfelker

    mwfelker

    Joined:
    Nov 2, 2011
    Posts:
    10
    OJ - This really helped, thanks!:eek:

     
  8. sotirosn

    sotirosn

    Joined:
    Jan 5, 2013
    Posts:
    24
    I don't get this, I thought quaternions were rotations. How do these moving points (Vector3) represent rotations(Vector4)?
     
    marcospgp and ajay_pat like this.
  9. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    The components of a quaternion vector are simply the components of a 4D complex number...

    Quaternion Q = <a, b, c, d>

    Q is a complex number of the form:
    Q = a + i*b + j*c + k*d; Where i, j, and k follow the various rules of quaternion arithmetic discovered by William Rowan Hamilton.

    Position in complex space can represent both scale and orientation in real space. Understanding precisely how a position in 4D complex hyperspace geometrically can represent real orientation in 3D real space is extremely difficult. But, you can begin to understand it if you know how 2D complex numbers can be used to represent 2D rotation (and scale) in real space and why multiplying two 2D complex numbers together results in a 2D rotational transform. There are many good explanations of that on the web.
     
    Last edited: Oct 28, 2013
    Bunny83 likes this.
  10. meatboyfan

    meatboyfan

    Joined:
    Oct 28, 2013
    Posts:
    7
    From my understanding the extra w parameter denotes the rotation.
     
  11. Brian-Stone

    Brian-Stone

    Joined:
    Jun 9, 2012
    Posts:
    222
    None of the components of the quaternion vector represent an angle, although it may be possible to run across a case where changing one or two of the values results in the rotation you expected. The coordinate system that a quaternion represents is determined by combinations of all four components.

    Given the unit quaternion q = (w,x,y,z), the equivalent left-handed (Post-Multiplied) 3×3 rotation matrix is:

    $b99e96bedf517a41b3e255335e7ab701.png

    Again, it's a lot easier to understand how and why quaternions work if you first learn how and why 2D complex numbers can be used to compute 2D rotational transforms. The principles are much the same.
     
  12. TheMaster42

    TheMaster42

    Joined:
    Mar 31, 2013
    Posts:
    4
    One of the best explanation videos I've ever seen in my life. Perfect and concise; thanks for posting this.
     
  13. DBarlok

    DBarlok

    Joined:
    Apr 24, 2013
    Posts:
    268
    Best post ever!
     
    Roman_Keivan likes this.
  14. BBeck

    BBeck

    Joined:
    Jan 12, 2014
    Posts:
    57
    Vector3's are not "points" and Vector4's are not "rotations". And Quaternions are not any type of vector although I actually like to imagine them as unit vectors that know what their orientation is.

    Vectors are arrows that represent a direction that is permanently tied to an amount. Like 4 pounds to the left or 3 meters down. The units are irrelevant.

    Where vectors get confused with points is that they store the point of the arrow head. The tail is always "assumed" to be at the origin (x,y,z=0,0,0). Mostly we use 2D or 3D vectors. A Vector 4 is kind of a tool we use to get the job done. Technically a Vector4 is a 4D vector which is impossible to imagine unless you come from a 4D universe. We don't use 4D vectors directly. Instead we shove 3D data into the 4D vector and then add another component. The reason we do this is generally to make the math come out right when working with the 4 by 4 matrices common in game math. (Which you never see in Unity, but that's basically what a "transform" is and you know how common those are in Unity. Matrices are handled under the hood for you in Unity, but you still need a 4D vector to multiply it times a 4 by 4 matrix.)

    Anyway, the 4D vector is probably x,y,z,w where x,y,z represents a 3D position or vector and w is an additional component that we use to make the math come out right which is usually 0 or 1.

    Vectors are always 2 positions: the head which is stored in the Vector object and the tail which doesn't need to be stored because ti's always at the origin.

    Quaternions are none of the above. That can be confusing for several reasons, including that Quaternions have 4 components, just like a 4D vector does. Because of that, you "could" store the 4 components of a Quaternion in the 4 components of a 4D vector, but that doesn't mean they are related to one another.

    All of the Quaternions used in game programming are a special type of quaternion called a "unit quaternion". Their value equals one. By doing that, the quaternion value is forced onto a 3D sphere that is two units in diameter. Because the sphere is 3D, you've "forced" the quaternion to exist in 3D space with a hidden 4th dimension that makes every possible 3D orientation unique.

    Quaternions are mind boggling. But it doesn't matter. You should think of quaternions as "black boxes" that hold rotations. It doesn't matter what happens inside the box. What matters is that you put a rotation/orientation in and you get the correct rotation/orientation out. And maybe it should be pointed out that we're not talking about a rotation here as much as we are talking about an orientation. All 3 dimensions of rotation (pitch, yaw, roll) are stored simultaneously in the quaternion. If you combine two quaternions with multiplication the resulting quaternion will hold the final orientation after applying both rotations.

    So, a quaternion can store the result of several orientation changes in one quaternion. This is basically the same for a rotational matrix. Quaternions and rotational matrices are used pretty much the same way, although I think rotational matrices are not commonly used externally in Unity (they're probably all over the place under the hood in Unity).

    But anyway, don't get vectors, quaternions, and matrices confused. We sometimes use them together, but they are all 3 completely separate things.

    Oh. And to answer your original question of "How do these points represent rotations?" LERP is interpolation. It's just a percentage: "How far is it between point A and point B? 20%? 40%? 82%?" When "LERP'ing" between two numbers you are just multiplying the percentage. So, you say "give me a point that is 23% of the way between Point A and Point B."

    With simple interpolation you do this on a 1 dimensional number line. For game programming you're probably going to use this at least in 2D if not 3D. So, you're finding a point that is 23% of the way between two 2D points or two 3D points, for example. It's called Linear intERPolation because the answers are all on a straight line.

    SLERP is the same thing except you're getting a percentage distance between two points on a circle or in 3D on a sphere.

    LERP in not necessarily a rotational thing, since it's on a straight line. A percentage distance between 2 points is not necessarily related to a rotation.

    But when you DO interpolate between two rotational positions, you generally want to use SLERP because LERP is inaccurate. That's because rotations happen on circles, not straight lines.

    So, SLERP is mostly for rotations, and LERP is largely not for rotations. Both are just finding a percentage position between two points. LERP does it on a straight line. SLERP does it on a circle, which is more appropriate for rotations. But hey, if you get good results from LERP in a rotation, more power to ya. The very helpful video shows how both could be applied to an unknown position between two given positions on a circle. When you do the LERP or SLERP math, you will give two points and a percentage between them and receive an answer of a point that is that percentage between the two. But LERP is linear and SLERP is spherical.
     
    Last edited: Feb 13, 2014
  15. neamerjell

    neamerjell

    Joined:
    Jul 20, 2015
    Posts:
    6
    Thank You! Thank You! Thank You!

    You have truly enlightened me! I can't say I understand all of it, but I now have a pretty good grasp of what all these things represent.
     
  16. zhengcsc

    zhengcsc

    Joined:
    Aug 20, 2016
    Posts:
    1
    I dont know much about Quaternion.
    But I think we should not try to understand Quaternion as an angle(or Radian).
    The difference between Quaternion and Degree(or Radian) determines the usage of Slerp(Instead of Lerp).
     
  17. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    For me this was from google so it's good that these topics exist.
     
    Guilherme-Kodi likes this.
  18. Will_Croxford

    Will_Croxford

    Joined:
    Mar 7, 2018
    Posts:
    5
    Best answer I think!
     
  19. Willard720

    Willard720

    Joined:
    Apr 22, 2018
    Posts:
    2
    This post is the first one that came up on Google. Congrats on being a jerk though
     
  20. aagun01

    aagun01

    Joined:
    Aug 10, 2020
    Posts:
    2
    you are the google search dude
     
    Arycama likes this.
  21. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,992
    Not only that but it's also wrong ^^.

    This is wrong. The point of Slerp is to provide an equidistant step throughout the interpolation. However Slerp only exists and make sense in the vector or quaternion case. Or as wikipedia states it there is the geometric slerp and the quaternion slerp, though both have the same goal. That is provide an equidistant / uniform linear interpolation along the unit arc between the two endpoints. This has nothing to do with any easing. It's a pure geometric property that doesn't exist in 1d. It ensures that the length of the vector is interpolated linearly from the start to the end. That means if the length of the start and end vector have the same length, the length will stay the same during the interpolation.

    So if you interpolate between two unit direction vectors the resulting vector will also be a unit vector and will rotate towards the target at a uniform angle step unlike Vector3.Lerp. With Vector3.Lerp the vector would generally shrink during the interpolation, having the shortest length at t==0.5. Also the delta angle (assuming a uniform change in t) will be smaller at the two ends and greatest at t==0.5. The worst edge case are two opposite vectors. Lerp would just shrink the vector down to 0 and suddenly flip it to the other side. So the angle would stay the same for half the distance and then flip 180° as the vector grows again. Slerp will properly rotate the vector around (0,0,0). That's why Slerp only makes sense for directions or rotations.
     
  22. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    I liked justinlloyd's comment only because of his last remark :) (a jerk is whoever is lazy or lying, not the ones who are annoyed by such lies, don't flip this around)

    Here's a more visual explanation of quaternion rotations for beginners, explaining how to develop an intuition for working with them. I have a decent understanding of quaternions and the analogy used in the end of this video is pretty much spot on, for all UNIT quaternions (which Unity's quaternions always are, unless you fiddle with their parameters on your own).

    Here's an explanation of how Quaternion.Euler works and full code for an expanded implementation of it (specifically, it enables a different order of Euler rotations). (The actual Euler code is more low-level, gritty and mathematical than this, so this is a useful tool to understand what's going on, because I've made it much more readable.)

    Another *lerp worth mentioning is NLerp, which provides a result that is similar to SLerp in practice, yet derived through an ordinary lerp. Practically, it's a poor man's SLerp, and if used in animation it rears its ugly head, but can be used for small angles -- on the upside it is much much faster than SLerp.

    NLerp is short for normalized lerp and is as simple as
    Code (csharp):
    1. Vector3 NLerp(Vector3 start, Vector3 end, float t) => Vector3.Lerp(start, end, t).normalized;
    Pretty useful if you don't want to let a player have to strafe diagonally to break your speed limit.
    https://doom.fandom.com/wiki/Straferunning
    https://www.quora.com/In-games-why-is-diagonal-movement-strafing-much-faster-than-straight-movement

    Here's a nice article about all three of these.
     
    Last edited: Oct 24, 2020
  23. Spemble

    Spemble

    Joined:
    Mar 20, 2016
    Posts:
    7
    I'm 80% sure the lerp point (blue) is projected back onto the sphere, so the in-between points still represent rotations.

    If the blue point was projected back onto the sphere, it still wouldn't be in the same place as the while point. The effect is that the white point rotates at a constant rate while the lerp is slower in the start and faster in the middle (and this discrepancy would be stronger if the two end angles where closer to 180 degrees apart). The trade-off is likely computational time.

    My guess is that in the end, if your two angles are close enough together, you are probably fine with LERP, but if they could be close to 180 degrees apart or you want to guarantee constant rotation speed for more accuracy, use SLERP.
     
    Bunny83 likes this.
  24. DigitalChaotics

    DigitalChaotics

    Joined:
    Dec 18, 2013
    Posts:
    25
    A quick google search led me directly to this page, and answered my question. :p
     
    B_ware likes this.
  25. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    But does everyone on the forum here need to know that? ;)

    Please don't necro threads, please use the like button.

    Thanks.
     
    B_ware, TsFreddie and Bunny83 like this.