How do I use Unlerp or invert linear ?

0 favourites
  • 9 posts
From the Asset Store
Draw the lines and guide the basketball into the ring!
  • Hai !

    I read the manual. But my usage of unlerp doesn't seems correct.

    My blocks conditions organisation seems to work as intended. But unlerp doesn't behave like i thought it should.

    Its for starting and slowing a camera depending on the distance to the AIM point :

    so i use unlerp(start, end, speedfactor) to cameraAIM X during the first "half" of the distance,

    then use lerp(start, end, speedfactor) to cameraAIM X during the second part to slow down.

    did i misunderstood something ?

  • Yes, you misunderstood.

    If you do lerp between 0 and 10 with a factor of 0.6, then you will be at 6. I hope this part is clear? It would be lerp(0, 10, 0.6) = 6

    Now unlerp gives you back the factor of 0.6, if you give it the previous lerp result together with the same starting and end points. (unlerp(0, 10, 6) = 0.6)

  • mmmm i'm bad at math, and still have hard time to get it.

    Do we agree Lerp is a linear decceleration between A,B at the speed "factor" of C right ?

    so scroll camera to AIM : lerp( Start point (A, actual scrollX) , end target (B, AIM position) , 0.1 ) for example

    will go fast and slow down at a ratio of 0.1 till it reaches AIM point. right ?

    So why unlerp doesn't simply does the same but accelerating instead ? ^.-

  • Seems you misunderstood unlerp:

    lerp(a,b,x) =y

    unlerp(a,b,y)=x

    so

    lerp(0, 100, 0.5) = 50

    unlerp(0, 100, 50) = 0.5

    You are trying:

    lerp(0, 100, 0.5) = 50

    unlerp(0, 100, 0.5) = 200

  • In my game, i want the camera to go from its actual position to another, accelerating from start to middle distance, then deccelerating (which works) from the middle to the target Point.

    How can i do this?

  • Because you understand that in my example I cannot have the "y" value. Thats why i use lerp on the decceleration part.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ok, i finally found a solution.

    You can just use lerp by inverting start and end position, and inverting the factor.

    to accelerating : lerp(B, A, 0.9) ;

    to deccellerate : lerp(A, B, 0.1) ;

    for example, just using a trigger to switch between both when you reach halfway.

    EDIT : nahhh it doesn't work. neither.

  • You can't use lerp() to do acceleration. You have two options. One is to use a third party plugin like litetween to do the easing or you can do it with math.

    For the math route to do acceleration you need to keep track of the object's speed. Then using this formula to calculate the stopping distance:

    stoppingDistance = (speed^2)/(2*acceleration)

    You could do this:

    global number acceleration=100

    global number velocityX=0

    global number velocityY=0

    global number targetX=0

    global number targetY=0

    global number angleToTarget=0

    system: compare distance(sprite.x,sprite.y,targetX,targetY) > (distance(0,0,velocityX,velocityY)^2)/(2*acceleration)

    set angleToTarget to angle(sprite.x,sprite.y,targetX,targetY)

    add cos(angleToTarget )*acceleration*dt to velocityX

    add sin(angleToTarget )*acceleration*dt to velocityY

    Else

    set angleToTarget to angle(sprite.x,sprite.y,targetX,targetY)

    add cos(angleToTarget )*-acceleration*dt to velocityX

    add sin(angleToTarget )*-acceleration*dt to velocityY

    Every tick

    sprite: set position to (self.x+velocityX*dt, self.y+velocityY*dt)

  • mmmm i'm bad at math, and still have hard time to get it.

    Do we agree Lerp is a linear decceleration between A,B at the speed "factor" of C right ?

    I would not say that this is the case.

    While you can use lerp for smoothing in onto a value, that's not what lerp explicitly does. Lerp just returns a point between A and B, namely it returns: A + (B - A) * C. The smoothing you know comes from using a variable start point A, which normally is the current value that you are smoothing, so for example an objects x position.

    And the deccelaration of that lerp construction isn't linear, at start you will see a faster slow down until it comes to a not noticable crawl.

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