将常规的libgdx形状与box2d形状一起使用

时间:2018-12-26 11:32:56

标签: java android libgdx

在我决定将box2d用于我的所有游戏物理之前,我已经使用shaperenderer设置了所有非动画形状。我已经将box2d的世界和形状转换为米,比例为32 ppm,但是我所有的常规形状仍然使用像素绘制。

由于我的box2d圆已与摄影机一起缩放,因此可以正确显示大小。但是,我使用shaperenderer用像素绘制的所有形状都不可见。

  1. 我需要用box2d形状重做所有原始形状吗?
  2. 我可以使用PPM缩放比例缩放所有原始形状吗?

据我了解,既然已经缩放了相机,我应该能够通过PPM缩放原始形状的所有尺寸吗?

public class Controller extends Game {
    public OrthographicCamera cam;
    public ShapeRenderer shape;
    public ShapeRenderer circle;
    public FitViewport viewport;
    public Stage stage;

    public World world;
    public Body ball;
    public Box2DDebugRenderer box2DDebugRenderer;

  @Override
  public void create() {
    cam = new OrthographicCamera();
    viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), cam);
    cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    shape = new ShapeRenderer();
    circle = new ShapeRenderer(); //balls shot from cannon

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

    setScreen(new GameScreen(this));
    //setScreen(new TitleScreen(this));
}

@Override
public void dispose() {
    shape.dispose();
    stage.dispose();
    circle.dispose();
    box2DDebugRenderer.dispose();
    world.dispose();
}

@Override
public void pause() {
}

@Override
public void resume() {
}

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

public Body createBall(){
    Body bBody;
    BodyDef def = new BodyDef();
    def.type = BodyDef.BodyType.DynamicBody;
    def.position.set(500,500);
    def.fixedRotation = true;
    bBody = world.createBody(def);

    return bBody;
}
}

这是实现所有原始形状和box2d形状的实际游戏屏幕:

import static com.mygdx.game.Utils.Constants.PPM;

public class GameScreen extends ScreenAdapter {
Controller game;

//x-axis length for top/bottom bar
private float goalWidth;

//y-axis height for back bar
private float goalHeight;
private float goalPostThickness;

//Screen height and width
private float screenWidth;
private float screenHeight;

//How far down/up posts are from edge of screen
private float goalPostTopOffset;
private float goalPostBottomOffset;

private float cannonOriginX; //Variables used for starting position of balls
private float cannonOriginY;

float ballX;
float ballY;

public GameScreen (Controller game){
    this.game = game;
}

@Override
public void show(){
    game.ball = game.createBall();
    // Create a circle shape and set its radius to 6
    CircleShape circle = new CircleShape();
    circle.setRadius(32/2/PPM);

// Create a fixture definition to apply our shape to
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.density = 0.5f;
    fixtureDef.friction = 0.4f;
    fixtureDef.restitution = 0.6f; // Make it bounce a little bit

// Create our fixture and attach it to the body
    Fixture fixture = game.ball.createFixture(fixtureDef);

    circle.dispose();
}

@Override
public void render(float delta){
    //Logic
    screenWidth = Gdx.graphics.getWidth();
    screenHeight = Gdx.graphics.getHeight();

    goalPostTopOffset = screenHeight/7;
    goalPostBottomOffset = goalPostTopOffset * 3;
    goalHeight = screenHeight - (goalPostTopOffset + goalPostBottomOffset);
    goalWidth = screenWidth / 6;
    goalPostThickness = screenWidth / 75;

    cannonOriginX = goalWidth / 2; //Variables used for starting position of balls
    cannonOriginY = (goalPostThickness*5) / 2;

    ballX = 0 + cannonOriginX;
    ballY = (goalPostBottomOffset - (goalPostBottomOffset / 4)) + cannonOriginY;

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

    //Draw
    game.cam.update();
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    game.box2DDebugRenderer.render(game.world, game.cam.combined.scl(PPM));
    game.shape.setProjectionMatrix(game.cam.combined);

    drawNonAnimated();
    drawAnimated();



}

@Override
public void hide(){

}

//draws stationary objects such as goal posts
public void drawNonAnimated(){
    //Top goal bar
    game.shape.setColor(1, 1, 1, 1);
    game.shape.begin(ShapeRenderer.ShapeType.Filled);
    game.shape.rect(screenWidth - goalWidth, screenHeight - goalPostTopOffset, goalWidth, goalPostThickness);
    game.shape.end();

    //Bottom goal bar
    game.shape.setColor(1, 1, 1, 1);
    game.shape.begin(ShapeRenderer.ShapeType.Filled);
    game.shape.rect(screenWidth - goalWidth, goalPostBottomOffset, goalWidth, goalPostThickness);
    game.shape.end();

    //Back goal bar
    game.shape.setColor(1, 1, 1, 1);
    game.shape.begin(ShapeRenderer.ShapeType.Filled);
    game.shape.rect(screenWidth - goalPostThickness, goalPostBottomOffset, goalPostThickness, goalHeight);
    game.shape.end();

    //Cannon platform
    game.shape.setColor(1, 1, 1, 1);
    game.shape.begin(ShapeRenderer.ShapeType.Filled);
    game.shape.rect(0, goalPostBottomOffset - (goalPostBottomOffset / 4),goalWidth, goalPostThickness*2);
    game.shape.end();

    //Cannon
    game.shape.setColor(1, 1, 1, 1);
    game.shape.begin(ShapeRenderer.ShapeType.Filled);
    game.shape.rect(0, goalPostBottomOffset - (goalPostBottomOffset / 4), cannonOriginX,
            cannonOriginY, goalWidth, goalPostThickness*5, 1, 1, 45);
    game.shape.end();
}

public void drawAnimated(){
    //Ball(s)
        game.circle.setColor(1, 1, 1, 1);
        game.circle.begin(ShapeRenderer.ShapeType.Filled);
        game.circle.circle(ballX, ballY, goalPostThickness * 2.5f);
        game.circle.end();
        game.cam.update();

        ballX += 1;
}



}

正如我之前提到的,用box2d渲染的圆形在PPM比例尺上显示良好。但是,我用shaperenderer绘制的所有形状都不在屏幕上。

0 个答案:

没有答案