Search code examples
javalibgdxbox2dbox2dlights

Adding rayhandler in LibGDx causes issues in InputListener


I am trying out Libgdx, and I have an actor which performs some action whenever we click on it. So far it is working fine. Now I want to add light to the actor. After doing some research I came across Box2DLights. When I tried adding it to my project onClick Actor which was working fine does not seem to work. I am pretty sure this is due to rayhandler/Box2DLights because that is the only change I am making. here is the minimal change that I made to include Box2DLights.

public class GameScreen implements Screen {
    private RayHandler rayHandler;
    private World world;

    public GameScreen(Game game) {
        this.game = game;
        world = new World(new Vector2(0, 0), true);
        rayHandler = new RayHandler(world);
        rayHandler.setAmbientLight(0.1f, 0.1f, 0.1f, 1f);
        rayHandler.setBlurNum(3);
    }

     @Override
    public void show() {
        viewport = new FitViewport(1080, 720);
        stage = new Stage(viewport);
        rayHandler.setCombinedMatrix(stage.getCamera().combined);
        Gdx.input.setInputProcessor(stage);
    }
    @Override
    public void render(float delta) {
        //some custom rendering logic, but nothing related to rayHandler, excluding this for brevity.
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
        rayHandler.updateAndRender();
    }

Now When I debugged, I realised the the onClick is

working little below the actual actor

, that means somehow the coordinates sifted(I know weird). Can you please help?


Solution

  • Thanks @Mikhail Churbanov for your response here. If somebody else stumbles on this again here is the solution which worked.

    viewport = new FitViewport(1080, 720);
    rayHandler.useCustomViewport(viewport.getScreenX(),
                                 viewport.getScreenY(),
                                 viewport.getScreenWidth(),
                                 viewport.getScreenHeight());
    

    The explaination is box2lights doesn't auto-acquire custom viewports, and restores the 'default one' after the updateAndRender called - your need to set your custom 'fitted' viewport to rayHandler so that it would restore it correctly- using the rayHandler.useCustomViewport(...) method. All credits to @mikahi churbanov