首页 > 解决方案 > 使用带有 box2d 形状的常规 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 绘制的所有形状都在屏幕之外。

标签: javaandroidlibgdx

解决方案


推荐阅读