Y轴反转libgdx

问题描述 投票:0回答:1

我正在尝试打印 libgdx 中我触摸的 PLAY 按钮的坐标 一切都朝着 X 方向顺利进行 但是Y是颠倒的,而且方向也不正确,它说它在173和312之间 可是我画的霜是72的,尺寸是64,怎么突然就变成139了 轴与我试图打印我触摸的 PLAY 按钮的 libgdx 中的坐标之间只是不匹配 一切都朝着 X 方向顺利进行 但是Y是颠倒的,而且方向也不正确,它说它在173和312之间 可是我画的霜是72的,尺寸是64,怎么突然就变成139了 轴与现实情况之间根本不匹配 另外,它并没有在0.0开始绘制,我开始相信也许我的版本有问题


    public static final int V_WIDTH =400;
    public static final int V_HEIGHT =208;

public class MainMenuScreen implements Screen {

    private Game game;
    SpriteBatch batch;
    private OrthographicCamera gamecam;
    private OrthographicCamera waterCam;
    private Viewport gamePort;
    private Viewport waterPort;
    private TmxMapLoader mapLoader;
    private TiledMap map;
    TiledMap WaterMap;
    private OrthogonalTiledMapRenderer renderer;
    private OrthogonalTiledMapRenderer rendererWater;
    Vector2 touchPosition;
    Texture img;
    Texture playButton;
    public MainMenuScreen(Game game, SpriteBatch batch) {
        this.game = game;
        this.batch=batch;
        img = new Texture("BG.jpg");
        playButton = new Texture("play.png");
        gamecam = new OrthographicCamera();
        waterCam = new OrthographicCamera();
        gamePort = new StretchViewport(LearningLetters.V_WIDTH,LearningLetters.V_HEIGHT,gamecam);
        waterPort= new StretchViewport(LearningLetters.V_WIDTH,LearningLetters.V_HEIGHT,waterCam);
        mapLoader = new TmxMapLoader();
        map= mapLoader.load("start.tmx");
        WaterMap = mapLoader.load("water.tmx");
        renderer = new OrthogonalTiledMapRenderer(map);
        rendererWater = new OrthogonalTiledMapRenderer(WaterMap);
        gamecam.position.set((16+4)*16,gamePort.getWorldHeight()/2,0);
        waterCam.position.set((16+4)*16,waterPort.getWorldHeight()/2,0);

    }

    @Override
    public void show() {

    }

    public void update(float dt){
        if(waterCam.position.x>(16+4)*16+128)
            waterCam.position.x=(16+4)*16;
        waterCam.position.x+=50*dt;
        gamecam.update();
        waterCam.update();
        renderer.setView(gamecam);
        rendererWater.setView(waterCam);

    }
    @Override
    public void render(float delta) {

        update(delta);
        Gdx.gl.glClearColor(1,1,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(img,16*16/2-16,0);
        batch.end();

        batch.setProjectionMatrix(waterCam.combined);
        batch.setProjectionMatrix(gamecam.combined);
        rendererWater.render();
        renderer.render();
        batch.begin();
        batch.draw(playButton,LearningLetters.V_WIDTH/2+64,LearningLetters.V_HEIGHT/2-64/2);
        batch.end();
        if(Gdx.input.isTouched()){
            touchPosition = new Vector2(Gdx.input.getX(), Gdx.input.getY());
            touchPosition.y=(Gdx.graphics.getHeight() - touchPosition.y)*64/Gdx.graphics.getHeight();
            if (touchPosition.x > LearningLetters.V_WIDTH/2+64/2 && touchPosition.x < LearningLetters.V_WIDTH/2+64+64) {
                if ( touchPosition.y > Gdx.graphics.getHeight() - LearningLetters.V_HEIGHT/2-64/2 - playButton.getHeight() && touchPosition.y < Gdx.graphics.getHeight() - playButton.getHeight() ) {
                    System.out.println(touchPosition);
                }

            }
        }
        // Render the main menu here
    }

    @Override
    public void resize(int width, int height) {
        gamePort.update(width, height);
        waterPort.update(width, height);
    }

    @Override
    public void pause() {
        // Pause the main menu here
    }

    @Override
    public void resume() {
        // Resume the main menu here
    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        map.dispose();
        // Dispose of the main menu here
    }
}

现实中正在发生 另外,它并没有在0.0开始绘制,我开始相信也许我的版本有问题

java android libgdx spritebatch
1个回答
0
投票

解决方案是查看相机的未投影坐标,而不是我查看它们的方式

我没有解释为什么它真的有效,但这是我找到的解决方案

if(Gdx.input.isTouched()){
            int x = Gdx.input.getX();
            int y = Gdx.input.getY();
            Vector3 touchPos = new Vector3(x,y,0);
            gamecam.unproject(touchPos);
            //touchPosition = new Vector2(Gdx.input.getX(), Gdx.input.getY());
            //touchPosition.y=(Gdx.graphics.getHeight() - touchPosition.y)*64/Gdx.graphics.getHeight();
            if (touchPos.x > LearningLetters.V_WIDTH/2+64 && touchPos.x < LearningLetters.V_WIDTH/2+64+64) {
                if ( touchPos.y >  LearningLetters.V_HEIGHT/2-64/2 && touchPos.y <  LearningLetters.V_HEIGHT/2-64/2+64 ) {
                    System.out.println(touchPos);
                }

            }
        }

© www.soinside.com 2019 - 2024. All rights reserved.