【问题标题】:Java / box2DLights - Wrong light positionJava / box2DLights - 错误的灯光位置
【发布时间】:2015-10-22 12:43:47
【问题描述】:

我将 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

【问题讨论】:

  • 您是否使用 box2D 的单位转换?尝试设置位置,例如( 0.5f, 0.5f )

标签: java libgdx box2d light box2dlights


【解决方案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 - 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

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

希望对你有帮助:)

【讨论】:

  • 其实你的想法不错,但不完全一样,因为(0,0)是屏幕的中心。所以比较复杂
  • 我尝试了您的解决方案,但效果不佳。例如,如果我尝试显示这些坐标 -> (50 - width/2)/width/2 and (50 - height/2)/height/2) 我将获得 x = -0,22... 和 y = -0,21...这比左下角更靠近中心,我们需要相反。我的屏幕 -> 860 像素,645 像素。我希望我足够清楚
  • 嗨 Simulacre,看看更新 2,x = -0.88,y = -0.84 而不是 (-0.22,-0.21)
  • 嗨米诺斯。太棒了,它正在工作!确实,我犯了一个小错误
【解决方案2】:

如果您距离0.5 并且您的光线照射到屏幕的一半以上,我只是假设50, 50 的位置不适合此屏幕。只需尝试将您的位置更改为较小的值。 Maybe your coordinates do not represent pixels but other units as it is recommended for box2d.

编辑:由于我不了解您的整个 libgdx 应用程序,我只是建议您深入了解CameraViewPort 等。例如 box2dlights RayHandler 可以通过 setCombinedMatrix 获取您的相机。您可能还希望将灯光与身体同步,将 box2d 世界与您的精灵同步。

【讨论】:

  • 就是这样。如果我选择 (0, 0),它在中心。如果我选择 (1,1),它在右上角,在顶部。如果我选择 (-1, -1),它就在左下角。如果我想照亮我的太阳系,怎么可能转换它?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
相关资源
最近更新 更多