Hi,
Is it possible to convert a libgdx game that draws whole lot of tiles but no box2d bodies to use box2dlights or do I need to create box2d bodies also?
Thanks
Hi,
Is it possible to convert a libgdx game that draws whole lot of tiles but no box2d bodies to use box2dlights or do I need to create box2d bodies also?
Thanks
I don’t think you need box2d bodies to use box2dlights. Why don’t you just try something out and see what happens?
I think you do for shadows. but not for lights
Mmmmmm, I’m sure you would need each tile to have an associated box2d body, can’t see how it would work otherwise?
I put a thread in java2d by mistake (please forgive me for cross post) on how I would go about adding lights to my game, I’ve no experience with shaders, although looked
at some tutorials, vignette shader looks ok to add some light around the player, I’m thinking when placing torches around be best to just light up
the tiles around the light - just set tint on them, this sound correct?
The map shown above is proceduarly generated and currently there are 5000x2000 tiles in the whole map where each tile is 16x16 and runs at 60fps on my crappy laptop. I think it would look better if the map started going darker as the tiles go down like in terraria, again I guess this could be done by getting the tiles y position and setting its colour accordingly?
I’ve messed around a little, well hacked my code some and come up with this:
Block lighting when player digs down from top, maybe the torches I place can use same technique, just light blocks up around the torch?
Any help with a simple light implementation would be greatly appreciated, this lights in terraria are awesome and how they do all this in 60fps is amazing to me.
Thanks
The solution I once used in a tile-based game such as yours was to first light downwards from a ‘sun’, with ambient light, and if I encountered a world light I would illuminate tiles around it by blending the ambient light with the tile’s light colour. The distance that a light illuminated the tiles around it was determined by not only its luminosity but also the amount of light which other tiles specified that they absorbed.
I use very few bodies for my tiles and haven’t encountered any issues with Box2dlights. You will need a Box2D world to initiate Box2dlights, but otherwise Box2D is not required unless of course you need shadows.
Many thanks all,
So for Box2d lights, just initialise a box2d world, no box2d bodies needed, interesting.
Are lights generally done via a physics engine?
another, possibly faster, solution is using blended “light” textures like I described over here:
http://techblog.orangepixel.net/2015/07/shine-a-light-on-it/
using it in various games to great effect
Hi,
Thanks for this, not used framebuffer before, this like an offscreen buffer you draw into and then overlay it onto your main screen?
Are your lights always in same position or as you scroll the screen you re-position them etc?
Thanks
If I remember correctly, for the lights in Terraria (and some other 2D block game I forgot), a different approach is used for nearby lights than for lights that are further away. E.g. blocks closer to the player get per-pixel lighting, and blocks that are further away get per-block lighting (which is much faster). This way you get good-looking shadows where the action is, whilst keeping performance high.
Games that look cool and perform well usually do such tricks.
Thanks Grunnt,
How would you do per-pixel lighting, I guess this has to be down the the fragment shader?
yes everything is always rendered to a framebuffer, but for this you just create another buffer that is rendered on top of the other.
the lights are rendered every frame, so they are dynamic just not casting shadows
Ok, but when screen scrolls around, do the lights get updated as to where they are on this fbo?
Do you need another map the same size of your game world map that contains the light positions?
Thanks
you redraw them in the right location every time.
so your framebuffer can be just as big as the screen, not bigger.
but you might want to start with something more simple if this is a bit too hard to understand (cause it’s actually really simple game code)
Hi,
What I was meant to say is where do you store the lights? Are they in a 2d array or something? My game world is in a large 2d array but of course
I only draw what the camera sees each frame.
So, render scene to standard frame buffer (screen) as normal like I do now, then render lights to lightbuffer, then render this light buffer to the standard frame buffer?
Is this correct?
Thanks
Tried to do the FBO stuff and this is what I’m getting:
Code:
this.lightBuffer.begin();
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE);
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glClearColor(0.9f,0.4f,0.9f,0.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.setColor(0.9f, 0.9f, 0.9f, 0.2f);
this.lightSprite.setX(300);
this.lightSprite.setY(200);
this.lightSprite.draw(batch);
batch.end();
this.lightBuffer.end();
Gdx.gl.glBlendFunc(GL20.GL_DST_COLOR, GL20.GL_ZERO);
batch.begin();
batch.draw(this.lightBufferRegion, 0,0);
batch.end();
I get this: (light sprite is just set at 300,200)
I’m not sure what the glBlendFunc’s are doing or setting the background clear colour needs setting?
You can see the round light being rendered, how to make this better?! Guess make it smaller, and only draw where lights are placed in my game?
Thanks
Now using Box2DLights and so much simpler I guess behind the scenes it renders to a framebuffer and uses raycasting?
This is what I got with one spot light:
I do exactly as you want to do: using libgdx, not box2D per se but still using box2Dlights just for lights
I made a short clip of my current map editor here: https://drive.google.com/file/d/0B7dHrGvdQHFbU2toZlp4NGdjQWM/
I toggled the bodies so its more obvious.
Please excuse the map tiles itself, its a mess, just to toy around with.
So yeah for anything you want to block light, you create a box2D body. in this case being level geometry they are static, for enemies and stuff you can have active ones. Many of these COULD be combined via algorithm into less bodies for performance reasons.
so now if something with a box2D body moves, you just set and update that body to be accurate, not using the physics of box2D but your own and then merely updating its position.
Absolutely possible and not uncommonly done with box2Dlights
That map editor looks really good, well done!
I don’t use box2d bodies as don’t need shadows, so just using RayHandler and a box2d World object.
Hoping to put in light map to render lights where they are placed and used.
Modified the lights on the blocks just under the grass and made under ground dark now, oh and I have a magical torch…