How do I stop lerp?

0 favourites
  • 7 posts
  • I'm using lerp quite heavily and while using the debug i can see that some objects actually never stop. (They just move too small steps to be noticeable) My guess is that this could cause a minor performance hit, so how do I stop the lerp when it's close to the destination, like half a pixel or so?

    And is there a good way to limit decimals in calculations?

  • What is the original formula you are using ? What are you lerping, and how ?

    Depending on the situation, you can try round(lerp()).

  • Just basic movement of an sprite, towards another sprite.

    lerp(self.x, object2.x, 0.1)

    will round actually stop it? i was thinking more of having a condition to make the event false if it was close enough, if the distance between object1 and object2 is less than a certain amount ... The point is that I'm trying to optimize for a lot of objects, so i don't want heavy calculations.

    maybe compare condition like this would be suffice, but I'm not sure if distance calculations for a lot of objects is very performance friendly?

    distance(object1.x, object2.x) > 0.5

    lerp(self.x, object2.x, 0.1)

    Or maybe it would be better to add a variable to object 1 which holds the last x postion, then checking if last position x (minus) current position x is less 0.5? or something like that, but I guess i always have to return a positive value then.

    abs(object1.lastpos-object2.x) > 0.5

    lerp(self.x, object2.x, 0.1)

    set object1.lastpos to self.x

    or something like that? lol

    Just specilating Maybe that would work... better try.

  • or something like that?

  • or something like that?

    yeah that could work.

    I tried that but it seemed that Sprite.X never reached exactly 300. it was 299.249859284752975029478520938752095786209587625

    but i didn't think of rounding in the condition :p round(sprite.x) = 300

    way easier.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thats is because you should lerp from start postion to end position. Now you lerp from a in-between-position to the end position. Sprite.X or Self.X is constanty updating. You create a Zeno paradox.

  • Two ways I use regularly.

    Store start and end position in variable and lerp from 0 to 1 with a dt counter/timer for a true lerp (linear).

    Or wrap the fixed step lerp based on current position to destination with ceil() or floor() depending if incrementing or decrementing, for an eased motion. Usually have to make a sub event condition for both because round() only works when lerping with a factor greater than 0.5 per tick.

    Another way I don't use normally is to have the lerp event only run when not within 1-2 pixels of the destination as a condition. I don't use this because I generally like more precise control of the final resting position.

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