0 Favourites

lerp Clarification

  • I don't fully understand the way lerp works.

    I'm doing this. Set size x = lerp(self.X,0,0.5) to get an object to disappear in time.

    Now, if the object is big this works, otherwise it's almost the same as setting it to zero.

    Now, for example, lerp(40,0,0.5) means that every tick the size goes from 40 to 0 in steps of (40*0.5=20) So basically two ticks to go from 40 to 0?

    Now a tick is not completely reliable, the way I understand If I have 1000 events a tick will last more than say 2 events, am I correct? Is there any way to control this, for instance, do each step every X seconds?

    Thank you

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • I'm just wondering the same thing, wouldn't it be:

    lerp(A,B,0.5*60*dt)

    I usually just do "*60*dt" in an equation if I forgot to add it previously to something. Though the 60 could be exchanged to whatever framerate you're use to seeing.

  • Not exactly. It uses the last part as a percentage.

    So for lerp(self.x,0,0.5), in the first tick it would be 50% of the way between it's own x and zero. then the next tick it would do the same, but since it is now closer, it only goes half the distance this tick. Then the next tick it goes half the distance, etc, etc.

    So then, in reality it will never reach its destination, although it will get awfully close (and rounding errors will see it get there anyway).

    By this logic, lerp(40,0,0.5) will always read 20. because 40 is a constant and not allowed to change. the first value should pretty much always be a variable.

    By the way,

    instead of lerping by like 0.5, it is best practice to lerp by dt * K, where K is a constant (higher = faster lerping.

    Hope I helped a bit.

  • It's still a mystery to me.

    I'm trying to make an object increase it's size from 0 to 600 in an x amount of time.

    System create object at (center of the screen)

    set object size 0,0

    set object size lerp(object.width,600,0.1*dt)

    The object doesn't even show, if I put 0.5 it goes from 0 to 300 in a fraction of a second and stops..

    Why? I don't understand this at all..

  • The lerp expression take the input range made by the two first values and consider it as equivalent to 1, then returns the value that corresponds to the x ratio. In other words, it takes the difference between the first two values, multiply it by the interpolation ratio and adds the result to the first value.

    This is what happens when you set a variable to lerp(variable,600,0.5) every tick, considering that the variable initial value is zero:

    <img src="http://dl.dropbox.com/u/7871870/construct/lerp-01.jpg" border="0" />

    In the first tick the variable will be set to the middle point between the range 0-600, which is 300. In the next tick the lerp will take the current variable value that is 300, and return the middle point of the range 300-600, which is 450. In the third tick the range will be 450-600 and lerp will return the middle point 525, and so on for the other ticks until it reaches 600.

    The rate at which the variable changes is a curve and not a straight line, so you get a deceleration as the variable approaches the destination value.

    If you want a constant rate you need to set constant values to the range and increment the ratio every tick. So you need to do something like:

    +Every Tick

    set width to lerp(0,600,variable)

    set variable to clamp( variable + dt / duration ,0,1)

    Being 'duration' the desired time in seconds to go from 0 to 600. Which means that if duration=2 the width of the object will go from 0 to 600 in 2 seconds.

    The clamp expression is used to ensure that the variable value will always stay between 0 and 1, otherwise the width of the object would increase forever.

  • Thank you, really thank you, I finally understand, you should make a tutorial with this information, otherwise will be lost in time (like tears in the rain <img src="smileys/smiley2.gif" border="0" align="middle" /> )

  • Animmaniac - yes, definitely tutorial material. Superb graphics - they deserve a wider audience.

  • Yep, this would make a great tutorial.

  • here's a little tutorial (including an interactive exe) on lerp

    lerp tut

    it also contains a link to an interactive exe with an example usage of lerp, qarp, and cubic.

  • How do i...

    I have sprite, when i press and hold for example 'q' my sprite smoothly increased two times but no more?

  • increased two times in size? delgado

    also Animmaniac, I guess I just replied to this thread last time without reading other replies, cuz I didn't see that graphic last time. it really does look great/tutorial material

  • If you want a constant rate you need to set constant values to the range and increment the ratio every tick. So you need to do something like:

    +Every Tick

    >set width to lerp(0,600,variable)

    >set variable to clamp( variable + dt / duration ,0,1)

    Being 'duration' the desired time in seconds to go from 0 to 600. Which means that if duration=2 the width of the object will go from 0 to 600 in 2 seconds.

    The clamp expression is used to ensure that the variable value will always stay between 0 and 1, otherwise the width of the object would increase forever.

    I have been studying in the manual and forum posts about the lerp function. I understand it much better now and want to thank those that have helped the new Construct 2 learners like me. I now can use lerp function effectively in my project. Yet, I was wanting to make my movements and scaling actions work at a constant rate and found the very helpful post by Animmaniac regarding this issue.

    I applied the clamp( variable + dt / duration ,0,1) function to accomplish this task, but it did not cause the animation 2 seconds to complete as stated. I tested several options and found that the divided by operator "/" in the formula

    "clamp( variable + dt / duration ,0,1)"

    did not cause the scaling over 2 seconds as stated.

    However, when I changed it to exponential "^" operator

    "clamp( variable + dt ^ duration ,0,1)"

    and it worked correctly. I am not sure why it works and the original "/" does not, but now my lerp move and scale actions now work at a constant rate. I hope my post does not confuse anyone if I am wrong in my approach to getting this to work the way I need it to.

  • Well I have to take everything I stated above back. I does not work with any other values because they grow exponentially and make it slow down to much. Back to the drawing board or Construct 2 animation manual. All I want is to control an animations rate of change over a fixed time frame of my choosing. If I enter 1, 2, or 3 seconds for the time frame that is what I want. Why is that so hard. Clamp is the answer, but it does not function with any consistency. Is it the min, max values that need to be set differently for different time frames?

  • Alright, I really feel dumb. My first post and I make a novice gaff and make the simple hard. My problem was I used a global variable in the clamp function that I did not reset. This caused the next clamp function to not start at 0 but at the last value it ended at. So the lesson is "Make sure you track your variables because if you don't you will get variable results.

    Sorry Animmaniac for not trusting your expertise!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)