I'm trying to use box2dlights to add lighting to my game and I have everything working except I am unable to have shadows cast with my box2d bodies. I also have collision working using category/maskBits with my various objects and I'm trying to apply the same strategy to box2dlight filters. You can see how the light just passed through the body.
EDIT: I think I may have discovered why the lights are not colliding with my box2d bodies. When I define the body shapes using "real world" scale pixel coordinates, the lights cast shadows properly, but I lose collision since the bodies are defined at pixel scale as opposed to the box2d scale I have defined. Does anyone know why the box2dlights are using real world scale?
Here is my RayHandler Setup
RayHandler.useDiffuseLight(false); rayHandler = new RayHandler(world); rayHandler.setAmbientLight(.5f, .5f, .2f, 1f); rayHandler.setBlurNum(1); rayHandler.setShadows(true);
I have a PointLight that is positioned in the center of the screen.
PointLight pl = new PointLight(game.rayHandler, 128); pl.setDistance(1000); pl.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2); pl.setColor(Color.WHITE); pl.setSoftnessLength(0); Filter filter = new Filter(); filter.categoryBits = B2DVars.CATEGORY_LIGHT; // Value listed below filter.maskBits = B2DVars.MASK_LIGHT; // Value listed below pl.setContactFilter(filter);
Also, my body is defined as
body = world.createBody(squareBodyDef); PolygonShape squareShape = new PolygonShape(); squareShape.setAsBox(b2dWidth / 2, b2dWidth / 2, new Vector2(squareBodyDef.position.x * WORLD_TO_BOX, squareBodyDef.position.y * WORLD_TO_BOX), 0f); body.setUserData(this); FixtureDef fdef = new FixtureDef(); fdef.shape = squareShape; fdef.density = 10.0f; fdef.friction = 0.1f; fdef.restitution = 1.15f; fdef.filter.categoryBits = CATEGORY_SQUARE_OBJECTIVE; // Value listed below fdef.filter.maskBits = MASK_SQUARE; // Value listed below body.createFixture(fdef).setUserData(this); squareShape.dispose();
Finally, my box2d filtering constants
public static final short CATEGORY_BALL = 1; public static final short CATEGORY_LINE = 2; public static final short CATEGORY_WALL = 4; public static final short CATEGORY_SQUARE_OBJECTIVE = 8; public static final short CATEGORY_TRIANGLE_OBJECTIVE = 16; public static final short CATEGORY_LIGHT = 32; public static final short LIGHT_GROUP = 1; public static final short MASK_BALL = CATEGORY_LINE | CATEGORY_WALL | CATEGORY_SQUARE_OBJECTIVE | CATEGORY_TRIANGLE_OBJECTIVE; public static final short MASK_LIGHT = CATEGORY_LINE | CATEGORY_SQUARE_OBJECTIVE | CATEGORY_TRIANGLE_OBJECTIVE | CATEGORY_WALL; public static final short MASK_SQUARE = CATEGORY_BALL | CATEGORY_LIGHT; public static final short MASK_WALL = -1
I've been going non-stop for about 3 days trying to solve this problem to no avail. Any help is appreciated!