为什么我的圈子在libgdx android中就像一个矩形?

时间:2015-09-05 10:14:09

标签: java android libgdx physics geometry

就像我的问题所说,有人可以告诉我,为什么我的圆形纹理/精灵就像一个矩形

代码:

`

@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的新手,我每天都在学习。

1 个答案:

答案 0 :(得分:2)

我有点困惑......你正在创建 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

然后在使用其位置和角度进行绘制时更新它 - 这就是全部