Libgdx light without box2d

I just started creating a game using libgdx. This is the top down arrow 2d using scene2d ui. Now I thought that I could add darkness and light to some levels, but I do not want to rewrite everything using box2d. I do not need realistic shadows, just some kind of ambient light and a light circle around my character, which is not affected by the walls and other obstacles surrounding him. So I wanted to know if there is any lighting system in libgdx? Or can I use box2dlights without using body2d body / world ...? Thanks

+6
source share
2 answers

Unfortunately, nothing of the kind has already been provided by LibGDX.

But you can easily do it yourself if you don't want a shadow.

Here is a short video about who did this through LibGDX. Here is an article for this video with the code and description and everything provided. You can do this with shaders, but you can also do the same by simply turning off the lightmap for the FBO (in these links you can see how to do it), and then just visualize it in the usual way with your screen blending enabled. The standard SpriteBatch can do this, and you don't need any custom shaders.

If you still want to have real shadows with obstacles, you will probably find this article very useful. But this is much slower and requires special shaders.

There is also no way to use Box2dLights without Box2D btw.

+6
source

In my opinion, the marked answer is incorrect.

It is actually quite simple to use box2dLights without using box2d if you don't want any shadows. The question was that if you could add some kind of circular light around the character.

I used two different approaches, only one used box2dlights.

The article in the marked answer describes a method using FBO. U really don't need it if you just want to highlight the area. U just need a sprite like this . Now put it somewhere on the ur screen, and when rendering, do the following:

 batch.setBlendFunction(GL20.GL_DST_COLOR, GL20.GL_SRC_ALPHA); theLightSprite.draw(batch, parentAlpha); batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); 

At first, the mixing function changes, so we do not just visualize the sprite, but mix it in the background. After drawing it, reset the blending function will be normal, so that everything else after that will be displayed again. Here it is.

The second approach uses box2dlights. Yes, we need a box2d world object, but we don’t need to do anything with it. So what we do:

 world = new World(new Vector2(0,0),false); rayHandler = new RayHandler(world); rayHandler.setCombinedMatrix(stage.getCamera().combined); new PointLight(rayHandler,1000, Color.BLUE,radius,x_position,y_position); 

First, we create our world that simply does nothing. We only need this for the second statement, where we create our RayHandler, which computes our lights. In the third statement, we establish the ray handler matrix. In this case, I use scene2d, and thus using a cascading combo matrix. If you are using another camera, just use its combined matrix. The last statement creates a pointlight with rayHandler and describes the number of rays, the background color, its radius and its position.

All we need to do now is draw our scene or sprites in our render () method and call

 rayHandler.updateAndRender(); 

after that in the rendering method. Pretty easy.

+20
source

All Articles