我最近开始学习LibGDX,并喜欢Actor系统在移动/淡入/旋转内容方面的简单性。太简单了!但是,进入3d,我想知道是否有类似的实现风格。
我看到有人说"instance.transform(x,y,z)",但这会立即应用并显示在下一次渲染中。我想知道我是否可以使用一些内置框架ala"model.addAction(moveTo(x,y,z),duration(0.4f))来插入这种转换;"我知道model并不像scene2d中的大多数元素那样继承Actor,但我正在寻找一种解决方案,在我的游戏世界中移动我的模型,而不必在渲染中做大量的依赖逻辑。
有没有一种方法可以在模型上使用Action框架?只是平移、旋转等。(如果没有,我可能会在Model对象上实现操作、池等。)
谢谢!
一个快速而肮脏的技巧是在侧面使用scene2d的动作系统。创建一个没有演员的舞台。你可以直接将你的动作发送到舞台,并每帧调用stage.update(delta);
一次来处理你的所有动作。你不需要画舞台就可以做到这一点。(无论如何,你可能会想要一个舞台来展示你的2D UI。)
您需要创建自己的操作。既然舞台不适合3D,就没有理由尝试使用Actors。你的动作可以直接应用到舞台上进行处理,并且不针对任何参与者。
这里有一个例子(未经测试)。由于你主要关注的是随着时间的推移混合东西,你可能会从TemporalAction扩展大部分动作。
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);