繁体   English   中英

是否可以仅通过libGDX中的平移和旋转来创建简单的动画?

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

我正在尝试使用libGDX开发一个非常简单的游戏,并带有可移动和旋转的盒子(因此是3D游戏)。

我几乎已经准备好所有东西,但是我无法为盒子动画。 我的意思是,当我触摸屏幕时,我希望我的立方体通过旋转90度并向右平移1(单位)来向右移动。 结果,框的右侧将是新的底部,旧的底部将在左侧,并且框将移到右侧。

因此,问题是:现在我已经正确设置了移动(我或者至少希望如此),但是更改立即应用; 那我怎么看第一位置和第二位置之间的动画呢?

在文档中仅参考3D对象的动画是关于使用Blender(及类似工具)中的obj文件,对于运动,我不需要这样做。

有人可以帮我些忙吗? 提前致谢!!

您可以执行以下操作:

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.
}

当然,这只是一个简单的例子。 实际实现将根据用例而有所不同。 例如,您还可以扩展ModelInstance并在其中跟踪动画。 因为它非常适合用例,但是实现起来非常简单,所以通常不值得使用工具(例如Universal Tween Engine

是我最近为最新教程写的另一个示例,也许也有帮助。 它旋转并移动此视频中的卡。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM