[英]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.