是否可以在 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 并跟踪其中的动画。因为它非常具体use-case,但实现起来非常简单,通常不值得使用工具(像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.
我正在尝试开发一个非常简单的游戏,使用 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 并跟踪其中的动画。因为它非常具体use-case,但实现起来非常简单,通常不值得使用工具(像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.