# How do I use Unlerp or invert linear ?

Get help using Construct 2

### » Thu Nov 05, 2015 4:27 pm

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 ?
twitch.tv/neozoom/
Dev stream from time to time !
B
6
S
2
G
2
Posts: 52
Reputation: 969

### » Thu Nov 05, 2015 4:44 pm

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)
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

### » Thu Nov 05, 2015 4:47 pm

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 ? ^.-
twitch.tv/neozoom/
Dev stream from time to time !
B
6
S
2
G
2
Posts: 52
Reputation: 969

### » Thu Nov 05, 2015 4:48 pm

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
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

### » Thu Nov 05, 2015 4:49 pm

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?
twitch.tv/neozoom/
Dev stream from time to time !
B
6
S
2
G
2
Posts: 52
Reputation: 969

### » Thu Nov 05, 2015 5:30 pm

Because you understand that in my example I cannot have the "y" value. Thats why i use lerp on the decceleration part.
twitch.tv/neozoom/
Dev stream from time to time !
B
6
S
2
G
2
Posts: 52
Reputation: 969

### » Thu Nov 05, 2015 6:08 pm

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.
twitch.tv/neozoom/
Dev stream from time to time !
B
6
S
2
G
2
Posts: 52
Reputation: 969

### » Thu Nov 05, 2015 7:21 pm

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)
B
95
S
34
G
128
Posts: 5,494
Reputation: 81,676

### » Thu Nov 05, 2015 9:22 pm

WeneW wrote: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.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788