Java libgdx move perspective camera

I am creating a 3D game, but I do not know how to translate the camera according to my fingers. I create a map (x = -30 to +30; y = -30 to 30, z = -1 to 1), where each coordinate is used for a model using .g3db files from my assets and created using the model transfer . This still works, the map looks good and can be viewed at an angle of 67%. The map is so large that it cannot be viewed in the camera right away (no, I don’t want to zoom out). Whenever I touch the screen of my Android phone, it just rotates around the center, but instead I want the memory card to stay in place from my point of view and instead change the position of the perspective camera on the x and y axis. There is no game object that can be used to track location, everything should depend on my finger actions. Thus, I want to go to the visible screen in each x and y direction (think of it as a 2D camera moving up, down and to the side in front of the image). Can you help me?

This is my code:

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 am using spring 3.5.1 toolkit as IDE and libgdx 1.0.1

+7
java android camera libgdx perspectivecamera
source share
1 answer

Do not use CameraInputController , but instead implement an InputProcessor and use this class in a call to Gdx.input.setInputProcessor .

For example:

 public class MyGdxGame extends ApplicationAdapter implements InputProcessor { 

And in the create method:

 Gdx.input.setInputProcessor(this); 

You will need to implement all the methods as shown in this link , with the exception of the touchDown and touchDragged , which require additional code:

 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 values ​​of dX and dY are the amount that the user dragged onto the screen in the range between -1f and +1f . For example. for dX value of 0.5f means that the user dragged half the screen size to the right. Note that these are delta values ​​since the last method call. This method is likely to be called many times during a drag operation. For this, the values ​​of dX and dY will be small values.

A call to cam.position.add(...) will actually move the camera, and a call to cam.update(); will recalculate the values ​​needed for rendering. I used a 10f factor to convert the X and Y cameras. This means that if the user completely drags from the left edge to the right edge of the screen, the camera will move the total number of 10 units to the right. You can adjust the value as needed.

Note that this moves the camera to the XY plane. If you need to move, for example. XZ plane, you need to adjust the call cam.position.add(...) accordingly. In addition, this does not change the direction of the camera. If you want to move the camera while looking at the same place, you need to add cam.lookAt(0,0,0); just before cam.update(); .

+14
source share

All Articles