0 votes

How we link two numerical variables together with inversely proportional quantities?

Same in math, when SPEED goes up, TIME to reach destination shortens.

The application would be a Lens Flare Sprite fading from the camera, the idea is to decrease visibility based on distance from the camera.

I could manually do:

match Distance:
     10: set_modulate(Color(1,1,1,1.0))
     20: set_modulate(Color(1,1,1,0.9))
     30: set_modulate(Color(1,1,1,0.8))
     40: set_modulate(Color(1,1,1,0.7))
     50: set_modulate(Color(1,1,1,0.6))
     60: set_modulate(Color(1,1,1,0.5))

But heck no, that's a retrocess.

I'm sure there's a solution, and this question could be applied to multiple different cases.

in Engine by (369 points)

2 Answers

+1 vote
Best answer

It looks like you want to map a number in one range to a number in a different range. That's exactly what lerp_range does. For the above example, something like the below should do what you want...

Basically, this maps a number in the range of 10 to 100 to a number in the range of 1 to 0.1

Using your above Distance variable, this should map to the correct output value.

var mappedVal = range_lerp(Distance, 10, 100, 1, .1)

by (10,932 points)
selected by
0 votes

I'm not familiar with how lens flares work, but is it possible to calculate the range of the flare distances? A minimum and maximum distance from the camera, then you could convert that into a float between 0 and 1 that you could simply plug into the modulate.

by (370 points)

It was just a sprite, and I was using distance_to to the camera2d.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.