Skip to content

Expose Easing equations #36

@dacrystal

Description

@dacrystal

Godot Version:
(3.1.1-stable)

Describe the project you are working on:
The project I'm working on required the use of easing functions(the compute part of it) and not whole Tweening.

Describe how this feature / enhancement will help your project:
Godot already implemented Easing functions('interpolaters') internally for the use within Tween, but they are not exposed. I believe it would be helpful to have them expose

Describe implementation detail for your proposal (in code), if possible:
I have already patched the Godot source to expose this functionality(see below). It is working for me (at least for my use-cases).

I have added a method to Tween class and bind it, but it could be part of Math funcs or another namespace/class.

//  tween.cpp
Variant Tween::interpolate(Variant p_initial_val, Variant p_final_val, real_t p_elapsed, TransitionType p_trans_type, EaseType p_ease_type) {
	

	// convert INT to REAL is better for interpolaters
	if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t();
	if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t();

	ERR_FAIL_COND_V(p_initial_val.get_type() == Variant::NIL, false);
	ERR_FAIL_COND_V(p_initial_val.get_type() != p_final_val.get_type(), false);
	ERR_FAIL_COND_V(p_elapsed < 0, false);
	ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false);
	ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false);


	InterpolateData data;
	data.type = INTER_PROPERTY;
	data.elapsed = p_elapsed;
	data.initial_val = p_initial_val;
	data.final_val = p_final_val;
	data.duration = 1.0;
	data.trans_type = p_trans_type;
	data.ease_type = p_ease_type;
	data.delay = 0;

	ERR_FAIL_COND_V(!_calc_delta_val(data.initial_val, data.final_val, data.delta_val), false)
	
	return _run_equation(data);
}

void Tween::_bind_methods() {
	...
	ClassDB::bind_method(D_METHOD("interpolate", "initial_val", "final_val", "elapsed", "trans_type", "ease_type"), &Tween::interpolate);
	...
}
# Example
var tween = Tween.new()
print(tween.interpolate(0, 1, 30.0/60, Tween.TRANS_LINEAR, Tween.EASE_IN))
print(tween.interpolate(0, 1, 30.0/60, Tween.TRANS_QUAD, Tween.EASE_IN))
print(tween.interpolate(5, 20, 30.0/60, Tween.TRANS_LINEAR, Tween.EASE_IN))
print(tween.interpolate(5, 20, 30.0/60, Tween.TRANS_QUAD, Tween.EASE_IN))

# Output
0.5
0.25
12.5
8.75

If this enhancement will not be used often, can it be worked around with a few lines of script?:
One might reimplement the easing functions in pure GDScript. I initially did that for some of the needed easing equation.

Is there a reason why this should be core and not an add-on in the asset library?:
Most of the code already implemented in the core and it is a matter of exposing it

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions