Box2Dlights阴影不起作用

时间:2014-06-22 14:22:29

标签: libgdx box2d

我刚刚在我的LibGDX游戏中实现了Box2DLights。它的工作原理很好,但在我添加静态物体后,它们不会投射阴影。

As you can see, the wall doesn’t cast a shadow

然而,墙壁确实以某种方式使点光倍增。

It even multiply point light somehow

以下是我使用Box2D和Box2Dlights的代码部分:

Vector2 gravity = new Vector2(0,0);
debugRenderer = new Box2DDebugRenderer();
world = new World(gravity, false);

rayHandler = new RayHandler(world);
rayHandler.setCombinedMatrix(camera.combined);
rayHandler.setShadows(true);

// This adds wall tiles from TiledMap into obstacles and to box2d world         
for (MapObject wall : mapObjects) {                                             
    RectangleMapObject rmo = (RectangleMapObject) wall;                         

    if (rmo.getName() == null || !rmo.getName().contentEquals("window")) {      
        Rectangle rec = rmo.getRectangle();                                     


        obstacles.add(rec);                                                     
        rec.set(rec.x / 128 - mapRec.x, rec.y / 128 - mapRec.y,                 
                rec.width / 128, rec.height / 128);                             

        BodyDef bodyDef = new BodyDef();                                        
        bodyDef.type = BodyType.StaticBody;                                     
        bodyDef.position.set(rec.getX()+rec.width/2,rec.getY()+rec.height/2);  

        Body body = world.createBody(bodyDef);                                  

        PolygonShape groundBox = new PolygonShape();                            

        groundBox.setAsBox(rec.width/2,rec.height/2);                           

        body.createFixture(groundBox, 1.0f);                                    

        groundBox.dispose();                                                    
    }                                                                           
}                                       

//The point light and the cone light;                                                                                                                              
pointLight= new PointLight(rayHandler, 100, new Color(1,1,1,0.5f), 0.5f, myCharacter.getAdjustedPosX(), myCharacter.getAdjustedPosY());                            
coneLight = new ConeLight(rayHandler, 100, new Color(1,1,1,0.5f), 3, myCharacter.getAdjustedPosX(), myCharacter.getAdjustedPosY(), myCharacter.getRotation(), 30f);

渲染部分:

// After I have rendered walls sprites etc.                                                                                     
world.step(1/60f, 6, 2);                                                                                                        
rayHandler.setCombinedMatrix(camera.combined, camera.position.x, camera.position.y,camera.viewportWidth, camera.viewportHeight);
rayHandler.updateAndRender();                                                                                                   
debugRenderer.render(this.world, camera.combined);

我已经尝试了很多教程和示例中的内容,例如我添加了world.step(),但它们没有改变。

1 个答案:

答案 0 :(得分:2)

将柔和度长度设置为零后,阴影开始工作。

pointLight.setSoftnessLength(0);
coneLight.setSoftnessLength(0);