Why Universal Tween Engine Boosts Game Performance

Written by

in

The Universal Tween Engine by Aurelien Ribon is a highly flexible Java library that lets you interpolate (animate) any attribute of any Java object. It is heavily used in game development frameworks like libGDX and Android applications because it is built to maximize performance and avoid allocating objects during runtime.

To get started with the engine, you need to follow a simple three-step process: implement an accessor, register it, and update the manager in your game loop. 1. Implement the TweenAccessor Interface

The engine is “universal” because it doesn’t know anything about your specific objects. You teach it how to read and write your object’s attributes by implementing the TweenAccessor interface.

For example, if you want to animate a custom Sprite or position vector:

import aurelienribon.tweenengine.TweenAccessor; public class MySpriteAccessor implements TweenAccessor { // Define IDs for the types of animations you want to support public static final int POSITION_XY = 1; public static final int ALPHA = 2; // 1. Get the current values from your object @Override public int getValues(MySprite target, int tweenType, float[] returnValues) { switch (tweenType) { case POSITION_XY: returnValues[0] = target.getX(); returnValues[1] = target.getY(); return 2; // Number of values modified case ALPHA: returnValues[0] = target.getAlpha(); return 1; default: assert false; return -1; } } // 2. Apply the modified values back to your object @Override public void setValues(MySprite target, int tweenType, float[] newValues) { switch (tweenType) { case POSITION_XY: target.setPosition(newValues[0], newValues[1]); break; case ALPHA: target.setAlpha(newValues[0]); break; default: assert false; break; } } } Use code with caution. 2. Register Your Accessor

Before running any animations, you must register your custom accessor class with the engine. This is typically done during your application initialization phase.

Tween.registerAccessor(MySprite.class, new MySpriteAccessor()); Use code with caution. 3. Create a TweenManager and Run Your Animations

The TweenManager handles updating all active animations. You must instantiate it and feed it your engine’s delta time inside your continuous render/update loop. The Main Setup Loop

public class GameScreen { private TweenManager tweenManager = new TweenManager(); private MySprite playerSprite = new MySprite(); public void init() { // Simple one-line animation definition // Arguments: (Target Object, Animation Type ID, Duration in seconds) Tween.to(playerSprite, MySpriteAccessor.POSITION_XY, 1.0f) .target(300, 400) // Target X and Y coordinates .ease(Elastic.INOUT) // Apply Robert Penner’s Easing Functions .delay(0.5f) // Optional delay before starting .start(tweenManager); // Hand off control to the manager } public void render(float deltaTime) { // CRITICAL STEP: Keep the engine ticking forward tweenManager.update(deltaTime); // Draw your sprite normally here using playerSprite.getX() and getY() } } Use code with caution. Core Animation Types Available

Tween.to(…): Interpolates smoothly from the object’s current value to the target value.

Tween.from(…): Forces the object to start at a specified value and interpolate back to its current state.

Tween.set(…): Instantly changes the attribute value without an animation transition (great when paired with a delay).

Tween.call(…): Triggers a custom callback function, effectively acting as a high-precision timer. Building Complex Animation Timelines

If you need to chain animations sequentially or trigger multiple actions at the exact same time, you use Timeline sequences.

Timeline.createSequence() // Move up, then right sequentially .push(Tween.to(playerSprite, MySpriteAccessor.POSITION_XY, 1.0f).target(0, 100)) .push(Tween.to(playerSprite, MySpriteAccessor.POSITION_XY, 1.0f).target(100, 100)) // Run these animations at the exact same time .beginParallel() .push(Tween.to(enemySprite, MySpriteAccessor.ALPHA, 0.5f).target(0)) .push(Tween.to(playerSprite, MySpriteAccessor.ALPHA, 0.5f).target(1)) .end() // Repeat everything twice with a 0.5-second bounce-back (yoyo) effect .repeatYoyo(2, 0.5f) .start(tweenManager); Use code with caution. Pro-Tip: Memory Management & Pooling

The engine automatically pools Tween and Timeline objects internally. This means when an animation finishes, its object container is recycled. You do not need to worry about the Java Garbage Collector causing frame-rate drops or stuttering during intensive game physics and animation states. AurelienRibon/universal-tween-engine: The … – GitHub

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *