Expression needed! Maybe i've missed one?

Get help using Construct 2

Post » Sun Jun 10, 2012 7:20 pm

We have lerp - for example: lerp(a, b, c)=d
So if i have a, b, d - how should i calcutate c?

Example: i have a timer that ticks from 3 to 0. When the clock is on 1.5 that expression should return 0.5, when the clock is on 0, the expression should return 1.

Is there already an expression on this? Or maybe you can give me a solution?NugMan2012-06-10 19:21:10
B
19
S
6
G
4
Posts: 171
Reputation: 2,512

Post » Sun Jun 10, 2012 7:32 pm

I apologise - it's 1 - 1/x.
...right?
B
19
S
6
G
4
Posts: 171
Reputation: 2,512

Post » Sun Jun 10, 2012 7:43 pm

Not quite. lerp(a, b, c) = a + c * (b - a) = d
To calculate c from a, b and d, you'd rearrange to solve for c:
a + c * (b - a) = d
c * (b - a) = d - a
c = (d - a) / (b - a)

e.g. if lerp(10, 20, 0.5) = 15
then the other way round, given a = 10, b = 20 and d = 15:
c = (15 - 10) / (20 - 10)
c = 5 / 10
c = 0.5

I'm wondering if an "unlerp" system expression would be a useful addition. So unlerp(a, b, d) would return c, e.g. unlerp(10, 20, 15) = 0.5Ashley2012-06-10 19:43:55
Scirra Founder
B
395
S
232
G
88
Posts: 24,371
Reputation: 193,762

Post » Sun Jun 10, 2012 8:47 pm

Google said that "unlerp" is already in use in some scripting languages. I think it would be usefull. I vote Yes.
B
19
S
6
G
4
Posts: 171
Reputation: 2,512


Return to How do I....?

Who is online

Users browsing this forum: David181, Laurent, OBLIVION and 55 guests