LibGDX随相机位置浮动,导致平铺贴图出现黑线

时间:2018-11-21 22:56:57

标签: java libgdx

我正在创建一个自上而下的射击游戏,每当我移动相机或进行缩放时,黑色的画面就会像image的网格一样出现

我正在使用Tiled创建地图,并且我的相机跟随居中的box2d主体。我发现使用int强制转换使相机位置等于bo​​x2d主体的位置会导致黑线消失,像这样:image

但是问题是,因为我将游戏缩小了比例,所以玩家将移动一两秒,然后当玩家在任一轴上到达下一个整数时,相机会捕捉到该玩家,而不是我想要的游戏震撼力。播放器的运动是细粒度的,但是四舍五入时,摄像机的运动不是。我不知道这是我的磁贴纸是否有问题,或者是否可以通过更改一些代码来解决。我尝试了各种不同类型的填充,间距和边距的组合。因此,最终,我如何才能使摄像机平滑地与播放器的位置匹配,并且不会引起黑线?我将不胜感激任何帮助或建议。预先谢谢你!

我在哪里键入将玩家的浮动位置转换为游戏类中的整数:

    public void cameraUpdate(float delta) {

    //timeStep = 60 times a second, velocity iterations = 6, position iterations = 2
    world.step(1/60f, 6, 2); //tells game how many times per second for Box2d to make its calculations
    cam.position.x = (int)playerOne.b2body.getPosition().x;
    cam.position.y = (int)playerOne.b2body.getPosition().y;

    cam.update();
}

大多数玩家类别:

public class PlayerOne extends Sprite implements Disposable{
public World world; // world player will live in
public Body b2body; //creates body for player
private BodyDef bdef = new BodyDef();
private float speed = 1f;
private boolean running;
TextureAtlas textureAtlas;
Sprite sprite;
TextureRegion textureRegion;
private Sound runningSound;



public PlayerOne(World world) {
    this.world = world;
    definePlayer();
    textureAtlas = new TextureAtlas(Gdx.files.internal("sprites/TDPlayer.atlas"));
    textureRegion = textureAtlas.findRegion("TDPlayer");
    sprite =new Sprite(new Texture("sprites/TDPlayer.png"));
    sprite.setOrigin((sprite.getWidth() / 2) / DunGun.PPM, (float) ((sprite.getHeight() / 2) / DunGun.PPM - .08));
    runningSound = Gdx.audio.newSound(Gdx.files.internal("sound effects/running.mp3"));

}


public void definePlayer() {
    //define player body
    bdef.position.set(750 / DunGun.PPM, 400 / DunGun.PPM);

    bdef.type = BodyDef.BodyType.DynamicBody;
    //create body in the world
    b2body = world.createBody(bdef);

    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(12 / DunGun.PPM);

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

public void renderSprite(SpriteBatch batch) {
    float posX = b2body.getPosition().x;
    float posY = b2body.getPosition().y;
    float posX2 = (float) (posX - .14);
    float posY2 = (float) (posY - .1);
    sprite.setSize(32 / DunGun.PPM, 32 / DunGun.PPM);
    sprite.setPosition(posX2, posY2);
    float mouseX = Level1.mouse_position.x; //grabs cam.unproject x vector value
    float mouseY = Level1.mouse_position.y; //grabs cam.unproject y vector value

    float angle = MathUtils.atan2(mouseY - getY(), mouseX - getX()) * MathUtils.radDeg; //find the distance between mouse and player

    angle = angle - 90; //makes it a full 360 degrees
    if (angle < 0) {
        angle += 360 ;
    }
    float angle2 = MathUtils.atan2(mouseY - getY(), mouseX - getX()); //get distance between mouse and player in radians
    b2body.setTransform(b2body.getPosition().x, b2body.getPosition().y, angle2); //sets the position of the body to the position of the body and implements rotation
    sprite.setRotation(angle); //rotates sprite
    sprite.draw(batch); //draws sprite
    }

public void handleInput(float delta) {
    setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2 + (5 / DunGun.PPM));

    this.b2body.setLinearVelocity(0, 0);

    if(Gdx.input.isKeyPressed(Input.Keys.W)){
        this.b2body.setLinearVelocity(0f, speed);
    }if(Gdx.input.isKeyPressed(Input.Keys.S)){
        this.b2body.setLinearVelocity(0f, -speed);
    }if(Gdx.input.isKeyPressed(Input.Keys.A)){
        this.b2body.setLinearVelocity(-speed, 0f);

    }if(Gdx.input.isKeyPressed(Input.Keys.D)){
        this.b2body.setLinearVelocity(speed, 0f);

    }if(Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.A)){
        this.b2body.setLinearVelocity(-speed, speed);

    }if(Gdx.input.isKeyPressed(Input.Keys.W) && Gdx.input.isKeyPressed(Input.Keys.D)){
        this.b2body.setLinearVelocity(speed, speed);
    }
    if(Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.A)){
        this.b2body.setLinearVelocity(-speed, -speed );

    }if(Gdx.input.isKeyPressed(Input.Keys.S) && Gdx.input.isKeyPressed(Input.Keys.D)){
        this.b2body.setLinearVelocity(speed, -speed);
    } 

我在这里声明每米比例的像素:

public class DunGun extends Game{
public SpriteBatch batch;
//Virtual Screen size and Box2D Scale(Pixels Per Meter)
public static final int V_WIDTH = 1500;
public static final int V_HEIGHT = 800;
public static final float PPM = 100; //Pixels Per Meter

游戏渲染和调整大小方法:

    @Override
public void render(float delta) {
    cameraUpdate(delta);
    playerOne.handleInput(delta);
    //clears screen
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
        cam.zoom -= .01;

    }
    if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)) {
        cam.zoom += .01;

    }


    mapRenderer.render();
    b2dr.render(world, cam.combined); //renders the Box2d world

    mapRenderer.setView(cam);
    //render our game map
    //mapRenderer.render(); // renders map
    //mapRenderer.render(layerBackround); //renders layer in Tiled that p1 covers
    game.batch.setProjectionMatrix(cam.combined); //keeps player sprite from doing weird out of sync movement

    mouse_position.set(Gdx.input.getX(), Gdx.input.getY(), 0);
    cam.unproject(mouse_position); //gets mouse coordinates within viewport
    game.batch.begin(); //starts sprite spriteBatch

    playerOne.renderSprite(game.batch);

    game.batch.end(); //starts sprite spriteBatch
    //mapRenderer.render(layerAfterBackground); //renders layer of Tiled that hides p1
}


@Override
public void resize(int width, int height) {
    viewport.update(width, height, true); //updates the viewport camera
}

1 个答案:

答案 0 :(得分:1)

我通过在GDX Texture Packer中修饰瓦片集的填充来解决它。我在32x32磁贴周围添加了5个像素填充。我在“平铺”中将边距设置为2,间距设置为4。我尝试了很多无效的填充/间距/边距组合,这使我认为这是一个编码问题,但是这些设置有效,而且我不必舍入浮点数。