LibGDX interpolation

So, I have a sprite and in render(), I want to interpolate its alpha value from 0 to 1 to create a fading effect.

This is my current code trying to follow examples without having (or being able to find) an understanding of what MathUtils.clamp does. This code does not work. The alpha values generated start deep in the negatives.

		time += Gdx.graphics.getDeltaTime();
		float alpha = new Interpolation.Swing(1).apply(0, 1, MathUtils.clamp(time / 4.5f, 0, 1));
		System.out.println(alpha);
		sprite.setAlpha(alpha);

Maybe its your method problem? Try printing


System.out.println(MathUtils.clamp(time / 4.5f, 0, 1));

instead of the value that the Interpolation generates.

That gives (seemingly) the same values that were being generated by the entire line

If you want to fade in (linear interpolation implied), then I’m not sure why you’re using Interpolation.Swing. Secondly, you should probably learn how to use google, it’s a powerful tool and will save you a lot of time. API

I did Google before I asked this question, and when I could find no answer, I asked here.

I actually found the exact docs that you sent, but they are lacking an explanation of the method I was using…

I realized what was happening. Swing must bounce a few above/below each value to give it a different effect. It just occurred to me that they each represent things like that.

Sorry for being new and trying to read documentation that pretty much only states the class’ name, and nothing more.

I still have no idea how MathUtils.clamp() works, or how these interpolation things work on the under-side, but I guess I will just use them blindly.

Second google search link gives you the code on github (literally 2 lines). If you’re using something blindly it’s your own fault.

Clamp will (or should) constrain your value between the min and max. I.e. if your value is less than min, clamp will return min. If value is greater than max, it will return max. If it is inbetween min and max, it will just return value.

As for the interpolation, I’m assuming that your ‘time’ variable is the time elapsed in the animation? And that it ranges between 0.0 and 4.5 (whatever units it’s in)? I’ve got no idea what this Interpolation.Swing business is, but if time ranges between 0 and 4.5, then your interpolated alpha value is simply time / 4.5f. If you want to ‘invert’ this, then the value would be 1.0f - time / 4.5f;

In a more general sense you can interpolate anything linearly by using currentValue / maximumValue;, where currentValue is somewhere between 0 and maximumValue. If you need a value in a range other than 0.0 to 1.0, for example 0 - 255, then you would multiply this interpolated value by 255 (or the maximum of the desired range).

Where? You seem angry, and I’m sorry, but I am just trying to learn. For me, a fairly reasonable Google search just gave me documents that had no explanation of what their methods do.

I can read these documents, and see the structure, yet cannot understand their functionality due to a lack of description. I do not understand the concepts behind interpolation and various aforementioned math utility methods. So, since the official documents where no help in aiding my understanding of these concepts, I asked here. But if it is wrong of me to ask here when I do not understand something and I misunderstood the purpose of this section of the forum, I can refrain from doing so from now on.

I don’t mean to come across as angry, it’s just that I expect forum members to at least try to solve their own problems before posting about them. In this case, I don’t see how you intend to learn what

Interpolation.Swing(1).apply

does if you’re googling “How to interpolate libGDX”. Besides, most interpolation types are pretty self-explanatory.

Because I was trying to understand how interpolation, the concept, works in LibGDX. I was only using the Swing statement from earlier because it was something I found on a random example, after not being able to find any in depth explanations of interpolation in LibGDX. But I will keep looking.

Interpolation in libgdx, conceptually, is the same as interpolation anywhere else. I’m not sure if you don’t understand interpolation as a concept, or how libgdx implements it.

I do not (or barely) know the concept of interpolation as a whole. But I figured/assumed it may be implemented different in other frameworks, so I searched specifically for guides in LibGDX. But if interpolation is similar throughout various frameworks, I will simply Google more generally.

Ok let me help you out. First lets talk about clamp.

there are many clamp methods, for different types. let’s take the float example since it’s the most common one.

	static public float clamp (float value, float min, float max) {
		if (value < min) return min;
		if (value > max) return max;
		return value;
	}

value is the initial value, min is the lowest you want it to be, max is the highest you want it to be.
so let’s say I am getting some float value that tends to be around 0f to 1.5f, but I don’t want any values to go over 1.0f, then I will call


float myValue = ... /// tends to be in 0f to 1.5f range
float myClampedValue = MathUtils.clamp(myValue, 0f, 1.0f); // guaranteed to be between 0f and 1.0f

Now lets talk about Interpolation. The interpolation methods work based on a provided alpha value. an alpha value of 0f means that the interpolation is at the very beginning of its track, a value of 1.0f means it is at the very end. There are many different interpolation methods in libgdx and you can learn more about them from this video: