Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
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 Trying to get my head around box2d lights  (Read 1297 times)
0 Members and 1 Guest are viewing this topic.
Offline phunni

Senior Newbie


Exp: 17 years



« Posted 2017-09-19 13:54:03 »

I've written a simple libGDX screen to experiment with lighting and shadows and was delighted to discover how easy it all was. I think, however, there must be something I'm missing...

With 2 lights and 2 objects, the shadows are nearly, but not quite, what I'd expect.  Here's a screenshot:



Here's the code used to create this:

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  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
public class LightsScreen extends RPGPlaygroundScreen {

    private RayHandler rayHandler;
    private World world;
    private Body squareBody;
    private Body circleBody;

    private Box2DDebugRenderer debugRenderer;

    public LightsScreen(Game game) {
        super(game);
    }

    @Override
    public void render(float delta) {
        super.render(delta);
        rayHandler.setCombinedMatrix(gameCamera);
        rayHandler.updateAndRender();
        debugRenderer.render(world, gameCamera.combined);
    }

    @Override
    public void show() {
        super.show();
        world = new World(new Vector2(0, -9.8f), true);
        debugRenderer = new Box2DDebugRenderer();
        float distance = 12;
        int rays = 128;
        rayHandler = new RayHandler(world);
        rayHandler.setAmbientLight(0f, 0f, 0f, 0.5f);
        new PointLight(rayHandler, rays, Color.SKY, distance
                , (gameViewport.getWorldWidth() / 4)
                , (gameViewport.getWorldHeight() / 4) * 3 );
     
        new PointLight(rayHandler, rays, Color.PINK, distance
                , (gameViewport.getWorldWidth() / 4) * 3
                , (gameViewport.getWorldHeight() / 4) * 3);
        createSquareBody();
        createShericalBody();
    }


    @Override
    public void dispose() {
        super.dispose();
        rayHandler.dispose();
        debugRenderer.dispose();
    }

    private void createSquareBody() {
        //create definition
        BodyDef squareBodyDef = new BodyDef();
        squareBodyDef.type = BodyDef.BodyType.StaticBody;

        //set position
        squareBodyDef.position.set(gameViewport.getWorldWidth() / 3
                , gameViewport.getWorldHeight() / 3);

        //create body and add it to the world
        squareBody = world.createBody(squareBodyDef);


        //create shape
        PolygonShape squareBox = new PolygonShape();
        squareBox.setAsBox(1, 1);

        //create a fixture from the shape and add it to body
        squareBody.createFixture(squareBox, 2.0f);

        squareBox.dispose();
    }

    private void createShericalBody() {
        //create definition
        BodyDef circleBodyDef = new BodyDef();
        circleBodyDef.type = BodyDef.BodyType.StaticBody;

        //set position
        circleBodyDef.position.set((gameViewport.getWorldWidth() / 3) * 2
                , gameViewport.getWorldHeight() / 3);


        //create body and add it to the world
        circleBody = world.createBody(circleBodyDef);

        //create shape
        CircleShape shape = new CircleShape();
        shape.setRadius(1);

        //create a fixture from the shape and add it to body
        circleBody.createFixture(shape, 2.0f);
        shape.dispose();
    }
}


You can clearly see that the shadows don't quite start where the object does.  I've found that it improves if I add rays, but only up to about 128 - which is good, since I wouldn't want to go higher than that anyway...

I've tried playing around with object density and restitution, without any effect.  It's possible that I'm simply misunderstanding how it's supposed to work, or, perhaps, thinking in too few dimensions...

Do I just have unrealistic expectations? Is it actually working as intended? Am I simply being an idiot?
Offline 65K
« Reply #1 - Posted 2017-09-19 14:16:59 »

Does calling PointLight#setSoftnessLength(0) help ?

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
Offline phunni

Senior Newbie


Exp: 17 years



« Reply #2 - Posted 2017-09-19 16:31:11 »

Indeed it does:



although I suppose you could argue it's gone too far the other way now - gives me a starting point for experimentation though - thanks!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #3 - Posted 2017-09-19 16:41:18 »

It's not perfect.

If you want smoother lights without jitter, try: https://github.com/piotr-j/box2dlights
If you want smoother lights without jitter and crashes, try: https://github.com/voodoosoft/box2dlights

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (149 views)
2017-09-30 02:57:34

theagentd (222 views)
2017-09-26 18:23:31

cybrmynd (301 views)
2017-08-02 12:28:51

cybrmynd (288 views)
2017-08-02 12:19:43

cybrmynd (298 views)
2017-08-02 12:18:09

Sralse (291 views)
2017-07-25 17:13:48

Archive (978 views)
2017-04-27 17:45:51

buddyBro (1102 views)
2017-04-05 03:38:00

CopyableCougar4 (1678 views)
2017-03-24 15:39:42

theagentd (1430 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!