fading alpha based on distance between two objects

Hey, how can I make so that a drawn object's (obj_storm_effect) alpha fades based on the distance between two objects (obj_player and obj_storm) ?
So that the alpha is 0 when obj_player and obj_storm are far from one another and alpha is 1 when close?
thank you!
 

Nidoking

Member
point_distance or distance_to_object gives you the distance
Depending on how you draw the sprite, you either set image_alpha or change the alpha value you use.
 

ZeGGamer

Member
Ok, this is just my gut reaction, but you could do something simple with variable management. I would personally do this with 1 if-else statement loop, with two general clamp areas

//If player is close to the storn
if(distance_to_object(obj_player) < full_cloud_distance_variable_value)
{
image_alpha = 1;
}
else if(distance_to_object(obj_player) > minimum_cloud_distance_veriable_value)
{
Image_alpha = 0;
}
else
{
// Now this is where it gets tricky, and you need to create an equation that gradually diminishes the alpha based on a formula. I'm no math expert, but this a starting point I would use before tweaking values to work with your game.

image_alpha = distance_to_object(obj_player)/transparency_variable.
}

The transparency variable would be a Constance to help bring the image_alpha to a decimal point. this is dependent on your pixel/game size.
Hope this helps!
 
Ok, this is just my gut reaction, but you could do something simple with variable management. I would personally do this with 1 if-else statement loop, with two general clamp areas

//If player is close to the storn
if(distance_to_object(obj_player) < full_cloud_distance_variable_value)
{
image_alpha = 1;
}
else if(distance_to_object(obj_player) > minimum_cloud_distance_veriable_value)
{
Image_alpha = 0;
}
else
{
// Now this is where it gets tricky, and you need to create an equation that gradually diminishes the alpha based on a formula. I'm no math expert, but this a starting point I would use before tweaking values to work with your game.

image_alpha = distance_to_object(obj_player)/transparency_variable.
}

The transparency variable would be a Constance to help bring the image_alpha to a decimal point. this is dependent on your pixel/game size.
Hope this helps!
thanks for replies,
I actually managed to do some digging and found that this code works to an extent:
GML:
image_alpha = max(0, min(1, 1 - point_distance(instance_nearest(x,y,obj_player).x, instance_nearest(x,y,obj_player).y, instance_nearest(x,y,obj_storm).x, instance_nearest(x,y,obj_storm).y) / 99));
BUT if there are multiple instances of obj_storm simultaneously in the room it messes things up. Then it seems to not work anymore, the alpha doesnt start fading when obj_player goes near, it only works with one instance in the room if I'm entirely correct... how to work around this?
 

FrostyCat

Redemption Seeker
GML:
image_alpha = clamp((distance_to_object(obj_player)-full_cloud_distance)/(min_cloud_distance-full_cloud_distance), 0, 1);
If at this point any of you have not heard of what linear interpolation is, learn how to recognize and use it NOW. Not learning it now means you'll spend needless hours on basic visual effects and various common geometric problems that experienced users handle in seconds.
 
GML:
image_alpha = clamp((distance_to_object(obj_player)-full_cloud_distance)/(min_cloud_distance-full_cloud_distance), 0, 1);
If at this point any of you have not heard of what linear interpolation is, learn how to recognize and use it NOW. Not learning it now means you'll spend needless hours on basic visual effects and various common geometric problems that experienced users handle in seconds.
thanks, but I don't fully understand the code, does it work on the obj_storm_effect controller, and if I put it in the controller object then it should have references to both obj_player and obj_storm? What do the 'cloud_distance' statements do?
 

FrostyCat

Redemption Seeker
If you want to work from obj_storm_effect (I originally assumed obj_storm), then you can do this:
GML:
var dist;
with (obj_player) {
    dist = distance_to_object(obj_storm);
}
image_alpha = clamp((dist-full_cloud_distance)/(min_cloud_distance-full_cloud_distance), 0, 1);
Notice that the linear interpolation formula is still the same.

The cloud distance variables are the distances at which you want the alpha to be 0 (full_cloud_distance) and 1 (min_cloud_distance).
 
If you want to work from obj_storm_effect (I originally assumed obj_storm), then you can do this:
GML:
var dist;
with (obj_player) {
    dist = distance_to_object(obj_storm);
}
image_alpha = clamp((dist-full_cloud_distance)/(min_cloud_distance-full_cloud_distance), 0, 1);
Notice that the linear interpolation formula is still the same.

The cloud distance variables are the distances at which you want the alpha to be 0 (full_cloud_distance) and 1 (min_cloud_distance).
got it, yeah, obj_storm and obj_storm_effect were intended as separate objects entirely. sorry for the confusion. i will try this one out.
 
If you want to work from obj_storm_effect (I originally assumed obj_storm), then you can do this:
GML:
var dist;
with (obj_player) {
    dist = distance_to_object(obj_storm);
}
image_alpha = clamp((dist-full_cloud_distance)/(min_cloud_distance-full_cloud_distance), 0, 1);
Notice that the linear interpolation formula is still the same.

The cloud distance variables are the distances at which you want the alpha to be 0 (full_cloud_distance) and 1 (min_cloud_distance).
are these GMS2 functions? I can't get it to work with full_cloud_distance etc. I'm using GMS1.4.
 

FrostyCat

Redemption Seeker
These are all just custom variable names, not built-in functions. You have to assign or substitute the values yourself.

I don't see any GMS 2 specific content in the code I provided.
 
Last edited:
tried it and it doesn't seem to change the alpha in any way whether its near or far an obj_storm... i'm doing it from the controller object (obj_storm_effect) as follows:

GML:
var dist;
with (obj_player) {
    dist = distance_to_object(obj_storm);
}
image_alpha = clamp((500)/(50), 0, 1);
 
Top