1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

What is the difference of Quaternion Slerp and Lerp?

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

  1. Germanator35

    Germanator35

    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,674
    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.
     
  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
     
  4. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
  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.
     
    JoeStrout likes this.
  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. maxfelker

    maxfelker

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

     
    SpaceManDan likes this.
  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)?
     
  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
  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. TheKingOfTheRoad

    TheKingOfTheRoad

    Joined:
    Apr 24, 2013
    Posts:
    16
    Best post ever!
     
  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).