48 lines
1.4 KiB
GDScript
48 lines
1.4 KiB
GDScript
extends ScrollDamper
|
|
class_name ExpoScrollDamper
|
|
|
|
|
|
## 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)
|
|
|
|
## Factor to use in formula
|
|
var _factor := 10000.0:
|
|
set(val): _factor = max(val, 1.000000000001)
|
|
|
|
## Minumun velocity.
|
|
@export_range(0.001, 100000.0, 0.001, "or_greater", "hide_slider")
|
|
var minimum_velocity := 0.4:
|
|
set(val): minimum_velocity = max(val, 0.001)
|
|
|
|
|
|
func _calculate_velocity_by_time(time: float) -> float:
|
|
var minimum_time = _calculate_time_by_velocity(minimum_velocity)
|
|
if time <= minimum_time: return 0.0
|
|
return pow(_factor, time)
|
|
|
|
|
|
func _calculate_time_by_velocity(velocity: float) -> float:
|
|
return log(abs(velocity)) / log(_factor)
|
|
|
|
|
|
func _calculate_offset_by_time(time: float) -> float:
|
|
return pow(_factor, time) / log(_factor)
|
|
|
|
|
|
func _calculate_time_by_offset(offset: float) -> float:
|
|
return log(offset * log(_factor)) / log(_factor)
|
|
|
|
|
|
func _calculate_velocity_to_dest(from: float, to: float) -> float:
|
|
var dist = to - from
|
|
var min_time = _calculate_time_by_velocity(minimum_velocity)
|
|
var min_offset = _calculate_offset_by_time(min_time)
|
|
var time = _calculate_time_by_offset(abs(dist) + min_offset)
|
|
var vel = _calculate_velocity_by_time(time) * sign(dist)
|
|
return vel
|