Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Easing equations #36

Closed
dacrystal opened this issue Sep 5, 2019 · 4 comments
Closed

Expose Easing equations #36

dacrystal opened this issue Sep 5, 2019 · 4 comments
Milestone

Comments

@dacrystal
Copy link

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

@Calinou
Copy link
Member

Calinou commented Sep 5, 2019

There's an ease() global scope function, which should expose parts of what you need. See also godotengine/godot#10572.

@dacrystal
Copy link
Author

dacrystal commented Sep 5, 2019

@Calinou I have used that at first. But it was not enough for what I was doing. Besides, it was confusing to use at the beginning, until I came across godotengine/godot#10572 with its graph.

I suppose the Tween approach gives more control over easing in terms of Transition and Ease types. The icing on the cake, It can ease most of Variant types too .

Calinou added a commit to Calinou/godot that referenced this issue Jun 9, 2020
This can be used as an alternative to `Tween.interpolate_property()`
when additional control is needed.

This closes godotengine/godot-proposals#36.

Co-authored-by: Nasser Alansari <alansari.n@gmail.com>
@Calinou
Copy link
Member

Calinou commented Sep 29, 2021

@KoBeWi Is this proposal still relevant with the Tween rewrite?

@KoBeWi
Copy link
Member

KoBeWi commented Sep 29, 2021

No, you can just do Tween.new().interpolate_variant(a, b, c, d), which is almost the same as built-in func.

@KoBeWi KoBeWi closed this as completed Sep 29, 2021
@KoBeWi KoBeWi added archived and removed archived labels Sep 29, 2021
@KoBeWi KoBeWi modified the milestones: 4.1, 4.0 Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants