一个快速而肮脏的技巧是在侧面使用scene2d的动作系统。创建一个没有演员的舞台。您可以将您的动作直接发送到舞台并每帧调用一次stage.update(delta); 来处理您的所有动作。你不需要绘制舞台来做到这一点。 (不过,在未来,您可能还是想要一个展示 2D UI 内容的舞台。)
您需要创建自己的操作。由于舞台不适合 3D,因此没有理由尝试使用 Actor。您的 Actions 可以直接应用于舞台进行处理,并且设计为不针对任何 Actor。
这是一个示例(未经测试)。由于您主要关心的是随着时间的推移混合内容,因此您可能会从 TemporalAction 扩展大部分 Action。
public class MoveVector3ToAction extends TemporalAction {
private Vector3 target;
private float startX, startY, startZ, endX, endY, endZ;
protected void begin(){
startX = target.x;
startY = target.y;
startZ = target.z;
}
protected void update (float percent) {
target.set(startX + (endX - startX) * percent, startY + (endY - startY) * percent, startZ + (endZ - startZ) * percent);
}
public void reset(){
super.reset();
target = null; //must clear reference for pooling purposes
}
public void setTarget(Vector3 target){
this.target = target;
}
public void setPosition(float x, float y, float z){
endX = x;
endY = y;
endZ = z;
}
}
您可以创建一个类似于 Actions 类的便利类,以便轻松设置使用自动池生成您的操作:
public class MyActions {
public static MoveVector3ToAction moveVector3To (Vector3 target, float x, float y, float z, float duration){
return moveVector3To(target, x, y, z, duration, null);
}
public static MoveVector3ToAction moveVector3To (Vector3 target, float x, float y, float z, float duration, Interpolation interpolation){
MoveVector3ToAction action = Actions.action(MoveVector3ToAction.class);
action.setTarget(target);
action.setPosition(x, y, z);
action.setDuration(duration);
action.setInterpolation(interpolation);
return action;
}
}
示例用法。 ModelInstance 移动起来有点棘手,因为您必须使用它们的 Matrix4 变换来完成它,但是您不能安全地返回位置或旋转或缩放变换。所以你需要保留一个单独的 Vector3 位置和四元数旋转,并在每一帧上应用它们来更新实例的变换。
//Static import MyActions to avoid needing to type "MyActions" over and over.
import static com.mydomain.mygame.MyActions.*;
//To move some ModelInstance
stage.addAction(moveVector3To(myInstancePosition, 10, 10, 10, 1f, Interpolation.pow2));
//In render():
stage.update(delta);
myInstance.transform.set(myInstancePosition, myInstanceRotation);