2016-02-03 186 views
1

背景信息Libgdx/Java性能與崩潰光的Box2D

嗨我目前工作的一個小RPG :)今天我試圖執行一些簡單的光....

主問題

當我在我的項目中使用點光源時,fps會變得越來越慢......我有一臺非常好的電腦,所以它不能成爲我的CPU或CPU ......所以我錯過了什麼?

這裏有一個截圖enter image description here

您可以在左下角看到FPS:6 順便說一句,我禁用垂直同步,我的GPU是GTX 960 ...所以我真的不知道爲什麼我有這麼低的FPS ...

Player類:

package Mobs; 


public class Player { 

AnimatedSprite animatedSprite; 
SpriteBatch batch; 

Light licht = new Light(Color.WHITE,500); 


public int state = 0; 
public int netState = 1; 
float speed = 2f; 
public Vector2 position = new Vector2(256,256); 
public Vector2 networkPosition = new Vector2(0,0); 



public Player(){ 



} 


public void update(){ 

     state = 0; 

     if(Gdx.input.isKeyPressed(Keys.A)){ 
      position.x -= Gdx.graphics.getDeltaTime() * 100f; 


      state = 1; 
      //System.out.println(currentState); 
     } 
     if(Gdx.input.isKeyPressed(Keys.D)){ 
      position.x += Gdx.graphics.getDeltaTime() * 100f;  


      state = 2; 
      //System.out.println(currentState); 
     } 
     if(Gdx.input.isKeyPressed(Keys.W)){ 
      position.y += Gdx.graphics.getDeltaTime() * 100f; 


      state = 3; 
      //System.out.println(currentState); 
     } 
     if(Gdx.input.isKeyPressed(Keys.S)){ 
      position.y -= Gdx.graphics.getDeltaTime() * 100f; 


      state = 4; 
      //System.out.println(currentState); 
     } 



} 

public void setX(float x){ 

    position.x = x; 

} 

public void setY(float y){ 

    position.y = y; 

} 

public void draw(float f, float g, OrthographicCamera camera){ 

    position.x = f; 
    position.y = g; 

    //System.out.println("In beforeSetState : "+currentState); 
    animatedSprite.setState(state); 
    //System.out.println("In after : "+currentState); 
    animatedSprite.createAnimation(); 

    camera.position.set(f,g,0); 
    camera.update(); 

    licht.drawLight(camera, f+25 , g+25); 


    batch.setProjectionMatrix(camera.combined); 
    batch.begin(); 
    batch.draw(animatedSprite.convertAnimationTOframes(),f,g, Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); 
    batch.end(); 



} 


public void doSetup(){ 


    batch = new SpriteBatch(); 
    animatedSprite = new AnimatedSprite(); 


} 



public float getX(){ 


    return position.x; 


} 

public float getY(){ 


    return position.y; 


} 


} 

而我的 「光」 類:

package Screen; 


public class Light { 


World world; 
RayHandler rayHandler; 
PointLight pointLight; 
Body player; 
Box2DDebugRenderer debugRenderer; 



public Light(Color farbe, int radius) { 

    world = new World(new Vector2(0,0),false); 
    rayHandler = new RayHandler(world); 

    pointLight = new PointLight(rayHandler, 500 , farbe , radius, 0, 0); 
    pointLight.setSoftnessLength(0f); 

    debugRenderer = new Box2DDebugRenderer(); 


} 


public void drawLight(OrthographicCamera playerCam, float x, float y){ 

    world.step(1/60f, 8, 3); 


    debugRenderer.render(world, playerCam.combined); 

    pointLight.setPosition(x,y); 

    rayHandler.setCombinedMatrix(playerCam.combined); 
    rayHandler.updateAndRender(); 




} 

public void removeLights(){ 

    rayHandler.removeAll(); 
    pointLight.remove(); 


} 


} 

因爲我仍然得到了laggs,我的繼承人MainClass:

public class LauncherScreen implements Screen{ 

//----------------------------------------------------------- 
//-----------------idle Animation---------------------------- 
//----------------------------------------------------------- 

Map duengon; 

AnimatedSprite animationForMultiplayer; 
SpriteBatch spriteBatch; 
Player mySelf; 

OrthographicCamera mpPlayerCam; 
OrthographicCamera camera; 

static Client client = new Client(); 
Launcher launcher = new Launcher(); 


int[][] map = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}; 


@Override 
public void render(float delta) { 
    // TODO Auto-generated method stub 

    Gdx.gl.glClearColor(1, 1, 1, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 


    launcher.update(); 

    //------------------------------------------------------------------------ 
    //------------------------Draws the Map----------------------------------- 
    //------------------------------------------------------------------------ 


    duengon.ubdate(map, camera); 



    //------------------------------------------------------------------------ 
    //------------------------Draws the Players------------------------------- 
    //------------------------------------------------------------------------ 



    for(MPPlayer mpPlayer : launcher.getPlayersValue()){ 

     animationForMultiplayer.setState(mpPlayer.state); 

     animationForMultiplayer.createAnimation();   

     camera.position.set(mpPlayer.x,mpPlayer.y,0); 


     spriteBatch.setProjectionMatrix(camera.combined); 

     spriteBatch.begin();     
     spriteBatch.draw(animationForMultiplayer.convertAnimationTOframes(), mpPlayer.x, mpPlayer.y,Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15);  // #6 
     spriteBatch.end(); 

     System.out.println("mpPlayer : "+mpPlayer.x+" "+mpPlayer.y); 


    } 


    mySelf.update(); 
    mySelf.draw(launcher.getPlayerX(), launcher.getPlayerY(), camera); 

    camera.update(); 

    System.out.println(Gdx.graphics.getFramesPerSecond()); 

    System.out.println("player : "+launcher.getPlayerX()+" "+launcher.getPlayerY()); 



} 




@Override 
public void show() { 
    // TODO Auto-generated method stub 
    animationForMultiplayer = new AnimatedSprite(); 
    spriteBatch = new SpriteBatch();    
    mySelf = new Player(); 

    mySelf.doSetup(); 

    mpPlayerCam = new OrthographicCamera(0,0); 
    mpPlayerCam.setToOrtho(false); 


    camera = new OrthographicCamera(0, 0); 
    camera.setToOrtho(false); 

    duengon = new Map(); 



} 


@Override 
public void resize(int width, int height) { 
    // TODO Auto-generated method stub 

} 


@Override 
public void hide() { 
    // TODO Auto-generated method stub 

} 




@Override 
public void pause() { 
    // TODO Auto-generated method stub 

} 




@Override 
public void resume() { 
    // TODO Auto-generated method stub 

} 




@Override 
public void dispose() { 
    // TODO Auto-generated method stub 

    spriteBatch.dispose(); 
    mySelf.doDispose(); 
    animationForMultiplayer.doDispose(); 
    duengon.doDispose(); 


} 






} 

的.doDispose()在我的mainClass是從類處置的資源使用

感謝您的幫助和時間的方法:)

+0

'player.createFixture(polygonFixture);'做了什麼? – Andonaeus

+0

它附加box2d動態對象播放器:) thx看着我的代碼,它不是經常,我得到一個答案 – genaray

+1

沒問題。你介意用包含的代碼編輯你的文章嗎?僅供參考,我假設您在每個抽獎週期不知不覺地創造資源,而不是將其釋放。這會導致渲染速度減慢,因爲它必須越來越多。我試圖找出可能發生的地方。 – Andonaeus

回答