Light and questions

Hello everyone,

I’m actually developping my own little game, will be a Diablo like game in 2D. I already started the development few month ago using LIBGdx and i’ve got a few question.

I reached a point where i need to handle light. I know there is something added in 1.0.0 who was available as an external librairies before called BOX2DLights. I’ve already used it for another game.
The problem is the following one, BOX2DLight need you to use BOX2D, and my game is top view game (meaning that the camera is on the top of the player, like Pokemon) and doesn’t implements any Box2D stuff. I’m using this code as an Entity and Wall collider :

	public boolean collision(float x, float y) {
		int posXleft = (int) (x / this.map.tileWidth);
		int posYbot = (int) (y / this.map.tileHeight);
		int posXright = (int) ((x + collisionWidth) / this.map.tileWidth);
		int posYtop = (int) ((y + collisionHeight) / this.map.tileHeight);

		if(map.collisionLayer.getCell(posXleft, posYbot).getTile().getProperties().containsKey("blocked")) {
			return true;
		}
		if(map.collisionLayer.getCell(posXleft, posYtop).getTile().getProperties().containsKey("blocked")) {
			return true;
		}
		if(map.collisionLayer.getCell(posXright, posYbot).getTile().getProperties().containsKey("blocked")) {
			return true;
		}
		if(map.collisionLayer.getCell(posXright, posYtop).getTile().getProperties().containsKey("blocked")) {
			return true;
		}
		if(map.collisionLayer.getCell(posXleft, posYbot).getTile().getProperties().containsKey("wall")) {
			return true;
		}
		if(map.collisionLayer.getCell(posXleft, posYtop).getTile().getProperties().containsKey("wall")) {
			return true;
		}
		if(map.collisionLayer.getCell(posXright, posYbot).getTile().getProperties().containsKey("wall")) {
			return true;
		}
		if(map.collisionLayer.getCell(posXright, posYtop).getTile().getProperties().containsKey("wall")) {
			return true;
		}
		
		
		return false;
	};
	
	protected boolean collisionInEntity(float x, float y, float xObject, float yObject, int collisionHeight, int collisionWidth) {
		Rectangle player = new Rectangle(x, y, this.collisionWidth, this.collisionHeight);
		ShapeRenderer shapeRenderer = new ShapeRenderer();
		shapeRenderer.setProjectionMatrix(map.camera.combined);
		shapeRenderer.begin(ShapeType.Line);
		shapeRenderer.setColor(new Color(0, 1, 0, 1));
		shapeRenderer.rect(x, y, this.collisionWidth, this.collisionHeight);
		shapeRenderer.end();
		Rectangle ennemy = new Rectangle(xObject, yObject, collisionWidth, collisionHeight);
		if(player.overlaps(ennemy) || ennemy.overlaps(player)) {
			return true;
		}
		return false;
	}

There won’t be anymore collision stuff (i can handle any sort of spell, ennemy or wall and that’s enough for me).
So here is my point. Do i need to rewrite my code to implement Box2D to each of my entities who will allow me to handle Box2DLights ? Do i have another possibility to handle the lights collision why my own collisions ? Is it possible to write my own light stuff ?

Now another question. As i say, i’m currently developping under Libgdx, but as far as i see, the only good point Libgdx offer me is cross platform possibility and for this project i clearly don’t need it ! Do i need to keep working on LIbgdx or can i switch back and redevelop entierly my stuff on LWJGL ?
And last question, is there anyway to use the 1.0.0 of LIbgdx without using their Gradle system … ?

Thanks everyone

I don’t think you should use Box2DLights in this scenario, as using Box2D limits you a lot in what you can and cannot do, and it will be far from optimal performance-wise. You might want to write your own lighting code or even use shaders. I’m pretty sure that some nice examples may be found on these boards if you look for them. Maybe someone else has a suggestion?

