【问题标题】:Hit-testing for decals in LibgdxLibgdx 中贴花的命中测试
【发布时间】:2013-04-07 13:56:09
【问题描述】:

我正在使用贴花类(请参阅https://code.google.com/p/libgdx-users/wiki/Decals)来渲染我的场景,我需要测试用户是否点击了其中一个贴花,类似于在舞台上为演员实现的方式。

是否有此类命中测试的实现?如果没有,libgdx 中是否有任何低级 API 可以帮助我实现它?

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

    我有一个使用 onTouch() 的好方法,可以轻松更改鼠标事件,

    Decal decal1 = Decal.newDecal(1, 1, textures[1], true);
        decal1.setPosition(1.5f, 0, .5f);
        decals.add(decal1);
        Decal decal2 = Decal.newDecal(1, 1, textures[0], true);
        decal2.setPosition(1f, 0, .5f);
        decals.add(decal2);
        decal2.translate(0, 0, 1f);
    //...
    for (int i = 0; i < positions.length; i++) {
            positions[i] = new Vector3(decal1.getPosition());
            new Vector3(decal2.getPosition());
        }
        positions[0].set(decal1.getPosition());
        positions[1].set(decal2.getPosition());
        renderer = new ImmediateModeRenderer10();
                    Vector3 intersection = new Vector3();
    
    
    //render method
        Ray pickRay = null;
        renderTowers();
        camera3d.update();
        if (Gdx.input.isTouched()) {
            pickRay = camera3d.getPickRay(Gdx.input.getX(), Gdx.input.getY(),
                    TB_X, TB_Y, TB_WIDTH, TB_HEIGHT);
        }
        boolean intersected1 = false;
        boolean intersected2 = false;
        for (int i = 0; i < positions.length; i++) {
            if (pickRay != null
                    && Intersector.intersectRaySphere(pickRay, positions[0],
                            .5f, intersection)) {
    
                gl.glColor4f(1, 0, 0, 1);
                intersected1 = true;
    
            } else {
                gl.glColor4f(1, 1, 1, 1);
            }
            gl.glPushMatrix();
            gl.glTranslatef(positions[i].x, positions[i].y, positions[i].z);
            gl.glPopMatrix();
        }
        for (int j = 0; j < positions.length; j++) {
            if (pickRay != null
                    && Intersector.intersectRaySphere(pickRay, positions[1],
                            .5f, intersection)) {
    
                gl.glColor4f(1, 0, 0, 1);
                intersected2 = true;
    
            } else {
                gl.glColor4f(1, 1, 1, 1);
            }
            gl.glPushMatrix();
            gl.glTranslatef(positions[j].x, positions[j].y, positions[j].z);
            gl.glPopMatrix();
        }
    
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
                Gdx.graphics.getHeight());
        sbatch.begin();
        if (intersected1) {
            stage.addActor(FireImage);
            camera3d.project(intersection, TB_X, TB_Y, TB_WIDTH, TB_HEIGHT);
    
        }
        if (intersected2) {
            stage.addActor(IceImage);
            camera3d.project(intersection, TB_X, TB_Y, TB_WIDTH, TB_HEIGHT);
    
        }
    

    不是很干净,但目前可以使用,其中部分内容来自一些优秀的 LIBGDX 中关于光线拾取的在线教程,所有这些都归功于他们!!!我只是为贴花定制了一点。希望它有所帮助,如果您想出一种更清洁的方法,我相信有一个请发布,我很乐意看到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 2021-08-14
      相关资源
      最近更新 更多