How do I lerp to integers?

  • I'm trying to make some basic cutscenes for my game, moving the camera sround to show stuff happening. Certain conditions are triggered when the camera reached a specified position. I'm trying to lerp from the previous camera position (in this specific example, camera.X = 470), at which time the next sequence of the cutscene triggers. However, for some reason, the lerp stops at 470.00000000006, so the event is never triggered.

    Is there any way around this? I have pixel rounding enabled in project settings, which I figured would prevent this, but I guess not. Any help would be appreciated.

  • A simple workaround would be to do all lerping with another variable and then set camera.X to round(newVariable).

  • A simple workaround would be to do all lerping with another variable and then set camera.X to round(newVariable).

    That wouldn't really change anything. The lerp would still go to 13 decimal places out (every lerp in the project is doing that now that I look at it), and setting the camera would just snap it into place, which isn't what I want to do.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Try something like this:

    Set X to min(lerp(self.X, 470.5, 0.3), 470)

  • > A simple workaround would be to do all lerping with another variable and then set camera.X to round(newVariable).

    That wouldn't really change anything. The lerp would still go to 13 decimal places out (every lerp in the project is doing that now that I look at it), and setting the camera would just snap it into place, which isn't what I want to do.

    Yes the lerp would still go to 13 decimal places but the X position would be rounded to the nearest integer, so your event would actually fire.

  • or just if camera X = or > than 470 then trigger

  • Looks like a classical numerical precision problem. Often when computers perform maths on non integer values a small amount of error will creep in. For instance if you add 0.1 and 0.2 in most languages you will get 0.30000000000000004. It's generally better to structure things to avoid needing this sort of check, but there's a well known solution when it does happen.

    You can use an appropriate epsilon ( or fudge factor if you like ) in the check.

    So replace your condition with something like:

    abs(470 - camera.X) < 0.01
    

    The "abs" will allow the camera to be a little less, or little than the target.

  • Hey, thanks everyone for the super helpful advice! I ended up going with dop200's suggestion, but there's lots of great stuff to keep in mindd here for the future!

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