使用libgdx平移3D模型

时间:2014-02-09 08:58:30

标签: android libgdx pan

我正在尝试平移3d对象,但它似乎无法正常工作。我向左和向右滑动手势以沿x轴平移对象。我添加了一个Log语句,它清楚地显示了logcat中的pan值,但对象仍然无法移动。

这是我的代码

public class 3DTest implements ApplicationListener, GestureListener {

public ModelBatch modelBatch;
public Model model;
public ModelInstance instance;
public Environment environment;
public OrthographicCamera camera;
public CameraInputController camController;
public PerspectiveCamera cam;
public ModelLoader loader;

@Override
public void create() {
    // TODO Auto-generated method stub
    InputMultiplexer im = new InputMultiplexer();
    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.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

    camera = new OrthographicCamera(1280, 720);

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

    camController = new CameraInputController(cam);

    loader = new ObjLoader();
    model = loader.loadModel(Gdx.files.internal("data/cube.obj"));
    instance = new ModelInstance(model);
    im.addProcessor(new GestureDetector(this));
    im.addProcessor(camController);
    Gdx.input.setInputProcessor(im);

}

@Override
public void dispose() {
    // TODO Auto-generated method stub
    modelBatch.dispose();
    model.dispose();
}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void render() {
    // TODO Auto-generated method stub
    camController.update();

    Gdx.gl.glClearColor(1, 1, 1, 1);    

    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(instance, environment);
    modelBatch.end();
}

@Override
public void resize(int arg0, int arg1) {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public boolean fling(float arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean longPress(float arg0, float arg1) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean pan(float arg0, float arg1, float arg2, float arg3) {
    // TODO Auto-generated method stub
    Log.i("App","pan" +arg2);
    camera.translate(arg2, 0);
    camera.update();
    return false;
}

@Override
public boolean panStop(float arg0, float arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean pinch(Vector2 arg0, Vector2 arg1, Vector2 arg2, Vector2 arg3) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean tap(float arg0, float arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean touchDown(float arg0, float arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean zoom(float arg0, float arg1) {
    // TODO Auto-generated method stub
    return false;
}

}

1 个答案:

答案 0 :(得分:1)

cam.position.set(1f, 1f, -10f);

更改cam中的“z”(第三个参数)。

工作正常。