libgdx / box2dlights

Fork of box2dlights by Kalle Hamalainen
Apache License 2.0
254 stars 81 forks source link

Changing properties at runtime - possible? #14

Closed IHazABone closed 9 years ago

IHazABone commented 10 years ago

Hi,

I'd like to first say that I'm loving the work done here. It's fantastic, even running smoothly on a low-end Android with ~700 PointLights (oops). I've come into two issues, here, and I'm not sure if what I'm trying to do is simply not possible or I'm doing it wrong. The first is not being able to change properties/values at runtime. If I have a button to, per se, change the color or size of a light, nothing happens, even though via the render method I should be updating the lights each frame. The second is referencing (in any way) any light outside of the render() or create() methods. I simply get a NullPointerException, which I don't quite understand, because these methods are being called by the player way after the lights have been initialized and are functional. I don't feel like code is necessary, everything is pretty normal and basic and where it should be (as in your demo code). If you'd like it, I can, but it's not convenient at the time. fadd3bce9e894211a94525bf42a0c61e The lines in you light classes pretty much point to adding the light to the RayHandler, if I recall correctly.

Also, sorry if this is the wrong place to post this. I've yet to receive a response about anything related anywhere else.

rinold commented 10 years ago

Looking at stack trace - the RayHandler instance passed to PointLight constructor is null. Could you debug or use logging to verify that you are passing the correct initialized RayHandler instance during that lights creation?

IHazABone commented 10 years ago

I am most definitely passing a correct, initialized RayHandler.

rinold commented 10 years ago

Also tried to change the color during runtime in test project - works fine. Could you share some code of actual box2dlights usage in your project, that is not working?

IHazABone commented 10 years ago

rayHandler = new RayHandler(world); rayHandler.useDiffuseLight(false); rayHandler.setCombinedMatrix(camera.combined); rayHandler.setAmbientLight(0.0f, 0.0f, 0.0f, 0.0f); ... a = new PointLight(rayHandler, 1024, Color.CYAN, light, -200, 200); b = new PointLight(rayHandler, 1024, Color.MAGENTA, light, -200, -200); c = new PointLight(rayHandler, 1024, Color.YELLOW, light, 250, 100); d = new PointLight(rayHandler, 1024, Color.MAROON, light, 200, 200); pl.attachToBody(p.body, 0f, 0f); pl2.attachToBody(p.body, 0f, 0f); pl.setXray(false); pl2.setXray(true); pl.setSoft(true); pl2.setSoft(true); ... public void lightOn() { new PointLight(rayHandler, 1024, Color.ORANGE, 200, 174, 430); //NullPointerException c.setColor(Color.BLUE); //NullPointerException }

Sorry, I'm not really sure how to format it as code here.

rinold commented 10 years ago

Yeah your code looks absolutely fine. What version of box2dlights are you using? You can try to find the code difference with box2dlights demo app (in tests - it's 100% working, tried it today).

IHazABone commented 10 years ago

It's the latest version... Hm. I'll take a look. Oh, and, just a question - is there any way to reduce the intensity of the center of a PointLight? What about reducing how incredibly bright it gets when you stack two together (I have a player light that follows the player and a torch, and if the player goes near the torch, it gets almost too intense to see the sprites).

rinold commented 10 years ago

What about reducing how incredibly bright it gets when you stack two together

Have you tried using the diffuse light model?

rayHandler.useDiffuseLight(true);

I see you set it to false in your code, is it essential for you?

IHazABone commented 10 years ago

The diffuse model, while being prominent in any 3D work I do (not with this engine of course), doesn't seem to work very well with this. It makes shadows appear as circular blobs about half the radius of the actual sprite, which looks dreadful.

rinold commented 10 years ago

The diffuse model, while being prominent in any 3D work I do (not with this engine of course), doesn't seem to work very well with this. It makes shadows appear as circular blobs about half the radius of the actual sprite, which looks dreadful.

This is quite an important comment, I will investigate it. Unfortunately, can't give you any estimates currently.

Also, about the exceptions, have you found any solution or root cause of it?

IHazABone commented 10 years ago

Actually, just today I managed to get it working: instead of calling some of my methods from inner classes and/or objects, I called them directly from the main class and it worked.