2014-02-20 36 views
0

我在Libgdx中开发桌面/ android应用程序,我想导入我在Blender中完成的动画。我已经从Blender导出了.obj文件,并成功将它导入到libgdx中,所以现在我启动应用程序,我可以看到该模型,但它并未移动。我应该怎么做才能让它按照关键帧移动?先谢谢你!这是我的代码:Libgdx - 从对象播放动画

public void create() { 
    modelBatch = new ModelBatch(); 
    environment = new Environment(); 
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f)); 
    environment.add(new DirectionalLight().set(0.50f, 0.8f, 0.8f, -1f, -0.8f, -0.2f)); 

    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
    cam.position.set(20f, 20f, 20f); 
    cam.lookAt(0,0,0); 
    cam.near = 0.1f; 
    cam.far = 300f; 
    cam.update(); 

    camController = new CameraInputController(cam); 
    Gdx.input.setInputProcessor(camController); 

    assets = new AssetManager(); 
    assets.load("mod1.obj", Model.class); 
    loading = true; 
    } 

private void doneLoading() {   
    Model mod1_model = assets.get("mod1.obj", Model.class); 
    ModelInstance mod1_instance= new ModelInstance(mod1_model); 
    mod1_instance.transform.setToTranslation(10f, 0, -10f); 
    instances.add(mod1_instance); 

    loading = false; 
} 

@Override 
public void render() { 
    if (loading && assets.update()) 
     doneLoading(); 
    camController.update(); 

    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

    modelBatch.begin(cam); 
    modelBatch.render(instances, environment); 
    modelBatch.end(); 
} 
+2

请勿使用.obj文件(obj不支持动画)。而是使用fbx-conv。另见:http://blog.xoppa.com/loading-models-using-libgdx/ – Xoppa

回答

2

采用3D封装的AnimationController来控制动画

private AnimationController animation; 
在创建

()或show()方法:

//Note you can get animation information by Animation Description 
animationDescription = new AnimationController(modelInstance); 
//when you export from blender to fbx the animation name is "new take" but here I used 'Walk' 
animation.animate("Walk", 2, 1f, null, 0.2f); 
在渲染

()方法:

//Note Gdx graphic delta 
animation.update(delta); 
//then render your instance 
+0

“animation.animate(”Walk“,2,1f,null,0.2f);”会根据这些坐标移动模型吗?事情是我已经在Blender中创建了动画,所以我只想在libgdx中播放它。 否则我已经将模型转换为g3db,它现在显示在屏幕上,但它不移动。 :? – user43051

+0

animate()适用于您的动作动画不移动整个角色,如移动旋转或移动的可能被蒙皮的节点,用于移动应该使用变形的角色,如modelInstance.transform.translate()。 – daniel

+0

另外不要忘记在render()中用delta来更新动画, – daniel