如何正确设置相机?

时间:2015-07-08 15:18:44

标签: java camera libgdx coordinates

在我的课上我实现了ApplicationListener,所以在我的创建方法中这就是我的工作:

public void create(){
camera=new Camera();
camera.setToOrtho(false,screenW,screenH);
}

//then on the render method:
public void render(){
camera.update();
}

但是当我使用Gdx.input.getY()时,结果是相反的,当我上升时,Y坐标会减少,当我下降时,它会变得更高。为了更好地理解:

enter image description here

2 个答案:

答案 0 :(得分:1)

In Java coordinates start in the upper left corner (0,0). On a 100 x 100, (100,0) would be upper right, and (0, 100) would be lower left. So the behavior you are seeing is expected.

http://inetjava.sourceforge.net/lectures/part2_applets/InetJava-2.1-2.2-Introduction-to-AWT-and-Applets_files/image001.gif

答案 1 :(得分:1)

查看相机类和unproject方法。这应该在屏幕坐标和世界坐标之间转换。 (可能是更有效的方法,但为了说明):

float x = Gdx.input.getX();
float y = Gdx.input.getY();
float z = 0.0f;
Vector3 screenCoordinates = new Vector3(x, y, z);
Vector3 worldCoordinates = camera.unproject(screenCoordinates);

然后使用worldCoordinates向量来满足您的需要。

编辑:添加了小工作示例和屏幕截图。我的屏幕截图没有捕获鼠标,因此是红色的“星星”。但是,当您在屏幕上移动鼠标时,这个简单的应用程序会在“初始”和“未投影”的坐标中显示y坐标。亲自试试吧。我想这就是你得到的,不是吗?

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector3;

public class SimpleInputCoords implements ApplicationListener {
   private OrthographicCamera camera;

   @Override
   public void create() {
      camera = new OrthographicCamera();
      camera.setToOrtho(false, 800, 480);
   }

   @Override
   public void render() {
       int x = Gdx.input.getX();
       int y = Gdx.input.getY();
       int z = 0;
       System.out.println("UnmodifiedYCoord:"+y);

       Vector3 screenCoordinates = new Vector3(x, y, z);
       Vector3 worldCoordinates = camera.unproject(screenCoordinates);
       System.out.println("UnprojectedYCoord:"+worldCoordinates.y);
   }

   @Override
   public void dispose() {
   }

   @Override
   public void resize(int width, int height) {
   }

   @Override
   public void pause() {
   }

   @Override
   public void resume() {
   }

}

enter image description here