2015-03-03 40 views
0

我最近拿起了学习LibGDX,并喜欢简单的演员系统,当涉及到移动/褪色/旋转的东西。很简单!但是,转入3D,我想知道是否有类似的实施方式。像演员一样随着时间的推移翻译模型?

我见过有人说“instance.transform(x,y,z)”,但是它立即应用并显示在下一个渲染上。我想知道是否可以使用某些内置框架插入转换ala“model.addAction(moveTo(x,y,z),duration(0.4f));”我知道Model并不像Scene2d中的大多数元素那样继承Actor。当涉及骨骼动画和导入的动画模型时,我理解当前的动画框架,但我正在寻找更多的解决方案来移动我的模型,而不必在渲染器中执行大量的依赖逻辑。

而不是使用自定义插值实现混乱我的渲染循环,有没有办法在模型上使用Action框架?只是像翻译,旋转等的东西(如果没有,我可能会沿着模型对象上的动作,游泳池等实现的道路。)

谢谢!

回答

0

一个快速而肮脏的窍门是在一侧使用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; 
    } 
} 

您可以创建一个方便的类类似于操作类,可以很容易地设置与自动生成汇集自己的行为:

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; 
    } 

} 

使用范例。 ModelInstances移动有点棘手,因为您必须使用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); 
+0

会给这个镜头并报告回来。谢谢! – 2015-03-03 14:26:05

+0

我已经能够通过扩展TemporalAction来实现我所需要的。谢谢!当我编写更多的函数时,我会用Action3d类发布一个小Github。也许它会帮助其他人。我喜欢你看看它,并告诉我如果你认为有一个下游问题来控制矩阵如此公然(或者如果使用更复杂的形状 - 我只使用一个立方体)。 – 2015-03-04 16:11:37

相关问题