# Get image_alpha based on distance_to_object

2 replies to this topic

### #1 Hello Darren

Hello Darren

Pirate

• Banned Users
• 275 posts
• Version:Unknown

Posted 03 April 2012 - 05:28 PM

I want it so when the player is standing right next to this special object it's image alpha is 1 but when the player is 35 pixels away the object's alpha is 0 with the alpha also being set in between. But I want the image_alpha to be linked directly with the distance, how would I do this? I'd also want to change the formula based on the objects that are close, so for example another object would increase it's alpha more even if it's the same distance with another object.

Edited by Hello Darren, 03 April 2012 - 08:54 PM.

### #2 IceMetalPunk

IceMetalPunk

InfiniteIMPerfection

• Retired Staff
• 9259 posts
• Version:Unknown

Posted 03 April 2012 - 07:45 PM

It's a basic (mathematical) linear regression you're looking for. Since this is something that you'll probably end up using for many things in the future, I'll explain in detail the general process.

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);`

-IMP

Edited by IceMetalPunk, 03 April 2012 - 07:46 PM.

• 2

### #3 Hello Darren

Hello Darren

Pirate

• Banned Users
• 275 posts
• Version:Unknown

Posted 03 April 2012 - 08:53 PM

Wow thanks that works great! Though I don't understand why it works lol

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users