Files
leonwww/addons/SmoothScroll/scroll_damper/linear_scroll_damper.gd
2025-07-23 15:23:32 +03:00

34 lines
826 B
GDScript

extends ScrollDamper
class_name LinearScrollDamper
## Friction, not physical.
## The higher the value, the more obvious the deceleration.
@export_range(0.001, 10000.0, 0.001, "or_greater", "hide_slider")
var friction := 4.0:
set(val):
friction = max(val, 0.001)
_factor = pow(10.0, friction) - 1.0
## Factor to use in formula
var _factor := 10000.0:
set(val): _factor = max(val, 0.000000000001)
func _calculate_velocity_by_time(time: float) -> float:
if time <= 0.0: return 0.0
return time * _factor
func _calculate_time_by_velocity(velocity: float) -> float:
return abs(velocity) / _factor
func _calculate_offset_by_time(time: float) -> float:
time = max(time, 0.0)
return 1.0/2.0 * _factor * time*time
func _calculate_time_by_offset(offset: float) -> float:
return sqrt(abs(offset) * 2.0 / _factor)