libgdx / box2dlights

Fork of box2dlights by Kalle Hamalainen
Apache License 2.0
257 stars 82 forks source link

Adding rayhandler in LibGDx causes issues in InputListener #110

Closed maneeshbhunwal123 closed 6 years ago

maneeshbhunwal123 commented 6 years ago

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? I also asked this question here on stackoverflow https://stackoverflow.com/q/48488569/4239538

rinold commented 6 years ago

The updateAndRender method can cause the reset of viewport to default one - thus your FitViewport breaks, so if your are setting in it show(), for custom viewports add use of the: rayHandler.useCustomViewport(x, y, width, height);

A few more details are on this Wiki page.

maneeshbhunwal123 commented 6 years ago

@rinold Thanks for your quick response, I tried

@Override
    public void show() {
        viewport = new FitViewport(1080, 720);
        stage = new Stage(viewport);
        rayHandler.useCustomViewport(0, 0, 1080, 720);
}

But this also is not working? Can you please help, what these values should be, if not these?

Thank you very much.

rinold commented 6 years ago

Try using the viewport you create, may be it's different from the 1080x720 - e.g. like so:

viewport = new FitViewport(1080, 720);
rayHandler.useCustomViewport(viewport.getScreenX(),
                             viewport.getScreenY(),
                             viewport.getScreenWidth(),
                             viewport.getScreenHeight());
maneeshbhunwal123 commented 6 years ago

Thanks @rinold , saved my day.

rinold commented 6 years ago

Have a nice day! :)