LibGDX box2dlight gives the wrong shadow offset

Take a look at this gif picture:

As you see the shadow acts as there is something else besides the sprite.
I think it’s because I set a circle ball with radius and the shadow only acts with the body’s shape.


    this.texture = new Texture("sprites/water.png");
    this.camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    this.camera.setToOrtho(false);
    this.renderer = new SpriteBatch();

    this.map = new TileMap(20, 20);
    this.map.generateAllSame(new Texture("sprites/sprite.png"));     

    this.world = new World(new Vector2(), true);

    this.createBodies();

    RayHandler.setGammaCorrection(true);
    RayHandler.useDiffuseLight(true);
    this.ray = new RayHandler(this.world);
    this.ray.setAmbientLight(0.2f, 0.2f, 0.2f, 0.1f);
    this.ray.setCulling(true);
    this.ray.pointAtLight(50, 0);
    this.ray.setBlurNum(1);
    camera.update(true);
    this.spriteLight = new PointLight(ray, 128);
    this.spriteLight.setDistance(500f);
    this.spriteLight.setPosition(150, 150);

    this.spriteLight.setColor(new Color(0.5f, 0.8f, 0.7f, 1f)); 
    this.spriteLight.setSoftnessLength(0);
    this.spriteLight.setSoft(true);


public void createBodies() {
    CircleShape chain = new CircleShape();
    chain.setRadius(10);

    FixtureDef def = new FixtureDef();
    def.restitution = 0.8f;
    def.friction = 0.01f;
    def.shape = chain;
    def.density = 1f;


    BodyDef body = new BodyDef();
    body.type = BodyType.DynamicBody;

    for (int i = 0; i < 3; i++) {
        body.position.x = 40 + MathUtils.random(500);
        body.position.y = 40 + MathUtils.random(500);

        Body box = this.world.createBody(body);
        box.createFixture(def);
        this.bodies.add(box);
    }
    chain.dispose();
}



My render:


public void render() {
    handleInput();
    camera.update();
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    renderer.setProjectionMatrix(camera.combined);

    renderer.disableBlending();
    renderer.begin();
    this.map.render(renderer);
    renderer.draw(texture, 50, 50);

    renderer.enableBlending();

    for (Body body : this.bodies) {
        Vector2 pos = body.getPosition();
        renderer.draw(texture, pos.x + amount, pos.y);
    }
    renderer.end();

    ray.setCombinedMatrix(camera.combined, camera.position.x, camera.position.y, 
            camera.viewportWidth * camera.zoom, camera.viewportHeight * camera.zoom);

    ray.update();
    ray.render();


}

And then I set a square polygon instead of a circle:

		PolygonShape chain = new PolygonShape();
		Vector2[] v = new Vector2[4];
		
		v[0] = new Vector2(10f, 0f);
		v[1] = new Vector2(45f, 0f);
		v[2] = new Vector2(10f, 0f);
		v[3] = new Vector2(0f, 45f);
		
		chain.set(v);

This is a lot better, but as you can see, the shadow goes OVER the sprite itself, and it makes it look unrealistic. Looks like it’s missing some offsets for bodies.
What did I do wrong? I just started LibGDX in general.

I’m sorry, but this is not a square :wink:

Try chain.setAsBox(w/2, h/2);

Hey thanks that worked, but theres still an offset problem:

Why? :S


		PolygonShape chain = new PolygonShape();
		chain.setAsBox(texture.getWidth() / 2, texture.getHeight() / 2);

Try to setup box2d debug renderer to see where you should put your sprites instead of doing all that guess work.

Thanks for debug mode.

renderer.draw(texture, pos.x - texture.getWidth() / 2, pos.y - texture.getHeight() / 2);

Is there anything wrong? Because I think there is, the light should make the sprite bright shouldnt it?

Box2dLights does not know anything about your sprites. It just blindly raycast all over places and stop when its collide something.