Interpolation
In games, things rarely just jump from point A to point B. Good games use smooth movement—characters walk, enemies bounce, menu buttons fade in, coins spin. Interpolation is the technique that makes this possible. It is a technique used to smoothly change a value over time.
Instead of instantly changing a value, interpolation lets a value gradually change over time, for example:
- Moving an object from x = 0 to x = 500 in small steps in 2 seconds
- Fading opacity from 0 (invisible) to 1 (fully shown)
- Growing size from small to large
Why Not Just Do It Manually?
You could try to animate manually like this:
This is a linear (same speed) animation. But often we want to have a more natural movement. For example, objects that are falling in real life get affected by gravity and speed up during the fall. A linear drop would feel robotic instead of natural.
Creating smooth motion manually requires complex math (curves, easing functions), which you can implement yourself. But we already did that for you!
With interpolation, you only need to define:
- A start value
- An end value
- A duration (how long it should take)
- An interpolation strategy (how the movement feels: linear, bounce, smooth, etc.)
Available interpolation strategies
The following interpolation strategies are currently available in the Saxion Game App:
| Strategy | Description |
|---|---|
linear |
Moves at a constant speed. No easing. Very robotic. |
fade |
Starts fast and slows smoothly at the end (soft stop). |
pow2 |
General power curve (uses both pow2in and pow2out). |
pow2in |
Starts slow and speeds up (acceleration). |
pow2out |
Starts fast and slows down (deceleration). |
pow3 |
Stronger version of pow2 (more curve). |
pow3in |
Starts very slow, speeds up strongly. |
pow3out |
Starts very fast, slows down strongly. |
pow4 |
Even stronger acceleration/deceleration curve. |
pow4in |
Very slow start, then very fast. |
pow4out |
Very fast start, strong slow finish. |
pow5 |
Extremely strong curve for dramatic motion. |
pow5in |
Extremely slow start → very rapid finish. |
pow5out |
Extremely fast start → very soft stop. |
exp10 |
Uses base-10 exponential curve (very sharp). |
exp10in |
Almost starts still, then explodes fast. |
exp10out |
Starts extremely fast, almost teleports, then stops. |
exp5 |
Softer exponential curve than exp10. |
exp5in |
Starts very, very slow → speeds up. |
exp5out |
Blasts fast → slows at the end. |
sine |
Smooth sine wave easing (in & out). |
sinein |
Smooth, gentle start (like slowly waking up). |
sineout |
Smooth, gentle end (like slowly coming to rest). |
circle |
Circular curve (natural, arched motion). |
circlein |
Starts slow and curves into high speed. |
circleout |
Starts fast then curves gently to stop. |
elastic |
Overshoots and wiggles like a rubber band. |
elasticin |
Pulls back, snaps forward with spring. |
elasticout |
Overshoots at the end, bounces back. |
bounce |
Falls and bounces at the end. |
bouncein |
Starts with small bounces before rising. |
bounceout |
Drops, hits, then bounces to rest. |
You can get all strategies in the code by using the getInterpolationStrategies() method.
Creating animations with interpolators
You don’t animate objects directly. Instead, you create a named interpolator, update it each frame, and use the returned value for movement, size, opacity, etc.
1. Creating an interpolator
Parameters:
- "moveX" = unique name for this interpolator
- 0f = initial value for the interpolator
- 500f = the value we are moving towards
- 2f = duration in seconds
- "bounce" = interpolation strategy
2. Update it every frame
The updateInterpolator(String name) method should be called in the render(float delta) method. You don't have to pass the delta to the interpolator (the interpolator has another way to get the delta value).
You use the currentX value to draw your object in this frame.
3. Check when it’s done
We can detect when the interpolator is finished. This is something we usually do in the render(float delta) method.
4. Reset (Play Again)
When the animation is finished, we can decide what we want to do next. We can leave the animation object like it is. Since the animation is finished, the GameApp.updateInterpolator(String name) method will always return the end value of the interpolator.
We can also choose to reset the animation, reverse it or schedule another animation with the same name.
We can also reverse the animation:
Other methods
hasInterpolator(name); // Check if an interpolator exists with a given name
disposeInterpolator(name); // Remove an interpolator
getInterpolator(Sname); // Get the Interpolator object (for advanced usage)
Example
The following example shows how we can move an enemy from left to right with a pow2Out interpolation strategy. We reverse the animation when the enemy reaches the endpoint.
public void show() {
// Start animation left to right in 3 seconds
GameApp.addInterpolator("enemyMove", 0f, 1000f, 3f, "pow2Out");
}
public void render(float delta) {
super.render(delta);
// Update the interpolator and get the current value
float pos = GameApp.updateInterpolator("enemyMove");
// Update the enemy position, based on the interpolator
enemy.setX(pos);
// When the interpolator is finished, reverse the animation
if (GameApp.isInterpolatorFinished("enemyMove")) {
GameApp.resetInterpolator("enemyMove", true);
}
}