1

I'm using Libgdx for a project and more precisely Box2DLights.

My problem is the following one : When I want to put a new "PointLight" it's always on the center of the screen. And if I change the coordinates, it doesn't work.

Inside my "show()" method :

    Box2D.init();
    world = new World(new Vector2(0, 0), true);

    rh = new RayHandler(world);
    rh.setAmbientLight(1.2f, 0.2f, 0.2f, 0.1f);
    pl = new PointLight(rh, 100, new Color(1,1,1,1),(float) 0.5,0,0);

Inside my "render()" method :

    Gdx.gl.glClearColor(0f, 0f, 0f, 1f); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    world.step(delta, 8, 3);

    renderer.begin(ShapeType.Filled);
    for (SolarSystem ss : solarSystemList)
    {
        if(ss.getColor() <= 15) colorSS = Color.YELLOW;
        else if(ss.getColor() > 15 && ss.getColor() < 31) colorSS = Color.ORANGE;
        else if(ss.getColor() > 30 && ss.getColor() < 46) colorSS = Color.RED;
        else if(ss.getColor() > 45) colorSS = Color.CYAN;

        renderer.setColor(colorSS);
        renderer.circle(ss.getMapX(), ss.getMapY(), ss.getSize() - 3);
    }
    renderer.end();

    rh.updateAndRender();

Result :

enter image description here Now if I try to change coordinates :

pl = new PointLight(rh, 100, new Color(1,1,1,1),(float) 0.5, 50, 50);

enter image description here

... no light anymore

Do you know how it's possible to put the light where I want ?

EDIT : My screen size : width - 860px / height - 645px

Sebastian
  • 5,721
  • 3
  • 43
  • 69
Simulacre
  • 89
  • 6

2 Answers2

1

if the (1,1) is the top right and the (0,0) is bottom left and the (0.5,0.5) is the middle of the screen, then i propose to do this : insert the value that you want and divide it by the width and height of of your screen for example

 ( xPosition/Gdx.graphics.width, yPosition/Gdx.graphics.height ) 

Update :

sorry i didn't see that (0,0) was the center so i propse to you to use this instead :

width  = Gdx.graphics.width;
height = Gdx.graphics.height;
((xPosition - width/2)/ width/2 , (yPosition - height/2)/ height/2)

Update 2 : i think you are doing little arithmetic mistake assume that your

width = 860 and your height = 645 as you said

this is the equation :

x= ((xPosition - width/2)/ width/2)

y= (yPosition - height/2)/ height/2)

x = (50 - 860/2) / (860/2)

y = (50 - 645/2) / (645/2)

x = (50 - 430) / (430)

y = (50 - 322.5) / (322.5)

x = (50 - 430) / (430) = (-380) / (430)

y = (50 - 322.5) / (322.5) = (-272.5) / (322.5)

x = -0.88

y = -0.84

which is closer to (-1,-1) aka : the left bottom corner

hope it was helpful :)

Netero
  • 3,761
  • 2
  • 29
  • 29
  • In fact, your idea is good but it's not exactly like that because (0,0) is the center of the screen. So it's more complicated – Simulacre Oct 22 '15 at 16:10
  • I tried your solution but it doesn't work so well. For example if I try to display these coordinates -> (50 - width/2)/width/2 and (50 - height/2)/height/2) I will obtain x = -0,22... and y = -0,21... This is closer to the center than the left bottom corner and we need to have to the opposite. My screen -> 860px, 645px. I hope I'am clear enough – Simulacre Oct 25 '15 at 09:43
  • hi Simulacre, take a look at Update 2, x = -0.88, y = -0.84 and not (-0.22,-0.21) – Netero Oct 25 '15 at 10:24
  • Hi Minos. Fabulous, it's working ! And indeed, I did a little mistake – Simulacre Oct 25 '15 at 12:04
0

If you take a distance of 0.5 and your light shines above half of the screen I just assume that a position of 50, 50 will not fit into this screen. Just try to change your position to a smaller value. Maybe your coordinates do not represent pixels but other units as it is recommended for box2d.

Edit: As I don't know your entire libgdx app I just recommend to have a deeper look into Camera, ViewPort and such. For example the box2dlights RayHandler can get your camera via setCombinedMatrix. You may also want to synchronize lights with bodies and the box2d world with your sprites.

Community
  • 1
  • 1
Sebastian
  • 5,721
  • 3
  • 43
  • 69
  • That's it. If I choose (0, 0), it's on the center. If I choose (1,1), it's in the right corner, on the top. And if I choose (-1, -1), it's below, in the left corner. If I want to put light on my solar systems, how it's possible to convert that ? Thx – Simulacre Oct 22 '15 at 13:46