-2

所以我使用 libgdx 库制作了一个 2d 游戏。最近我一直在尝试使用 box2dlights 库将照明系统合并到游戏中。但是,由于某种原因,点光源不会出现在屏幕上。

这是我的代码:

    private OrthographicCamera camera;
private SpriteBatch batch;
private ShapeRenderer sr;
private TextureAtlas textureAtlas;

    private RayHandler rayHandler;
    private World world;

    private TiledMap map;
private OrthogonalTiledMapRenderer renderer;

private float animationTime = 0;
private Player player;
private int X = 400;
private int currentDirection = 0;
private int bulletDirection = 0;
private boolean animationPlaying = true;
private boolean animationPlaying2 = true;
public Animation walking;
public Animation walkingLeft;
public Animation walkingRight;
public Animation walkingUp;

private CollisionObjects CO;
private ArrayList<playerPositions> allPlayers = new ArrayList<playerPositions>();

//modes
ArrayList<BulletWorks> bullets = new ArrayList<BulletWorks>();
public boolean leftMode = false;
public boolean frontMode = true;
public boolean rightMode = false;
public boolean upMode = false;
public ArrayList<positions> pos = new ArrayList<positions>();
public float zoom = 0.1f;
public float xposCamera = 0f;
public float yposCamera = 0f;
boolean keyProcessed = true;
boolean play = false;
private float currentPosx = 400;
private float currentPosy = 400;

private PointLight pt;

    public gameFirst(GameTrial game) {
        TmxMapLoader loader = new TmxMapLoader();
        map = loader.load("data/WhiteWater.tmx");
        batch = new SpriteBatch();
        renderer = new OrthogonalTiledMapRenderer(map);
        sr = new ShapeRenderer();

            camera = new OrthographicCamera();
            world = new World(new Vector2(0, 0), true);

            rayHandler = new RayHandler(world);
            rayHandler.setCulling(true);
            rayHandler.useDiffuseLight(true);
            rayHandler.setAmbientLight(0.2f, 0.2f, 0.2f,1.0f);



        player = new Player("prithvi2502", "currymonster69");
        CO = new CollisionObjects();
        allPlayers.add(CO.addPositionPlayers(player));

        pt = new PointLight(rayHandler, 50, Color.CYAN, 40, 400, 300);

        camera.zoom = .5f;
    }

    @Override
    public void show() {

    }


    @Override
    public void render(float delta) {
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Update
        renderer.setView(camera);
            rayHandler.update();

        rayHandler.setCombinedMatrix(camera.combined.cpy().scl(100));

        // Render
        renderer.render();

        batch.begin();
        inputUpdate();
        camera.update();
        characterUpdate();
        batch.end();

        rayHandler.render();
    }

    public void characterUpdate() {
            allPlayers = CO.updatePlayerPositions(player.getPlayerID(), player.getPlayerCurrentPosX(), player.getPlayerCurrentPosY(), allPlayers);
        currentPosx =  player.getPlayerCurrentPosX();
    currentPosy = player.getPlayerCurrentPosY();
        for(int i=0; i < allPlayers.size(); i++) {
            System.out.println(allPlayers.get(i).getPlayerID() + " " + allPlayers.get(i).getX() + " " + allPlayers.get(i).getY());
            currentPosx = allPlayers.get(i).getX();
            currentPosy = allPlayers.get(i).getY();
        }

        for (int i = 0; i < bullets.size(); i++) {
            if(bullets.get(i).getDirection() == 0) {
                    bullets.get(i).updateButtom();
                            if(bullets.get(i).didCollide(allPlayers) == true) {
                                    System.out.println("HIT");
                                    bullets.remove(i);
                            } else if(bullets.get(i).getY() > 0 && bullets.get(i).getY() < 1080) {
                                    bullets.get(i).draw(batch);
                            }else {
                                    bullets.remove(i);
                            }
            } else if(bullets.get(i).getDirection() == 1) {
                    bullets.get(i).updateLeft();
                            if(bullets.get(i).didCollide(allPlayers) == true) {
                                    System.out.println("HIT");
                                    bullets.remove(i);
                            } else if(bullets.get(i).getX() > 0 && bullets.get(i).getX() < 1920) {
                                    bullets.get(i).draw(batch);
                            } else {
                                    bullets.remove(i);
                            }
            } else if(bullets.get(i).getDirection() == 2) {
                    bullets.get(i).updateRight();
                            if(bullets.get(i).didCollide(allPlayers) == true) {
                                    System.out.println("HIT");
                                    bullets.remove(i);
                            } else if(bullets.get(i).getX() > 0 && bullets.get(i).getX() < 1920) {
                                    bullets.get(i).draw(batch);
                            } else {
                                    bullets.remove(i);
                            }
            } else if(bullets.get(i).getDirection() == 3) {
                    bullets.get(i).updateTop();
                            if(bullets.get(i).didCollide(allPlayers) == true) {
                                    System.out.println("HIT");
                                    bullets.remove(i);
                            } else if(bullets.get(i).getY() > 0 && bullets.get(i).getY() < 1080) {
                                    bullets.get(i).draw(batch);
                            } else {
                                    bullets.remove(i);
                            }
            }

        }
    }
    public void inputUpdate() {
            if(Gdx.input.isKeyPressed(Keys.A) ){
           player.moveLeft(camera, batch);
           currentDirection = 2;
        }else if(Gdx.input.isKeyPressed(Keys.D) ){
            player.moveRight(camera, batch);
            currentDirection = 1;
        }else if(Gdx.input.isKeyPressed(Keys.S) ){
            player.moveDown(camera, batch);
            currentDirection = 0;
        }else if(Gdx.input.isKeyPressed(Keys.W) ){
            player.moveUp(camera, batch);
            currentDirection = 3;
        }else if(Gdx.input.isKeyPressed(Keys.T) ){
            BulletWorks BW = new BulletWorks((1920/2) + 300, 1080/2, (int) currentPosx, (int) currentPosy);
            bulletDirection = 2;
            BW.setDirection(bulletDirection);
                bullets.add(BW);

        }else if(Gdx.input.isKeyPressed(Keys.SPACE)){
            BulletWorks BW = new BulletWorks(1920/2, 1080/2, (int) currentPosx, (int) currentPosy);

                player.shoot(camera, batch);
                if(currentDirection == 0){
                    bulletDirection = 0;

            }else if(currentDirection == 1){
                    bulletDirection = 1;

            }else if(currentDirection == 2){
                    bulletDirection = 2;

            }else if(currentDirection == 3){
                    bulletDirection = 3;

            }
                BW.setDirection(bulletDirection);
                bullets.add(BW);
        }else{

            if(player.frontMode){
                    setGameToNormal(player.getTexture());

            }else if(player.rightMode){
                    setGameToNormal(player.getTexture3());

            }else if(player.leftMode){
                    setGameToNormal(player.getTexture2());

            }else if(player.upMode){
                    setGameToNormal(player.getTexture4());

            }
                play = false;
        }
    }
    private void setGameToNormal(TextureRegion texture) {
            batch.draw(texture, 1920/2, 1080/2);
    }

    @Override
    public void resize(int width, int height) {
            camera.setToOrtho(false, 1920, 1080);
    }



    @Override
    public void hide() {


    }

    @Override
    public void pause() {


    }

    @Override
    public void resume() {


    }

    @Override
    public void dispose() {
            rayHandler.dispose();
            world.dispose();
            renderer.dispose();
            map.dispose();
            batch.dispose();
            textureAtlas.dispose();
    }

}

