I tried to implement Box2dLights in my game and it also works pretty well. But I cannot get shadows working although I read a lot of articles and watched a lot of videos but I just cannot figure out what my mistake is. Maybe you can, this would be just great. Here is my code:
public class GameScreen implements Screen {
//Handler
private Handler handler;
//Graphics
private OrthographicCamera camera;
private Box2DDebugRenderer b2dr;
private OrthogonalTiledMapRenderer renderer;
//World
private RoomStage stage;
private World world;
private RayHandler rayHandler;
//Creatures
private Player player;
//Game
private int level = 0;
private FPSLogger fps;
public GameScreen(Handler handler) {
this.handler = handler;
}
//METHODS
@Override
public void show() {
fps = new FPSLogger();
handler.setLevel(level);
//Graphics
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
ScreenViewport viewport = new ScreenViewport(camera);
//Box2d
world = new World(new Vector2(0f, 0f), false);
b2dr = new Box2DDebugRenderer();
handler.setWorld(world);
RayHandler.setGammaCorrection(true);
RayHandler.useDiffuseLight(true);
rayHandler = new RayHandler(world);
rayHandler.setAmbientLight(new Color(0f, 0f, 0f, 0.05f));
rayHandler.setBlurNum(3);
rayHandler.setShadows(true);
handler.setRayHandler(rayHandler);
stage = new RoomStage(handler, viewport, "start");
handler.setStage(stage);
Gdx.input.setInputProcessor(stage);
stage.loadRoom();
//Creatures
player = new Player(handler, 12.5f * Global.PPM, 1f * Global.PPM);
stage.addActor(player);
stage.setKeyboardFocus(player);
renderer = new OrthogonalTiledMapRenderer(stage.getMap());
}
@Override
public void render(float delta) {
update(Gdx.graphics.getDeltaTime());
//Clear and Update
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
//Render
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setView(camera);
int[] backgroundLayers = new int[] {0, 1};
int[] foregroundLayers = new int[] {2, 3};
renderer.render(backgroundLayers);
stage.draw();
renderer.render(foregroundLayers);
rayHandler.setCombinedMatrix(camera);
rayHandler.setShadows(true);
rayHandler.update();
rayHandler.render();
fps.log();
}
public void update(float delta) {
stage.act(Gdx.graphics.getDeltaTime());
world.step(1 / 60f, 6, 2);
rayHandler.update();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
rayHandler.dispose();
world.dispose();
b2dr.dispose();
stage.dispose();
}
//Getters
public int getLevel() {
return level;
}
public World getWorld() {
return world;
}
//Setters
public void setLevel(int level) {
this.level = level;
}
}
And here are the lights:
//Light
rayHandler = handler.getRayHandler();
for(int x = 0; x < lightLayer.getWidth(); x++) {
for(int y = 0; y < lightLayer.getHeight(); y++) {
Cell cell = lightLayer.getCell(x, y);
if(cell != null && cell.getTile() != null) {
if(cell.getTile().getId() != 0) {
box2dLight.PointLight pl = new box2dLight.PointLight(handler.getRayHandler(), 20, new Color(4f, 1f, 0f, 0.2f), 12 * Global.PPM, x * Global.PPM + Global.PPM / 2, y * Global.PPM + Global.PPM / 2);
pl.setXray(false);
pl.setActive(true);
}
}
}
}
As I said light is already appearing and physics etc. work just fine but there is not shadow visible although there're bodies added to the world. Hope anyone can help me.
Oh.. yeah I actually kind of solved it already by reading this question: Unable to get Box2dLigh... Using this post I got them working, but now the only appear at 0,0 of the object which looks kind of weird: Shadow casted at the wrong position
Here is my Screen-Class:
Furthermore, here are my dynamic / static bodies
The lights have no longer got the * Global.PPM (which is 32):
If really would appreciate your help!