# Gradually increase a value by another value increasing from A to B in the BGE

Is there a way to gradually increase a value by how much another value is? Like let’s say when a specific value reaches 10, I want the other value to gradually reach 5. As in the closer value gets to 10 the more the other value increases to 5. Do you know what I am saying?

Thanks!

This is simple school math. Just look for percentage calculation.

Arange = distance of the A values (Amin … Amax)
Avalue = value on Arange

Brange = distance of the B values (Bmin … Bmax)
Bvalue = value on Brange

Arange = Amax - Amin
Brange = Bmax - Bmin

given is Avalue
you look for Bvalue

in difference to percentage calculation you need to take the minimum values into account
the formula is

``````
Bvalue-Bmin   Avalue-Amin
----------- = -----------
Brange         Arange

``````

You resolve to Bvalue:

Bvalue = Bmin + (Avalue-Amin)/Arange*Brange

That is all

Or you could do A = 0.5 * B every frame. If B reaches 10, A will reach 5.

If that is what you want to do.

I’m sorry but I’ve read it like 20 times and cannot figure out what you mean. Can you please give me a blend or something?
thank you! I’m sorry, I’m clueless…

Hopefully Monster doesn’t post before I do.

First of all, you need to know what value is your control variable and which value is dependent on this variable.
Let’s say x is our control variable and y’s value is dependent on x’s value.

We want to make it so that when x = 10, y = 5.

Simply put,
x = Any Number, y = x/2

What ever value x is, we divide it by 2 and we get the y value for it.
x = 1, y = 0.5
x = 5, y = 2.5
x = 6, y = 3
etc
etc

What Monster is doing is setting a range to calculate percentage.

``````x_min = 0 #Minimum value for x is 0
x_max = 10 #Maximum value for x is 10
Thus,
x_range = x_max - x_min
``````

Sometimes the minimum isn’t zero like it is with your situation,
x_min = 10
x_max = 20
The range isn’t from 0 to 20, it’s from 10 - 20 which is 10.

Can you guess what it is for y?

``````y_min = 0
y_max = 5
y_range = y_max - y_min
``````

Now we need to know the percentage of each of the values. How much of the variable is filled.
For example, we know that since the range between, 0 and 2 is 2, 1 would mean that 50% of this range is filled. Right?

0 is the minimum, 2 is the maximum.
2 is the range (2-0)
1 is the value
0.5 is the percent: 1(value) divided by 2(range)

So far we have the range, and now we need the value. Since x has a value that we set, the only value we don’t know is y.
So we know,

``````x_value = (any number we set)
y_value = (affected by number set to x)
``````

Now we have:

``````x_value/(x_range)
y_value/(y_range)
``````

But aren’t we missing something? What if the minimum of x and y were not 0 and instead 5? The equation would fail. Think about it for a bit and you’ll understand why.

``````A = (x_value-x_min)/x_range
B = (y_value-y_min)/y_range
``````

These percentages must be the same, why? Because when x is half filled, you want y to be half filled. Therefore y is proportionate to x.
And,
A = B

Remember, we still don’t have the value for y, but we have everything else. With basic algebra you arrange it so the y_value is what we are solving for.

Finally,

``````y_value = y_min + (x_value-x-min)/(x_range)*y_range
``````

This is actually how we get x = # and y = x/2

y_value = 0 + (#-0)/10*5

Linkxgl, you blew my mind! haha. I sat there and read it for hours and I think I finally understand it! Correct me if I’m wrong but, doesn’t this just split the x value by half?

Yes, but the point is that you can use it for more things than just x = # and y = x/2.

What if you wanted it to work for intervals between 10 - 22 and 5 - 18?
The formula would be a great use for that.

y_value = 5 + (#-10)/12*13

It’s a universal formula!

Wow, I get it now! Thank you so much, You’re awesome!

Thanks Linkxgl for the nice explaination.

I really had no idea how to explain the formula without writing another huge (and mostlikely more confusing) post

Thanks