Also, don’t underestimate what LibGDX does for you with the TextureAtlas, SpriteBatch, Camera, Scene2D, etc etc etc support that it gives. Feel free to brew your own, but also know that it will be nigh impossible to do it better as a single developer.

Good luck with your game!

Hi,

Thanks for your answer, that was what I was thinking about too… Any tips on how to do a light engine or any tutorial who can explain me how to ?
I already have some ideas, here are my tought :

Draw an black screen on top of my screen, put an opacity of 0.7 or 0.8.
Draw a white circle on top of it, this white circle will have a CircleCollider or another collider (don’t know yet…).
Each object that should interrupt the light (so a collider…) will cut of the circle, as shown below :

http://nsa34.casimages.com/img/2014/05/27/140527011319597910.png

In the cut part (if i manage to store the geometrical form) i could apply a gradient on it.

Of course the circle will have number of rays to detect where it collides.

Sound like a good idea ?

Now that I actually use box2Dlights I disagree with Grunnt.

I wrote my own physics system for my game, which had nothing to do with box2D,then I wanted lighting.

Turns out you can use box2Dlights without box2D basically. Using an empty world.

If you want shadows you would create static or dynamic bodies for all the “walls” in you game.
I have just finished with all that and it works beautifully.
Might be a TINY little more overhead or RAM usage than doing it yourself, but its way way worth the speed of development, ease of use and also the difference is negligible

Well, even if you used “static” bodies, you are still using Box2D.

I totally agree with you on the fact that using something who already exist (and since i already used it…) will be more easy for me, but i’m affraid that it will overload the code. I know it’s a small project who can run for the moment on every computer i tried, but i’m still afraid of framerates drop >.>

[EDIT] Out of the subject but not so much, i’m using Math.sqrt in to calculate the distance between two points, i’ve read on the internet that this method is not that good and long to do, is there an other method that does the same thing ?

Writing a lighting system yourself will overload the code.

In this case all you need would be this:

Init light system and create one light:


//init
world = new World(new Vector2(0, 0), true);
rayHandler = new RayHandler(world);

//new light
new PointLight(rayHandler, RAYS_NUM, new Color(1,1,1,1), lightDistance, x, y);

//every frame
world.step(Gdx.graphics.getDeltaTime(), 0, 0);
rayHandler.setCombinedMatrix(mapCamera.combined, mapCamera.position.x, mapCamera.position.y, mapCamera.viewportWidth * mapCamera.zoom, mapCamera.viewportHeight * mapCamera.zoom);
rayHandler.updateAndRender();


					BodyDef bodyDef = new BodyDef();
					bodyDef.type = BodyDef.BodyType.StaticBody;
					Body body = world.createBody(bodyDef);
					PolygonShape shape = new PolygonShape();
					shape.setAsBox(width, height);
					body.setFixedRotation(false);
					body.createFixture(shape, 0);
					body.setTransform(xpos, ypos, 0);

and render box2D for debug, if needed:

box2DRenderer.render(world, camera.combined);

and a lot of stuff adjustable just with one line, distance, number of rays, static or dynamic light, color, diffuse, gammacorrection, xray

Take a deeper look and discover that there is whole lot more.

Keep working with Libgdx: Yes.
Redevelop: Hell, no.

Good point, dependency build systems suck.
Download the sources and work with them.

My top down game uses Libgdx with Box2dLights as well: http://diabolus-ex-machina.com/index.html

Yeah I just download the nightly jar and import that

Hi everyone,

First of all, i’d like to thank you for all your answer, they were very usefull to my knowledge and my test.
I’m now facing a small problem, i don’t know what to use.
I tried to what i asked help for, and the result is good but i feel the game pretty empty and dark (it’s not an horror game, it’s a RPG game…) so i’d like your opinion on the question.

Which version do you prefer ? Do you have any other idea that could make it better ?

Here is a link to download a .rar who contains one file + 2 executable jar, just execute them and don’t move the “external” folder.

Link : http://dl.free.fr/kBFyUhRgP

Thanks :slight_smile: