java - Libgdx/java 使用 box2d light 时性能崩溃

标签 java libgdx box2d light

背景信息

嘿伙计们,我目前正在制作一个小角色扮演游戏:),今天我尝试实现一些简单的灯光....

主要问题

当我在项目中使用点光时,fps 会变得越来越慢...我有一台非常好的电脑,所以它不可能是我的 GPU 或 cpu ...那么我错过了什么?

这是屏幕截图 enter image description here

您可以在左下角看到 fps:6。 顺便说一句,我禁用了垂直同步,而我的 GPU 是 gtx 960 ...所以我真的不知道为什么我的 fps 这么低...

玩家等级:

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;


}


}

还有我的“Light”类(class):

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


}


}

因为我仍然有滞后,这是我的 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();


}






 }

我的 mainClass 中的 .doDispose() 是从正在使用的类中释放资源的方法

感谢您的帮助和您的时间:)

最佳答案

您没有处置任何东西,您可能想查看 LibGDX 的 dispose() 函数 链接在这里 https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Disposable.html

如果这不是问题,请告诉我们。

关于java - Libgdx/java 使用 box2d light 时性能崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35186711/

相关文章:

java - 如何排除已验证的链接验证?

resize - LibGDX : How can 9patch be used to create a sprite?

ios - 如何根据iOS设备定义PTM_RATIO

java - 使用textureRegion绘图

Java 10 Eclipse + Maven "java.lang.module.FindException: Module myproject not found"

java - RandomStringUtils.randomAlphanumeric(30) 是有效的 GUID 策略吗?

java - 有趣的 HashMap 实现(build 1.7.0_25-b17)

java - LibGdx Facebook分数

java - 创建 Box2d LibGDX 后我的 body 有点下降

java - 如何循环遍历包含 map 的ArrayList?