【问题标题】:java libgdx move perspective camerajava libgdx移动透视相机
【发布时间】:2014-07-18 06:18:58
【问题描述】:

我正在创建一个 3D 游戏,但我不知道如何根据我的手指来平移相机。我正在创建一个地图(x = -30 到 +30;y = -30 到 30;z = -1 到 1),其中每个坐标都用于使用我资产中的 .g3db 文件的模型,并使用模型放置翻译。到目前为止,这很有效,地图看起来不错,并且可以 67% 的角度查看。地图太大了,无法立即在相机中查看(不,我不想缩小)。每当我触摸我的安卓手机屏幕时,它只是围绕中心旋转,但我希望游戏地图从我的角度保持在原位,而是改变透视相机在 x 和 y 轴上的位置。没有可用于位置跟踪的游戏对象,一切都应该取决于我的手指动作。通过这种方式,我想移动到每个 x 和 y 方向的可见屏幕(在视觉上想象它就像一个 2D 相机在图片前面向上、向下和向两侧移动)。你能帮帮我吗?

这是我目前的代码:

public class MyGdxGame extends ApplicationAdapter implements ApplicationListener{
//define variables
...
@Override
public void create() {
    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));

    modelBatch = new ModelBatch();

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

    assets = new AssetManager();
    //load assets
    ...
    loading = true;        
    camController = new CameraInputController(cam);
    camController.forwardTarget = true;
    Gdx.input.setInputProcessor(camController);      
}

private void doneLoading() {
//load models and add them to instances
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(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

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

我使用 spring 工具套件 3.5.1 作为 IDE 和 libgdx 1.0.1

【问题讨论】:

    标签: java android camera libgdx perspectivecamera


    【解决方案1】:

    不要使用CameraInputController,而是实现InputProcessor 并在对Gdx.input.setInputProcessor 的调用中使用该类。

    例如:

    public class MyGdxGame extends ApplicationAdapter implements InputProcessor {
    

    而在create方法中:

    Gdx.input.setInputProcessor(this);
    

    您必须实现this link 中所示的所有方法,除了需要额外代码的touchDowntouchDragged 方法:

    private int dragX, dragY;
    @Override
    public boolean touchDown (int x, int y, int pointer, int button) {
        dragX = x;
        dragY = y;
        return true;
    }
    
    @Override
    public boolean touchDragged (int x, int y, int pointer) {
        float dX = (float)(x-dragX)/(float)Gdx.graphics.getWidth();
        float dY = (float)(dragY-y)/(float)Gdx.graphics.getHeight();
        dragX = x;
        dragY = y;
        cam.position.add(dX * 10f, dY * 10f, 0f);
        cam.update();
        return true;
    }
    

    这里的dXdY 值是用户在-1f+1f 之间的范围内在屏幕上拖动的量。例如。对于dX0.5f 的值表示用户向右拖动了一半的屏幕大小。请注意,这些是自上次调用该方法以来的增量值。每次拖动操作可能会多次调用该方法。因此,dXdY 将是较小的值。

    cam.position.add(...) 的调用实际上会移动相机,对cam.update(); 的调用将重新计算渲染所需的值。我对相机的 X 和 Y 平移都使用了 10f 的乘数。这意味着如果用户从屏幕的左边缘完全拖动到右边缘,则相机将总共向右移动 10 个单位。您可以根据需要调整该值。

    请注意,这会在 XY 平面上移动相机。如果您需要继续前进,例如XZ 平面,您需要相应地调整调用cam.position.add(...)。此外,这不会改变相机的方向。如果要移动相机,同时查看同一位置,则需要在 cam.update(); 之前添加 cam.lookAt(0,0,0);

    【讨论】:

    • 对于键盘输入,render() 中的 if (Gdx.input.isKeyPressed(Input.Keys.W)) 也很有用
    猜你喜欢
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    相关资源
    最近更新 更多