java - 当相机平移时, Sprite 不会跟随 box2d 主体,但如果相机在 libgdx 中是静态的,则效果良好

标签 java camera libgdx sprite

我的问题如下,当我不移动相机时,我的 Sprite 很好地跟随我的 box2d body 。但是,当我实现滚动时,我使用 cam.translate Sprite 将每一帧调高,直到它消失。 奇怪的是,如果我检查 b2body 和 sprite 之间的坐标是否完全相同。

更新: 我尝试使用 cam.position.set(cam.position.x, cam.position.y * 1.001f, 0) 来替换 cam.translate 但我遇到了同样的问题。

变量:

private GalaxyFighter jeu;

//render variables
private OrthographicCamera cam;
private Viewport gamePort;
private Hud hud;
private float maxScrolling;

//tilded map variables
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;

//box2d variables
private World world;
private Box2DDebugRenderer b2dr;
private SpaceShip player;
private Sprite spriteSpaceShip;

构造函数:

public EcranJeu(GalaxyFighter jeu, float maxScrolling){
    this.jeu = jeu;

    cam = new OrthographicCamera();

    gamePort = new FitViewport(GalaxyFighter.V_WIDTH / GalaxyFighter.PPM, GalaxyFighter.V_HEIGHT / GalaxyFighter.PPM, cam);

    hud = new Hud(jeu.batch);

    this.maxScrolling = maxScrolling;

    maploader = new TmxMapLoader();
    map = maploader.load("level1.tmx");
    renderer = new OrthogonalTiledMapRenderer(map, 1 / GalaxyFighter.PPM);

    cam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);

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

    b2dr = new Box2DDebugRenderer();

    player = new SpaceShip(world);

    spriteSpaceShip = new Sprite(new Texture("redship1.png"));

    player.b2body.setUserData(spriteSpaceShip);

    new B2WorldCreator(world, map);

    world.setContactListener(new WorldContactListener());
}

这是我在渲染中调用的更新方法:

 public void update(float dt){
    handleInput(dt);

    world.step(1/60f, 6, 2);

   if(cam.position.y < (maxScrolling / GalaxyFighter.PPM))
        cam.translate(0, 0.5f / GalaxyFighter.PPM);

    limitPlayer();

    spriteSpaceShip.setPosition(player.b2body.getPosition().x * GalaxyFighter.PPM - 8, player.b2body.getPosition().y * GalaxyFighter.PPM - 8);
    System.out.println("Pos player x = " + (player.b2body.getPosition().x * GalaxyFighter.PPM - 8) + " y = " + (player.b2body.getPosition().y * GalaxyFighter.PPM - 8));
    System.out.println("Pos sprite x = " + spriteSpaceShip.getX() + " y = " + spriteSpaceShip.getY());

    cam.update();
    renderer.setView(cam);
}

这是我的渲染方法:

 public void render(float delta) {
    update(delta);

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

    renderer.render();

    b2dr.render(world, cam.combined);

    jeu.batch.setProjectionMatrix(hud.stage.getCamera().combined);

    hud.stage.draw();

    jeu.batch.begin();
    spriteSpaceShip.draw(jeu.batch);
    jeu.batch.end();
}

我不知道我的错误在哪里,所以我希望有人能找到它! 感谢您抽出时间。

编辑:这是我正在使用的另一个类,我完成了 gameScreen 类

平视显示器

public class Hud implements Disposable {
public Stage stage;
private Viewport viewport;

private Integer life;
private static Integer score;
private Integer niveau;

Label lifeLabel;
private static Label scoreLabel;
Label niveauLabel;
Label lifeStrLabel;
Label scoreStrLabel;
Label niveauStrLabel;

public Hud(SpriteBatch sb){
    life = 3;
    score = 0;
    niveau = 1;

    viewport = new FitViewport(GalaxyFighter.V_WIDTH, GalaxyFighter.V_HEIGHT, new OrthographicCamera());
    stage = new Stage(viewport, sb);

    Table table = new Table();
    table.top();
    table.setFillParent(true);

    lifeLabel = new Label(String.format("%02d", life), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    scoreLabel = new Label(String.format("%04d", score), new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    niveauStrLabel = new Label("NIVEAU", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    lifeStrLabel = new Label("VIE", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    scoreStrLabel = new Label("SCORE", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
    niveauLabel = new Label(String.format("%02d", niveau), new Label.LabelStyle(new BitmapFont(), Color.WHITE));

    table.add(lifeStrLabel).expandX().padTop(10);
    table.add(niveauStrLabel).expandX().padTop(10);
    table.add(scoreStrLabel).expandX().padTop(10);
    table.row();
    table.add(lifeLabel).expandX();
    table.add(niveauLabel).expandX();
    table.add(scoreLabel).expandX();

    stage.addActor(table);
}

以及玩家的 spaceship :

public class SpaceShip extends Sprite {
public World world;
public Body b2body;

public SpaceShip(World world){
    this.world = world;
    defineSpaceShip();
}

public void defineSpaceShip(){
    BodyDef bdef = new BodyDef();
    bdef.position.set(GalaxyFighter.V_WIDTH / 2 / GalaxyFighter.PPM, 3 / GalaxyFighter.PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);

    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(5 / GalaxyFighter.PPM);

    fdef.filter.categoryBits = GalaxyFighter.SPACESHIP_BIT;
    fdef.filter.maskBits = GalaxyFighter.DEFAULT_BIT | GalaxyFighter.ASTEROIDE_BIT |GalaxyFighter.BONUS1_BIT|GalaxyFighter.BONUS2_BIT|GalaxyFighter.BONUS3_BIT|GalaxyFighter.PISTE_BIT|GalaxyFighter.BORDSMAP_BIT;

    fdef.shape = shape;
    b2body.createFixture(fdef);

    EdgeShape head_spaceship = new EdgeShape();
    head_spaceship.set(new Vector2(-4 / GalaxyFighter.PPM, 0), new Vector2(4 / GalaxyFighter.PPM, 0));
    fdef.shape = head_spaceship;
    fdef.isSensor = true;
    b2body.createFixture(fdef).setUserData("head_spaceship");

    EdgeShape head_spaceship2 = new EdgeShape();
    head_spaceship2.set(new Vector2(0, -4 / GalaxyFighter.PPM), new Vector2(0, 4 / GalaxyFighter.PPM));
    fdef.shape = head_spaceship2;
    fdef.isSensor = true;
    b2body.createFixture(fdef).setUserData("head_spaceship2");
}

最佳答案

您是否尝试过将摄像机的位置设置为玩家的位置,而不仅仅是线性平移?像这样:

cam.position.set(0, spriteSpaceShip.getY(), 0);

关于java - 当相机平移时, Sprite 不会跟随 box2d 主体,但如果相机在 libgdx 中是静态的,则效果良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55208296/

相关文章:

android - 如何在 Android 中为硬件按钮设置事件处理程序?

java - 碰撞检测中的 NullPointerException

java - Camunda测试部署注释未部署

java - 无效整数 : "res/color/abc_primary_text_material_light.xml"

java - 如何在 Jtable 中显示数据库的选定列?

android - 在 fragment 中使用相机预览时性能下降

flutter - 使用列表的值构成另一个类

android - 使用 Libgdx 在 Android 应用程序中没有声音

java - libgdx 游戏中的 Google Play 商店错误

java - 计算年数的函数