2016-08-05 51 views
0

我学习维基libgdx的正交相机的例子:旋转OrthographicCamera从点(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()); 
} 
} 

在相机的旋转,地图与照相机的旋转屏幕中点。我想从某个角度旋转相机。例如,点0,0。我试图使用方法rotateAround(Vector3点,Vector3轴,浮点角度),我没有得到预期的结果。

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

我知道可以将相机移动到0.0点,然后旋转。但那不是我想要的。

在游戏中,我玩的玩家是在中间屏幕底部的一个固定位置,我围绕它旋转了屏幕,但没有将玩家带到屏幕中间,然后旋转。

我很感激帮助。

回答

2

你只需要在cam.rotateround后更新相机:) 它适用于我。

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

这是我测试的截图。正如你所看到的围绕红点旋转的屏幕。 (透视相机还绕着同一个点,在自己的坐标。)

此外,我建议你使用

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

,而不是

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

Arcon

+0

德尼兹感谢寻求帮助。我设法旋转相机。 相机旋转后,坐标对我来说很混乱。 我有一个相机的角色和地图。 此球员只能朝北方向(90°),南方(270°),东方(0°),西方(180°)方向行走。 从摄像机位置旋转摄像机后,他开始以新角度移动角度作为替换角度。 有没有办法将原件重新定位到坐标而不移动地图到原始位置? 我将尝试在堆栈溢出的图像上再打开一个问题。 我很感激帮助。 – Lovera

+0

我在等你的问题。如果你提供一些关于玩家动作的代码,这将有所帮助。 –

+0

我把有问题的图像。 http://stackoverflow.com/questions/38975166/libgdx-coordinates-after-rotatearound-camera 再次感谢您的帮助。 – Lovera