Search code examples
javaanimationlibgdxtranslate-animationrotateanimation

Is it possible to create a simple animation just with translation and rotation in libGDX?


I'm trying to develop a very simple game using libGDX with boxes (so 3D game) moving and rotating.

I have almost everything ready, but I'm not able to animate my boxes. I mean, when I touch the screen, I'd like my cube to move to the right by rotating 90 degrees and translating 1 (unit) to the right. As result, the right side of the box will be the new base, the old base will be in the left side, and the box is moved to the right.

So, the question is: now that I have the move set correctly (I or at least I hope so), but change is applied immediately; so how can I see the animation between first position and second position ?

Only reference to animation for 3D objects in documentation is about using obj files from blender (and similar), and for movement I need I do not consider it necessary.

Can anybody provide me some help? Thanks in advance!!


Solution

  • You can do that something like this:

    public static class YourAnimation {
        public ModelInstance instance;
        public final Vector3 fromPosition = new Vector3();
        public float fromAngle;
        public final Vector3 toPosition = new Vector3();
        public float toAngle;
        public float speed;
        public float alpha;
        private final static Vector3 tmpV = new Vector3();
    
        public void update(float delta) {
            alpha += delta * speed;
            if (alpha >= 1f) {
                alpha = 1f;
                // TODO: do whatever you want when the animation if complete
            }
            angle = fromAngle + alpha * (toAngle - fromAngle);
            instance.transform.setToRotation(Vector3.Y, angle);
            tmpV.set(fromPosition).lerp(toPosition, alpha);
            instance.transform.setTranslation(tmpV);
        }
    }
    
    YourAnimation animation = null;
    
    void animate(ModelInstance instance) {
        animation = new YourAnimation();
        animation.instance = instance;
        animation.instance.transform.getTranslation(animation.fromPosition);
        animation.toPosition.set(animation.fromPosition).add(10f, 10f, 10f);
        animation.fromAngle = 0;
        animation.toAngle = 90f;
        animation.speed = 1f; // 1 second per second
        animation.alpha = 0;
    }
    
    public void render() {
        final float delta = Math.min(Gdx.graphics.getDeltaTime(), 1/30f);
        if (animation != null)
            animation.update(delta);
        // render model as usual etc.
    }
    

    Ofcourse this is just a quick example. The actual implementation will vary depending on the use case. For example you could also extend ModelInstance and keep track of the animation in there. Because it is very specific to the use-case, but very simple to implement, it is usually not worth using tools (like the Universal Tween Engine)

    Here is another example I recently wrote for my latest tutorial, perhaps it helps as well. It rotates and moves the cards in this video.