Edited by Hello Darren, 03 April 2012 - 08:54 PM.
Get image_alpha based on distance_to_object
Posted 03 April 2012 - 05:28 PM
Posted 03 April 2012 - 07:45 PM
The first thing you want to realize is that you already have two points--that's right, points. You know that at distance 0, the alpha should be 1, and at distance 35 the alpha should be 0. That gives you the two (x, y) points defined by (0, 1) and (35, 0).
You also want the alpha to change linearly with the distance. This is why it's called a linear regression. Hopefully you know from math classes that any line can be described by an equation of the form y = mx + b, right? Good.
In the form above, m is the slope. The slope of any line is simply rise/run = (y2-y1)/(x2-x1). Remember the first thing I asked you to note? You have two points. Meaning you have all the information you need to find m. In this case, that's (0 - 1) / (35 - 0), so m = -1/35.
Great! So we're reduced to y = -1/35 * x + b. In this scenario, "y" is the alpha and "x" is the distance (since that's how we put them in the original point definitions; you could reverse them just fine as long as you are consistent throughout the process). That means all we need to know now is the value of "b". Can you think of a way to find that?
We already have two pairs of (x, y) values. We know, for example, that when x=0, y=1. So just plug those values in, and "b" will be our only variable, which we can then solve for and get our final, complete equation!
y = -1/35 * x + b
1 = -1/35 * 0 + b
1 = b
Tada! Remembering that, in this case, y is image_alpha and x is distance, you now have your final equation:
image_alpha = -1/35 * distance_to_object(special_obj) + 1;
You might also want to limit that to between 0 and 1, just for when you're farther than 35px away:
image_alpha = median(0, 1, -1/35 * distance_to_object(special_obj) + 1);
Edited by IceMetalPunk, 03 April 2012 - 07:46 PM.
Posted 03 April 2012 - 08:53 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users