RayHandler不适合Box2D世界

时间:2018-11-18 16:51:13

标签: libgdx jbox2d box2dlights

我处境艰难。

我正在制作2D游戏,现在正在前进 box2Dlights部分的集成。

但是当我渲染RayHandler时,我有一个黑屏。 image

这是另一张图片,我不渲染灯光。现在没有灯。image 我需要将Box2D和Box2Dlights的碰撞混合在一起。 请注意

  • 我所有的盒子都是用单位制成的,等于1,所以一个房间的长度是16平方厘米。
  • 整个世界放大setToOrtho((16 - 1.875f) / Gdx.graphics.getHeight()),其中16是图块大小。
  • 如果缩放瓷砖大小,我希望可以使用此东西。

所有代码都很复杂,因此我现在仅发送GameScreen和GameMap类:

@Override
public void show() {
    InstanceVars inst = InstanceVars.getInstance();
    inst.world = new World(new Vector2(0, 0f), true);
    inst.engine = new Engine();
    inst.handler = new RayHandler(inst.world);

    LevelCreator creator = new LevelCreator("level_def.cfg");

    try {
        gameWorld = creator.createNewLevel();
        gameWorld.start();
    } catch (FileCorruptedException e) {
        e.printStackTrace();
    }
}

@Override
public void render(float delta) {

    gameWorld.render();
}

@Override
public void resize(int width, int height) {
    gameWorld.resize(width, height);
}

@Override
public void dispose() {
    gameWorld.dispose();
    InstanceVars.getInstance().dispose();
} 

用于 GameScreen.java

private World physWorld = InstanceVars.getInstance().world;
private Box2DDebugRenderer physRender = new Box2DDebugRenderer();
private OrthographicCamera cam = new OrthographicCamera();
private RayHandler lights = new RayHandler(physWorld);
private PlayerController controls;
private float STD_ZOOM = ((float) CELL_SIZE + 16) / Gdx.graphics.getHeight();
private Body playerBody;

public GameMap() {
    cam.zoom = STD_ZOOM;
}

public void start() {
    GameObject player = (GameObject) getProperties().get("ent_player");
    playerBody = player.getComponent(PhysicsComponent.class).getBody();

    controls = new PlayerController();
    Gdx.input.setInputProcessor(controls);
    physWorld.setContactListener(new CollisionInteractor(InstanceVars.getInstance().engine));

    PointLight light = new PointLight(lights, 1000, Color.GOLDENROD, 500, playerBody.getPosition().x, playerBody.getPosition().y);
}

public void render() {
    updatePlayer();
    physWorld.step(1/60f, 8, 2);
    updateCamera();

    physRender.render(physWorld, cam.combined);
//        lights.updateAndRender();
}

private void updatePlayer() {
    if (controls.up) playerBody.setLinearVelocity(playerBody.getLinearVelocity().x, -controls.speed);
    else playerBody.setLinearVelocity(playerBody.getLinearVelocity().x, controls.down ? controls.speed : 0);

    if (controls.down) playerBody.setLinearVelocity(playerBody.getLinearVelocity().x, controls.speed);
    else playerBody.setLinearVelocity(playerBody.getLinearVelocity().x, controls.up ? -controls.speed : 0);

    if (controls.left) playerBody.setLinearVelocity(-controls.speed, playerBody.getLinearVelocity().y);
    else playerBody.setLinearVelocity(controls.right ? controls.speed : 0, playerBody.getLinearVelocity().y);

    if (controls.right) playerBody.setLinearVelocity(controls.speed, playerBody.getLinearVelocity().y);
    else playerBody.setLinearVelocity(controls.left ? -controls.speed : 0, playerBody.getLinearVelocity().y);
}

public void resize(float width, float height) {
    STD_ZOOM = ((float) CELL_SIZE - 1.875f) / Gdx.graphics.getHeight();

    cam.setToOrtho(true, width, height);
}

private void updateCamera() {
    cam.position.set(playerBody.getPosition(), 0);
    cam.update();
}

public void dispose() {
    super.dispose();
    physWorld.dispose();
    physRender.dispose();
}

GameMap.java 。 如果您需要更多详细信息,请不要害怕询问。 我不能独自解决问题。

0 个答案:

没有答案