0

I am using box2dlights in my project and there seem to be an issue. When I actually use RayHandler in the program, only the light is drawn but no textures. And so when I remove RayHandler from the render loop it all works fine. I want the light to be drawn and then textures to be drawn above it. How do I reach that? This is the code:

public class GameScreen implements Screen {

private Core core;

private SpriteBatch batch;
private Sprite spiderSprite;
private OrthographicCamera camera;
private RayHandler rayHandler;
private Box2DDebugRenderer debugRenderer;

private World world;
private Body spider, prop;
private PointLight spiderLight;

private Stage stage;

private boolean paused, lost, check;
private int pointsCount;
private float time;


public GameScreen(Core c) {
    core = c;

    stage = new Stage();

    batch = new SpriteBatch();
    camera = new OrthographicCamera();
    camera.setToOrtho(false, core.SCREEN_WIDTH, core.SCREEN_HEIGHT);
    batch.setProjectionMatrix(camera.combined);
    debugRenderer = new Box2DDebugRenderer();

    spiderSprite = core.chars.createSprite("spider");

    world = new World(new Vector2(.0f, -10.0f), true);

    BodyDef spiderDef = new BodyDef();
    spiderDef.type = BodyDef.BodyType.DynamicBody;
    spiderDef.position.set(core.SCREEN_WIDTH / 2, core.SCREEN_HEIGHT / 2);
    spider = world.createBody(spiderDef);
    CircleShape shape = new CircleShape();
    shape.setRadius(core.SCREEN_WIDTH / 15);
    spider.createFixture(shape, .1f);

    BodyDef propDef = new BodyDef();
    propDef.type = BodyDef.BodyType.StaticBody;
    propDef.position.set(core.SCREEN_WIDTH / 2, core.SCREEN_HEIGHT);
    prop = world.createBody(propDef);
    CircleShape propShape = new CircleShape();
    propShape.setRadius(0.1f);
    prop.createFixture(propShape, 1.0f);

    RopeJointDef rope = new RopeJointDef();
    rope.bodyA = spider;
    rope.bodyB = prop;
    rope.maxLength = core.SCREEN_HEIGHT / 2;
    world.createJoint(rope);

    rayHandler = new RayHandler(world);
    rayHandler.setAmbientLight(.5f);

    spiderLight = new PointLight(rayHandler, 100, Color.WHITE, core.SCREEN_WIDTH, 100, 100);
    spiderLight.attachToBody(spider);
}

@Override
public void show() {
    Gdx.input.setInputProcessor(stage);
    stage.addAction(Actions.fadeIn(0.85f));
}

@Override
public void render(float delta) {
    clearScreen();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    if (!lost && !paused) {
        updateWorld();
        updateSpider();
    }
    drawSpider();
    stage.act();
    stage.draw();
    batch.end();
}

private void clearScreen() {
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

}

private void updateSpider() {
    //spider.setLinearVelocity(Gdx.input.getAccelerometerX() > 0 ? 50.0f : -50.0f, 0.0f); 
    float angle = MathUtils.radiansToDegrees * MathUtils.atan2(core.SCREEN_HEIGHT - spider.getPosition().y, core.SCREEN_WIDTH / 2 - spider.getPosition().x) - 90;
    spiderSprite.setPosition(spider.getPosition().x, spider.getPosition().y);
    spiderSprite.setRotation(angle);
}

private void drawSpider() {
    spiderSprite.draw(batch);
    rayHandler.render();
}

private void updateWorld() {
    world.step(1/60f, 6, 2);
    rayHandler.setCombinedMatrix(camera);
    rayHandler.update();
    time += Gdx.graphics.getDeltaTime();
}

}

I also removed a redundant part of the code which is not connected with the issue.

S. O. Chaos
  • 136
  • 1
  • 7

2 Answers2

0

Stage has it's own batch with it's own .begin() and .end(). You should call batch.end(); before calling stage.draw();

p.streef
  • 3,652
  • 3
  • 26
  • 50
  • As I mentioned, the matter is in Box2DLights, because without it everything works fine, including drawing stage between `batch.begin()` and `batch.end()` – S. O. Chaos Apr 06 '17 at 06:00
0

I found it out. RayHandler.render() must not be invoked between batch.begin() and batch.end().

S. O. Chaos
  • 136
  • 1
  • 7