Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX box2dlight gives the wrong shadow offset  (Read 448 times)
0 Members and 1 Guest are viewing this topic.
Offline benberi

Senior Newbie





« Posted 2014-07-08 16:18:25 »

Take a look at this gif picture:

Click to Play


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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
    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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
      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);


Click to Play


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.
Offline Herjan
« Reply #1 - Posted 2014-07-08 16:42:19 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
      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);


I'm sorry, but this is not a square Wink

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

Offline benberi

Senior Newbie





« Reply #2 - Posted 2014-07-08 16:59:42 »

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



Why? :S


1  
2  
      PolygonShape chain = new PolygonShape();
      chain.setAsBox(texture.getWidth() / 2, texture.getHeight() / 2);
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2014-07-08 17:09:59 »

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

Senior Newbie





« Reply #4 - Posted 2014-07-08 18:37:35 »

Thanks for debug mode.

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


Click to Play


Is there anything wrong? Because I think there is, the light should make the sprite bright shouldnt it?
Offline pitbuller
« Reply #5 - Posted 2014-07-08 19:49:35 »

Box2dLights does not know anything about your sprites. It just blindly raycast all over places and stop when its collide something.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (39 views)
2014-10-17 03:59:02

Norakomi (30 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (58 views)
2014-10-14 00:35:47

TehJavaDev (48 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!