box2dLights

Present your own projects here, be they WIPs, betas or finished products. Get feedback from others and help each other out with some lovely testing.

box2dLights

Postby pitbuller » Sat Nov 26, 2011 5:02 pm

http://code.google.com/p/box2dlights/
http://dl.dropbox.com/u/10960490/Uusi%2 ... index.html
Image

I write library for dynamic 2d soft lighting. Its use box2d geometry and raycasting and create light meshes from collision data.
Library work with gles 1.1 or gles 2.0 automatically depending on program.

Rendering flow should go something like this.

Code: Select all
normal rendering/spriteBatch etc

rayHandler.updateAndRender()

ui stuff


Nothing special is needed it just works.

When creating a Rayhandler user just need to give box2d World and ortho camera
Code: Select all
rayHandler = new RayHandler(world, camera);

Lights can be created easily like this:
Code: Select all
new PointLight(rayHandler, RAYS_NUM, new Color(1,1,1,1), lightDistance, x, y);


After light is created rayHandler manage, update and render it so nothing special is needed from user.

Source is lisenced with apache 2.0 so its compatible with libgdx.

http://code.google.com/p/box2dlights/
Last edited by pitbuller on Wed Dec 28, 2011 12:18 pm, edited 2 times in total.
pitbuller
 
Posts: 134
Joined: Mon Jul 25, 2011 9:07 am

Re: box2dLights

Postby bach » Sat Nov 26, 2011 11:53 pm

Awesome stuff :)
bach
 
Posts: 713
Joined: Mon Mar 07, 2011 1:50 am

Re: box2dLights

Postby tescott » Sun Dec 11, 2011 4:40 pm

This is extremely cool! I'd really like to throw this in my game!!! However, I'm seeing a few unexpected issues:

1) The light tends to bleed outside the boundaries of my levels. Click on the screenshots below to expand things out so you can see what's up. I can change the order of the rendering so that my tiles are 'above' the rendered light to prevent bleed through on top of the tiles, but the bleed through will still be apparent because I simply don't draw anything in those 'black' areas.

Here's the line of code I'm using:

Code: Select all
PointLight light = new PointLight(GameRenderer.getGameRenderer().getRayHandler(), 64, false, false, new Color(1,0,0,1), 16, 0, 0);


BleedThrough.png
This shows a "light edge" that extends outside the boundary of my level and some minor bleed through
BleedThrough.png (347.03 KiB) Viewed 43797 times

BleedThrough2.png
This shows some bleed through.
BleedThrough2.png (344.62 KiB) Viewed 43797 times


2) If I change the light distance to something smaller, then the light / shadow appears to invert. The first two screenshots have a light distance of 16 and appear to work okay (apart from the bleed through as I mentioned). The third screenshot has a light distance of 4 and the shadowing effect appears to be inverted.

Code: Select all
PointLight light = new PointLight(GameRenderer.getGameRenderer().getRayHandler(), 64, false, false, new Color(1,0,0,1), 4, 0, 0);


Inverted.png
This shows what appears to be inversion going on with light.
Inverted.png (349.86 KiB) Viewed 43797 times


Thanks!
--tim
tescott
 
Posts: 487
Joined: Tue Apr 12, 2011 2:32 am

Re: box2dLights

Postby pitbuller » Sun Dec 11, 2011 9:23 pm

Thanks for testing and reporting. It's really nice to see that other ppl can benefit this project too.

For your problems.

Try to mess up with public field float softShadowLenght;
It's set to 5 at default. That should fix almoust all your glitches. If you want softness off completly you can set public boolean soft to false.



I have planned for better api for modifications but I am kinda busy right now. I understand that maybe best use case for that lib is just customize for every one needs. But I aim API that can encapsulate all the hoops from end user.
pitbuller
 
Posts: 134
Joined: Mon Jul 25, 2011 9:07 am

Re: box2dLights

Postby DRing » Wed Dec 14, 2011 5:27 pm