4

2 回答 2

0

我认为您尝试在每次 libgdx 刷新屏幕时创建的默认(黑色)背景上渲染灯光。黑色背景超重,灯光无法正确渲染。黑色“吞噬”了光。

当我第一次尝试 Box2D-lights 时,我遇到了同样的问题。在调用rayHandler.render();之前,我通过在整个屏幕上通过 1x1 像素纹理渲染彩色背景来解决它。

示例代码:

private SpriteBatch batch;
private World world;
private RayHandler rayHandler;
private Texture texture;
private OrthographicCamera camera;

@Override
public void show(){
    batch = new SpriteBatch();
    camera = new OrthographicCamera();
    camera.setToOrtho(false, 50, 50/16*9);
    camera.position.set(0, 0, 0);
    camera.update();
    world = new World(new Vector2(0, 0), true);
    texture = new Texture("white.png"); // 1x1 pixel texture
    rayHandler = new RayHandler(world);
    new PointLight(rayHandler, 32, Color.CYAN, 20, 0, 0);
}

@Override
public void render(float delta){
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    world.step(delta, 8, 3);

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(texture, -Gdx.graphics.getWidth()/2, -Gdx.graphics.getHeight()/2, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    batch.end();
    
    rayHandler.setCombinedMatrix(camera.combined);
    rayHandler.updateAndRender();
}

运行此代码,我得到以下结果

我不太确定这是否是最好的方法,但它对我有用:)

于 2015-04-27T09:41:33.307 回答
0

设置 rayHandler.setAmbientLight(1); 它默认设置为 0,这可能是问题。它为我解决了同样的问题!

于 2015-09-16T09:17:22.410 回答