0
votes

I am studying the example of Orthographic Camera of wiki libgdx :

https://github.com/libgdx/libgdx/wiki/Orthographic-camera#description

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;

public class OrthographicCameraExample implements ApplicationListener {

static final int WORLD_WIDTH = 100;
static final int WORLD_HEIGHT = 100;

private OrthographicCamera cam;
private SpriteBatch batch;

private Sprite mapSprite;
private float rotationSpeed;

@Override
public void create() {
    rotationSpeed = 0.5f;

    mapSprite = new Sprite(new Texture(Gdx.files.internal("sc_map.png")));
    mapSprite.setPosition(0, 0);
    mapSprite.setSize(WORLD_WIDTH, WORLD_HEIGHT);

    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    // Constructs a new OrthographicCamera, using the given viewport width and height
    // Height is multiplied by aspect ratio.
    cam = new OrthographicCamera(30, 30 * (h / w));

    cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);
    cam.update();

    batch = new SpriteBatch();
}

@Override
public void render() {
    handleInput();
    cam.update();
    batch.setProjectionMatrix(cam.combined);

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    mapSprite.draw(batch);
    batch.end();
}

private void handleInput() {
    if (Gdx.input.isKeyPressed(Input.Keys.A)) {
        cam.zoom += 0.02;
    }
    if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
        cam.zoom -= 0.02;
    }
    if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
        cam.translate(-3, 0, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
        cam.translate(3, 0, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
        cam.translate(0, -3, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
        cam.translate(0, 3, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.W)) {
        cam.rotate(-rotationSpeed, 0, 0, 1);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.E)) {
        cam.rotate(rotationSpeed, 0, 0, 1);
    }

    cam.zoom = MathUtils.clamp(cam.zoom, 0.1f, 100/cam.viewportWidth);

    float effectiveViewportWidth = cam.viewportWidth * cam.zoom;
    float effectiveViewportHeight = cam.viewportHeight * cam.zoom;

    cam.position.x = MathUtils.clamp(cam.position.x, effectiveViewportWidth / 2f, 100 - effectiveViewportWidth / 2f);
    cam.position.y = MathUtils.clamp(cam.position.y, effectiveViewportHeight / 2f, 100 - effectiveViewportHeight / 2f);
}

@Override
public void resize(int width, int height) {
    cam.viewportWidth = 30f;
    cam.viewportHeight = 30f * height/width;
    cam.update();
}

@Override
public void resume() {
}

@Override
public void dispose() {
    mapSprite.getTexture().dispose();
    batch.dispose();
}

@Override
public void pause() {
}

public static void main(String[] args) {
    new LwjglApplication(new OrthographicCameraExample());
}
}

On the rotation of the camera, map is rotated with the camera at the midpoint of the screen. I would like to rotate the camera from a certain point. For example, the point 0,0 . I tried to use the method rotateAround ( Vector3 point , Vector3 axis , float angle) , I did not get the expected result.

cam.rotateAround(new Vector3(0,0,0), new Vector3(0,0,1), 1);

I know it's possible to move the camera to the point 0.0 and then rotate . But that's not what I want .

In the game I'm doing the player is in a fixed position at the bottom of the screen in the middle and I turned the screen around it , but without taking the player to the middle of the screen and then rotate .

I appreciate the help .

1

1 Answers

2
votes

You just need to update camera after cam.rotateround :) It works for me.

 cam.rotateAround(new Vector3(270, 0, 0), new Vector3(0, 0, 1), 0.1f);      
 cam.update();

Here is a screenshot of my test. As you can see screen rotating around red dot. (Perspective camera is also rotating around same point that in its own coordinate.)

Also i suggest you to use

cam.setToOrtho(false, cam.viewportWidth, cam.viewportHeight);

instead of

cam.position.set(cam.viewportWidth / 2f, cam.viewportHeight / 2f, 0);

Arcon