【问题标题】:libgdx and box2d, screen touch coordinates nonsensicallibgdx 和 box2d,屏幕触摸坐标无意义
【发布时间】:2017-02-10 01:05:49
【问题描述】:

我有一个简单的 box2d 世界,有一个平台和一个摄像头跟踪玩家。当我触摸屏幕的一侧时,我试图获得触摸输入控制,但感觉比我想象的要复杂。触摸屏幕偶尔会移动盒子,但当我到达我期望的位置时从来没有,并且试图通过在我触摸的地方绘制图片来进行调试只会导致更多的混乱。这是相机或触摸监听的所有代码

创建方法

    public void create () {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    Gdx.input.setInputProcessor(new MyInputProcessor());

    camera = new OrthographicCamera();
    camera.setToOrtho(false, w/2, h/2);

    touchpos = new Vector3();

    world = new World(new Vector2(0, -9.8f), false);
    b2dr = new Box2DDebugRenderer();
    player = createBox(8, 10, 32, 32, false);
    platform = createBox(0, 0, 64, 32, true);
}

渲染方法

    public void render () {
    update(Gdx.graphics.getDeltaTime());

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    b2dr.render(world, camera.combined.scl(PPM)); 
}

调整大小

public void resize(int width, int height) {
    camera.setToOrtho(false, width / 2, height / 2);
}

相机更新

    public void cameraUpdate(float delta){
    Vector3 position = camera.position;
    position.x = player.getPosition().x * PPM;
    position.y = player.getPosition().y * PPM;
    camera.position.set(position);

    camera.update();
}

触摸输入法和数学

    @Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    touchpos.set(screenX, screenY, 0);
    camera.unproject(touchpos);

    if (touchpos.x > 500){
        touchleft = true;
    }
    else touchleft = false;

    if (touchpos.x < 300){
        touchright = true;
    }
    else touchright = false;

    if (300 < touchpos.x && touchpos.x < 500){
        touchcenter = true;
    }
    else touchcenter = false;

    return true;
}

我认为它应该像使用原始触摸值一样简单,而不会弄乱相机,因为我希望用于控制的区域始终相同,但这不起作用,所以我使用谷歌并尝试取消投影和用相机做其他摆弄,但触摸从未奏效。

我觉得答案应该很简单。我需要的是,当它检测到屏幕左侧或右侧的触摸时,将相应的变量设置为 true

如果有更多经验的人能看到错误,我会非常感激

【问题讨论】:

    标签: android libgdx box2d


    【解决方案1】:

    scl() 转换 Matrix4(camera.combined) 值,所以不要缩放 camera.combined。

    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    camera = new OrthographicCamera(30, 30 * (h / w));
    
     public void render () {
        update(Gdx.graphics.getDeltaTime());
    
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
        b2dr.render(world, camera.combined); 
    }
    

    您将使用物理学,因此将移动物体的大小保持在大约 0.1 到 10 米之间,而不是大于 10。

    如果您想绘制图像,只需将 box2d 维度缩放 30 并以像素为单位获取维度。

    对于触摸,您根据设备的宽度和高度设置视口,并在预定义的值上应用条件。

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        touchpos.set(screenX, screenY, 0);
        camera.unproject(touchpos);
    
        if (touchpos.x > camera.viewportWidth*.8f){
            touchleft = true;
        }
        else touchleft = false;
    
        if (touchpos.x < camera.viewportWidth*.2f){
            touchright = true;
        }
        else touchright = false;
    
        if (camera.viewportWidth*.2f< touchpos.x && touchpos.x < camera.viewportWidth*.8f){
            touchcenter = true;
        }
        else touchcenter = false;
    
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      相关资源
      最近更新 更多