Libgdx Box2d像素计量缩放

时间:2017-08-15 19:48:41

标签: java android libgdx box2d

我正在尝试用Box2d制作游戏。我的问题是像素和米之间的缩放。我画了一艘大小约为500px * 200px的船。这艘船的实际高度为1.5米。所以我用330计算了我的PPM。我的背景是8000px * 1500px大。这也是世界规模。现在世界和船只都是很好的去除了,但一如既往,我无法正确地扩展整个thign,那也是Box2d elemts正确缩放......

编辑:

现在我忘了像素并用米做任何事情。虽然我不明白为什么大多数人使用这个PPM时完全没必要。那么你能看看我的代码并告诉我,如果我理解它并做对了吗?它完美无缺;)

package com.timelab.space;

import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;


public class PlayScreen implements Screen {
private Space g;
//Textures
private Texture background;
private Texture ship;

//Parameters
private int worldHeight = 10;//Meters
private int worldWidth = 53;//Meters fitting to the texutrebackground

//Camera
private OrthographicCamera camera;

//Box2D
private World world;
private Box2DDebugRenderer b2ddr;
private Body player;


public PlayScreen(Space pgame) {
    g = pgame;

    camera = new OrthographicCamera();
    camera.update();

    System.out.print("Created "+this.getClass().getSimpleName());
}

@Override
public void show() {
    background = g.getAssets().manager.get("Textures/space.png",Texture.class);
    ship = g.getAssets().manager.get("Textures/ship.png",Texture.class);

    //Box2D
    world = new World(new Vector2(0,0), false);
    b2ddr = new Box2DDebugRenderer();
    player = createPlayer();
}

private void update(float delta) {
    //Camera
    camera.position.add(0,0,0);
    //Box2d
    world.step(1 / 60f, 6, 2);

}

@Override
public void render(float delta) {
    this.update(delta);

    //camera
    g.batch.setProjectionMatrix(camera.combined);
    camera.update();

    g.batch.begin();
    g.batch.draw(background,0,0,worldWidth,worldHeight);
    g.batch.draw(ship,player.getPosition().x-3.7f/2,player.getPosition().y-1.5f/2,3.7f,1.5f);
    g.batch.end();

    //box2d
    b2ddr.render(world, camera.combined);
}

@Override
public void resize(int width, int height) {
    camera.viewportHeight = worldHeight;
    camera.viewportWidth = (worldHeight * ((float)width/(float)height));
    camera.position.set(camera.viewportWidth/2,camera.viewportHeight/2,0);
    camera.update();
}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {
    b2ddr.dispose();
}

public Body createPlayer() {
    Body body;
    //Physical body definition
    BodyDef def = new BodyDef();
    def.type = BodyDef.BodyType.DynamicBody;
    def.position.set(0,0);
    def.fixedRotation = true;

    //Add the body to the world
    body = world.createBody(def);

    //Add a shape
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(3.7f/2, 1.5f/2);
    body.createFixture(shape, 1.0f);

    //disposing
    shape.dispose();

    return body;
}
}

0 个答案:

没有答案