简体   繁体   English

java libgdx移动透视摄像头

[英]java libgdx move perspective camera

I'm creating a 3D game but I don't know how to translate the camera according to my fingers. 我正在创建一个3D游戏,但我不知道如何根据我的手指翻译相机。 I'm creating a map(x = -30 to +30;y = -30 to 30;z = -1 to 1) where every coordinate is used for a model using .g3db files from my assets and put in place using model translation. 我正在创建一个地图(x = -30到+30; y = -30到30; z = -1到1),其中每个坐标用于使用我的资产中的.g3db文件的模型,并使用模型放置到位翻译。 This works so far, the map looks good and is viewed in a 67% angle. 这项工作到目前为止,地图看起来很好,并以67%的角度查看。 The map is so large it can't be viewed in the camera at once(no I don't want to zoom out). 地图太大,无法立即在相机中查看(不,我不想缩小)。 Whenever I'm touching the screen of my android phone it's just rotating around the center, but instead I want the gamemap to stay in place from my point of view and instead change the position of the perspective camera on the x and y axis. 每当我触摸我的Android手机的屏幕时,它只是围绕中心旋转,而是我希望游戏地图从我的视角保持原位,而是改变透视摄像机在x和y轴上的位置。 There's no game object that can be used for position tracking, everything should be depending on my finger actions. 没有可用于位置跟踪的游戏对象,一切都应该取决于我的手指动作。 This way I want to move to visible screen to each x and y direction(think of it visualy like a 2D camera moving up, down and to the sides in front of a picture). 这样我想要移动到每个x和y方向的可见屏幕(想象它看起来像2D摄像机向上,向下和向前移动到图片前面)。 Can you help me? 你能帮助我吗?

This is my code so far: 到目前为止这是我的代码:

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

I'm using the spring tool suite 3.5.1 as IDE and libgdx 1.0.1 我使用Spring工具套件3.5.1作为IDE和libgdx 1.0.1

Dont use a CameraInputController , but instead implement InputProcessor and use that class in the call to Gdx.input.setInputProcessor . 不要使用CameraInputController ,而是实现InputProcessor并在调用Gdx.input.setInputProcessor使用该类。

For example: 例如:

public class MyGdxGame extends ApplicationAdapter implements InputProcessor {

And in the create method: 并在创建方法:

Gdx.input.setInputProcessor(this);

You'll have to implement all methods as shown in this link , except for the touchDown and touchDragged method which require additional code: 除了需要额外代码的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;
}

Here the dX and dY values is the amount that user has dragged on the screen within the range between -1f and +1f . 这里dXdY值是用户在-1f+1f之间的范围-1f屏幕上拖动的量。 Eg for dX a value of 0.5f means that the user dragged half the screen size to the right. 例如,对于dX ,值为0.5f意味着用户将屏幕尺寸的一半拖到右侧。 Note that these are delta values since the last time the method was called. 请注意,这些是自上次调用方法以来的delta值。 The method is likely to be called many times per drag operation. 每次拖动操作可能会多次调用该方法。 Therefor, the dX and dY will be small values. 因此, dXdY将是小值。

The call to cam.position.add(...) will actually move the camera and the call to cam.update(); cam.position.add(...)的调用实际上会将摄像头和调用移动到cam.update(); will recalculate the values needed for rendering. 将重新计算渲染所需的值。 I've used a multiplier of 10f for both the X and Y translation of the camera. 我已经使用了10f的乘数来进行相机的X和Y平移。 This means that if the user fully drags from the left edge to the right edge of the screen, that the camera will move a total amount of 10 units to the right. 这意味着如果用户完全从屏幕的左边缘拖动到右边缘,则相机将向右移动总共10个单位。 You can adjust the value as needed. 您可以根据需要调整值。

Note that this moves the camera on XY plane. 请注意,这会将相机移动到XY平面上。 If you need to move on eg the XZ plane, you need to adjust the call the cam.position.add(...) accordingly. 如果需要移动,例如XZ平面,则需要相应地调整cam.position.add(...)的调用。 Also, this doesn't change the direction of the camera. 此外,这不会改变相机的方向。 If you want to move the camera, while looking at the same location, you'll need to add cam.lookAt(0,0,0); 如果你想移动相机,同时查看相同的位置,你需要添加cam.lookAt(0,0,0); just before cam.update(); 就在cam.update();之前cam.update(); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM