为什么我的圆在 libgdx android 中表现得像一个矩形?

Why does my circle act like a rectangle in libgdx android?

正如我的问题所说,有人可以告诉我,为什么我的圆 texture/sprite 表现得像一个矩形

代码:

`

@Override
public void create() {

    batch = new SpriteBatch();
    img = new Texture("ball.png");
    sprite = new Sprite(img);

    sprite.setPosition(-sprite.getWidth()/2,-sprite.getHeight()/2);

    world = new World(new Vector2(0, -1f),true);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set((sprite.getX() + sprite.getWidth()/2) /
                    PIXELS_TO_METERS,
            (sprite.getY() + sprite.getHeight()/2) / PIXELS_TO_METERS);

    body = world.createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(sprite.getWidth()/2 / PIXELS_TO_METERS, sprite.getHeight()
            /2 / PIXELS_TO_METERS);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 0.1f;
    fixtureDef.restitution = 0.8f;

    body.createFixture(fixtureDef);
    shape.dispose();

    //CONTINUE

    // BOTTOM

    BodyDef bodyDefBottom = new BodyDef();
    bodyDefBottom.type = BodyDef.BodyType.StaticBody;
    float w = Gdx.graphics.getWidth()/PIXELS_TO_METERS;
    // Set the height to just 50 pixels above the bottom of the screen so we can see the edge in the
    // debug renderer
    float h = Gdx.graphics.getHeight()/PIXELS_TO_METERS- 50/PIXELS_TO_METERS;
    bodyDefBottom.position.set(0,0);
    FixtureDef fixtureDef2 = new FixtureDef();

    EdgeShape edgeShape = new EdgeShape();
    edgeShape.set(-w/2,-h/2,w/2,-h/2);
    fixtureDef2.shape = edgeShape;

    bodyEdgeScreenBottom = world.createBody(bodyDefBottom);
    bodyEdgeScreenBottom.createFixture(fixtureDef2);
    edgeShape.dispose();

    // TOP

    BodyDef bodyDefTop = new BodyDef();
    bodyDefTop.type = BodyDef.BodyType.StaticBody;
    w = Gdx.graphics.getWidth()/PIXELS_TO_METERS;
    // Set the height to just 50 pixels above the bottom of the screen so we can see the edge in the
    // debug renderer
    h = Gdx.graphics.getHeight()/PIXELS_TO_METERS;
    bodyDefTop.position.set(0,0);
    fixtureDef2 = new FixtureDef();

    edgeShape = new EdgeShape();
    edgeShape.set(-w/2,h/2,w/2,h/2);
    fixtureDef2.shape = edgeShape;

    bodyEdgeScreenTop = world.createBody(bodyDefTop);
    bodyEdgeScreenTop.createFixture(fixtureDef2);
    edgeShape.dispose();

    // LEFT

    BodyDef bodyDefLeft = new BodyDef();
    bodyDefLeft.type = BodyDef.BodyType.StaticBody;
    w = Gdx.graphics.getWidth()/PIXELS_TO_METERS;
    // Set the height to just 50 pixels above the bottom of the screen so we can see the edge in the
    // debug renderer
    h = Gdx.graphics.getHeight()/PIXELS_TO_METERS;
    bodyDefLeft.position.set(0,0);
    fixtureDef2 = new FixtureDef();

    edgeShape = new EdgeShape();
    edgeShape.set(w/2,-h/2,w/2,h/2);
    fixtureDef2.shape = edgeShape;

    bodyEdgeScreenLeft = world.createBody(bodyDefLeft);
    bodyEdgeScreenLeft.createFixture(fixtureDef2);
    edgeShape.dispose();

    // RIGHT

    BodyDef bodyDefRight = new BodyDef();
    bodyDefRight.type = BodyDef.BodyType.StaticBody;
    w = Gdx.graphics.getWidth()/PIXELS_TO_METERS;
    // Set the height to just 50 pixels above the bottom of the screen so we can see the edge in the
    // debug renderer
    h = Gdx.graphics.getHeight()/PIXELS_TO_METERS;
    bodyDefRight.position.set(0,0);
    fixtureDef2 = new FixtureDef();

    edgeShape = new EdgeShape();
    edgeShape.set(-w/2,-h/2,-w/2,h/2);
    fixtureDef2.shape = edgeShape;

    bodyEdgeScreenRight = world.createBody(bodyDefRight);
    bodyEdgeScreenRight.createFixture(fixtureDef2);
    edgeShape.dispose();

    //CONTINUE

    Gdx.input.setInputProcessor(this);

    //debugRenderer = new Box2DDebugRenderer();
    font = new BitmapFont();
    font.setColor(Color.BLACK);
    camera = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.
            getHeight());
}

private float elapsed = 0;
@Override
public void render() {
    camera.update();
    // Step the physics simulation forward at a rate of 60hz
    world.step(1f / 60f, 6, 2);

    body.applyTorque(torque, true);

    sprite.setPosition((body.getPosition().x * PIXELS_TO_METERS) - sprite.
                    getWidth() / 2,
            (body.getPosition().y * PIXELS_TO_METERS) - sprite.getHeight() / 2)
    ;
    sprite.setRotation((float) Math.toDegrees(body.getAngle()));

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

    batch.setProjectionMatrix(camera.combined);
    //debugMatrix = batch.getProjectionMatrix().cpy().scale(PIXELS_TO_METERS,
//          PIXELS_TO_METERS, 0);
    batch.begin();

    if(drawSprite)
        batch.draw(sprite, sprite.getX(), sprite.getY(),sprite.getOriginX(),
                sprite.getOriginY(),
                sprite.getWidth(),sprite.getHeight(),sprite.getScaleX(),sprite.
                        getScaleY(),sprite.getRotation());

    font.draw(batch,
            "Restitution: " + body.getFixtureList().first().getRestitution(),
            -Gdx.graphics.getWidth()/2,
            Gdx.graphics.getHeight()/2 );
    batch.end();

    //debugRenderer.render(world, debugMatrix);
}

@Override
public void dispose() {
    img.dispose();
    world.dispose();
}

@Override
public boolean keyDown(int keycode) {
    return false;
}

@Override
public boolean keyUp(int keycode) {
    return false;
}

@Override
public boolean keyTyped(char character) {
    return false;
}


// On touch we apply force from the direction of the users touch.
// This could result in the object "spinning"
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    body.applyForce(1f,1f,screenX,screenY,true);
    //body.applyTorque(0.4f,true);
    return true;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
    return false;
}

@Override
public boolean scrolled(int amount) {
    return false;
}

`

我曾尝试搜索其他帖子,但找不到任何可以向我解释为什么它像矩形一样运行以及如何解决此问题的内容。我也尝试过寻找其他方法来绘制纹理,甚至使用 CircleShape 但不能从图像中放入纹理。我是 libgdx 的新手,我每天都在学习。

我有点困惑...您正在创建 PolygonShape 并期望它表现得像圆形?

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(...

您可以通过这种方式实现的是 多边形,其中包含小球精灵。如果您想使用您提到的圆 CircleShape

    CircleShape shape = new CircleShape();
    shape.setRadius(1); //or anything you need

    ...

    //assing it to the fixture etc

然后在绘制时用它的位置和角度更新它 - 就是这样