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:
private Handler handler;
//Graphics
private OrthographicCamera camera;
private OrthogonalTiledMapRenderer renderer;
private static final int viewportWidth = 768;
private static final int viewportHeight = 768;
// Create a separate camera for box2dlights
OrthographicCamera b2dCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
//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(viewportWidth, viewportHeight);
camera.setToOrtho(true, viewportWidth, viewportHeight);
camera.position.set(0, viewportHeight / 2f, 0);
camera.update();
ScreenViewport viewport = new ScreenViewport(camera);
//Box2d
world = new World(new Vector2(0f, 0f), false);
handler.setWorld(world);
RayHandler.useDiffuseLight(false);
RayHandler.setGammaCorrection(false);
rayHandler = new RayHandler(world);
rayHandler.setWorld(world);
rayHandler.setAmbientLight(new Color(0f, 0f, 0f, 0.02f));
rayHandler.setBlurNum(5);
handler.setRayHandler(rayHandler);
b2dCam.setToOrtho(true);
b2dCam.update();
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();
b2dCam.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(b2dCam.combined.scale(Global.PPM, Global.PPM, Global.PPM), b2dCam.position.x / Global.PPM + 0.5f, b2dCam.position.y / Global.PPM + 0.5f, b2dCam.viewportWidth * b2dCam.zoom, b2dCam.viewportHeight * b2dCam.zoom);
rayHandler.updateAndRender();
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();
stage.dispose();
}
//Getters
public int getLevel() {
return level;
}
public World getWorld() {
return world;
}
//Setters
public void setLevel(int level) {
this.level = level;
}
Furthermore, here are my dynamic / static bodies
private void createBody() {
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.StaticBody;
def.position.set(getX() / Global.PPM, getY() / Global.PPM);
def.fixedRotation = true;
body = handler.getWorld().createBody(def);
PolygonShape shape = new PolygonShape();
shape.setAsBox(getWidth() / 2 / Global.PPM, getHeight() / 2 / Global.PPM, new Vector2(0.5f, 0.5f), 0);
body.createFixture(shape, 0.0f);
shape.dispose();
}
private Body createBody() {
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.DynamicBody;
def.position.set(getX() / Global.PPM, getY() / Global.PPM);
def.fixedRotation = false;
body = handler.getWorld().createBody(def);
body.setLinearDamping(10f);
PolygonShape shape = new PolygonShape();
shape.setAsBox(getWidth() / 2 / Global.PPM, getHeight() / 2 / Global.PPM);
body.createFixture(shape, 0.0f);
shape.dispose();
return body;
}
The lights have no longer got the * Global.PPM (which is 32):
pl = new PointLight(handler.getRayHandler(), 100, color, 12, x, y);
If really would appreciate your help!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.