2014-06-22 70 views
1

我剛在我的LibGDX遊戲中實現了Box2Dights。它工作正常,但在我添加靜態物體後,它們不會投射陰影。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(),但他們沒有改變一件事。

回答

2

將柔和長度設置爲零後陰影開始工作。

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