So i got it working, but is there a reason why PositionalLight is abstract and you can create a direction light. Looking to make some headlights and this would look nice.
DRing
 
Posts: 104
Joined: Thu Oct 20, 2011 7:42 pm

Re: box2dLights

Postby pitbuller » Wed Dec 14, 2011 7:23 pm

All different lights are inherited from base class Light. Directional light is very special case for simulating sun. It acts like rays are coming from infinite distance so every ray are allways from same direction no matter where you look.

Other lights have position component. Currently there are Cone light and Point light these are inherited from positional light.

For headlight use coneLight.
pitbuller
 
Posts: 134
Joined: Mon Jul 25, 2011 9:07 am

Re: box2dLights

Postby pitbuller » Wed Dec 14, 2011 7:25 pm

Just get gl2.0 support working. RayHandler detect openGl mode automatically and should work just like with fixed pipeline.

I also added support for box2d filters so lights can go thriught some box2d objects.

Thanks for JFreu.
pitbuller
 
Posts: 134
Joined: Mon Jul 25, 2011 9:07 am

Re: box2dLights

Postby DRing » Wed Dec 14, 2011 9:46 pm

So thanks for the coneLight it works great, now the main problem Im having is trying to put multiple lights on a single body, they combine every time i try. And changing the x and y doesnt seem to do anything

Edit: seems that changing the directionDegree isnt doing anything
DRing
 
Posts: 104
Joined: Thu Oct 20, 2011 7:42 pm

Re: box2dLights

Postby pitbuller » Wed Dec 14, 2011 10:41 pm

DRing wrote:So thanks for the coneLight it works great, now the main problem Im having is trying to put multiple lights on a single body, they combine every time i try. And changing the x and y doesnt seem to do anything

Edit: seems that changing the directionDegree isnt doing anything


I haven't yet added good way to attach lights to bodies but I add that soon. This is my method that I use some at one of my game. This attach light to sprite. It's easy to edit this to work for bodies too.
So dont attach lights to just yet. I have to make api little better before its ready enough.
Code: Select all
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;

public class AttachedLight {

   Sprite sprite;
   ConeLight light;
   float offX;
   float offY;
        float originalDirection;

   public AttachedLight(ConeLight light, Sprite sprite, float offSetX,
         float offSetY,float originalDirection) {
      this.sprite = sprite;
      this.light = light;
      this.offX = offSetX;
      this.offY = offSetY;
      this.originalDirection =originalDirection;
   }

   public void update() {

      final float angle = sprite.getRotation();

      final float sin = MathUtils.sinDeg(angle);
      final float cos = MathUtils.cosDeg(angle);
      final float x = sprite.getX() + offX * cos - offY * sin;
      final float y = sprite.getX() + offX * sin + offY * cos;
      light.setPosAndDirection(x, y, angle + originalDirection);
   }
}
pitbuller
 
Posts: 134
Joined: Mon Jul 25, 2011 9:07 am

Re: box2dLights

Postby pitbuller » Mon Dec 26, 2011 7:59 pm

http://dl.dropbox.com/u/10960490/Uusi%2 ... index.html
New test case.

GL es2.0 light rendering is done adjustable size FBO now. After all lights are rendered lightmap is blurred(n times) with efficient gaussian blur filter described by here http://rastergrid.com/blog/2010/09/effi ... -sampling/
Only 10 texture look ups but result is exaclty same as with 9x9 kernel.

Becouse you can use lower size FBO. The performance is great. This give huge edge over fixed pipeline when fill rate is limited. In that test case I used 200x120 FBO and blurred it four times, there is 12 dynamic lights each with 256 ray cast. This give about 350fps with desktop.
On android this give about 30fps but keep in mind that that many so accurate light with 4 blur pass is totally overkill. Even one blur pass is enough.


FBO style also finally fix android shadow support.
pitbuller
 
Posts: 134
Joined: Mon Jul 25, 2011 9:07 am

Next

Return to Showcase

Who is online

Users browsing this forum: No registered users and 1 guest