1

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

我几乎准备好了所有东西,但我无法为我的盒子制作动画。我的意思是,当我触摸屏幕时,我希望我的立方体向右移动 90 度并向右平移 1(单位)。结果,盒子的右侧将是新底座,旧底座将在左侧,盒子向右移动。

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

文档中仅引用 3D 对象的动画是关于使用来自搅拌机(和类似的)的 obj 文件,而对于我需要的运动,我认为没有必要。

谁能给我一些帮助?提前致谢!!

4

1 回答 1

1

你可以这样做:

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

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

于 2015-12-31T12:03:03.650 回答