1

我将 Libgdx 用于项目,更准确地说是 Box2DLights。

我的问题是以下一个:当我想放置一个新的“PointLight”时,它总是在屏幕的中心。如果我改变坐标,它就不起作用。

在我的“show()”方法中:

    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);

在我的“render()”方法中:

    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();

结果 :

在此处输入图像描述 现在,如果我尝试更改坐标:

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

在此处输入图像描述

... 没有光了

你知道怎么可能把灯放在我想要的地方吗?

编辑:我的屏幕尺寸:宽度 - 860px / 高度 - 645px

4

2 回答 2

1

如果(1,1)是右上角,(0,0)是左下角,(0.5,0.5)是屏幕中间,那么我建议这样做:插入您想要的值并除以例如,它通过屏幕的宽度和高度

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

更新 :

抱歉,我没有看到 (0,0) 是中心,所以我建议你改用它:

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

更新 2: 我认为你做的算术错误很小,假设你的

正如你所说,宽度 = 860 和你的高度 = 645

这是等式:

x= ((xPosition - 宽度/2)/宽度/2)

y= (yPosition - 高度/2)/高度/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

更接近 (-1,-1) aka :左下角

希望它有帮助:)

于 2015-10-22T14:43:43.663 回答
0

如果您走一段距离0.5并且您的光线照射到屏幕的一半以上,我只是假设该位置50, 50不适合该屏幕。只需尝试将您的位置更改为较小的值。也许您的坐标不代表像素,而是其他单位,建议用于 box2d

编辑:由于我不知道您的整个 libgdx 应用程序,我只是建议您更深入地Camera了解ViewPort. 例如 box2dlightsRayHandler可以通过setCombinedMatrix. 您可能还希望将灯光与身体和 box2d 世界与您的精灵同步。

于 2015-10-22T13:38:45.307 回答