Java LibGDX无法在物理主体和重力启用的情况下移动

时间:2016-08-24 03:12:55

标签: java libgdx box2d

我真的可以使用一些帮助我坚持这个。我试图在一个简单的游戏中制作一个块作为地面而另一个块落在它上面然后可能反弹一点但是能够向左或向右移动框以从第一个块上掉下来。有人可以帮我这个吗?

对于对PoprostuRonin和dermetfan的YouTube视频感兴趣的人,我能够得到我想要的结果,你可以在项目中尝试以下代码,只需更改精灵纹理。

private Box2DDebugRenderer debugRenderer;
private OrthographicCamera camera;
private float spriteSpeed = 500000;
private World world;
private Sprite playersprite;
private Sprite groundsprite;
private Body playerBody;
private Body groundbody;
private Vector2 movement = new Vector2();
private Array<Body> tmpBodies = new Array<Body>();

public TestState(GameStateManager gsm) {
    super(gsm);

    Gdx.input.setInputProcessor(this);

    batch = new SpriteBatch();

    debugRenderer = new Box2DDebugRenderer();
    camera = new OrthographicCamera();

    // Sprites
    playersprite = new Sprite(new Texture("badlogic.jpg"));
    groundsprite = new Sprite(new Texture("ground-tiles-01.gif"));

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

    // Player Sprite
    // Body definition
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(0, 200); //1m

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

    // Fixture definition
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 2.5f; //2.5kg
    fixtureDef.friction = 0; //0-1
    fixtureDef.restitution = .75f; //0-1

    playerBody = world.createBody(bodyDef); 
    playerBody.createFixture(fixtureDef);
    playerBody.setUserData(playersprite);
    playersprite.setSize(20, 20);
    playersprite.setOrigin(playersprite.getWidth() / 2, playersprite.getHeight() / 2);
    shape.dispose();

    // Ground Sprite
    // Body definition
    BodyDef groundbodyDef = new BodyDef();
    groundbodyDef.type = BodyType.StaticBody;
    groundbodyDef.position.set(0, 0); //1m

    PolygonShape groundshape = new PolygonShape();
    groundshape.setAsBox(groundsprite.getHeight() / 2, groundsprite.getWidth() / 2);

    // Fixture definition
    FixtureDef groundfixtureDef = new FixtureDef();
    groundfixtureDef.shape = groundshape;
    groundfixtureDef.density = 100; //2.5kg
    groundfixtureDef.friction = .25f; //0-1
    groundfixtureDef.restitution = 0; //0-1

    groundbody = world.createBody(groundbodyDef); 
    groundbody.createFixture(groundfixtureDef);
    groundbody.setUserData(groundsprite);
    groundsprite.setSize(groundsprite.getHeight(), groundsprite.getWidth());
    groundsprite.setOrigin(groundsprite.getWidth() / 2, groundsprite.getHeight() / 2);

    groundshape.dispose();
}

@Override
public void update(float delta) {
    camera.viewportWidth = Gdx.graphics.getWidth();
    camera.viewportHeight = Gdx.graphics.getHeight();
}

@Override
public void render() {
    Gdx.gl.glClearColor(50 / 255f, 213 / 255f, 237 / 255f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    debugRenderer.render(world, camera.combined);
    batch.setProjectionMatrix(camera.combined);
    world.step(Gdx.graphics.getDeltaTime(), 6, 2);

    playerBody.applyForceToCenter(movement, true);

    batch.begin();
    world.getBodies(tmpBodies);
    for (Body body : tmpBodies){
        if (body.getUserData() != null && body.getUserData() instanceof Sprite) {
            Sprite sprite = (Sprite) body.getUserData();
            sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2);
            sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
            sprite.draw(batch);
        }
    }
    batch.end();

    camera.position.set(playerBody.getPosition().x, playerBody.getPosition().y, 0);
    camera.update();

}

@Override
public void dispose() {
    world.dispose();
    playersprite.getTexture().dispose();
    groundsprite.getTexture().dispose();
}

@Override
public boolean keyDown(int keycode) {
    switch (keycode) {
    case Keys.DPAD_LEFT:
        movement.x = -spriteSpeed;
        break;
    case Keys.DPAD_RIGHT:
        movement.x = spriteSpeed;
        break;
    case Keys.DPAD_UP:
        movement.y = spriteSpeed;
        break;
    case Keys.DPAD_DOWN:
        movement.y = -spriteSpeed;
    }
    return true;
}

@Override
public boolean keyUp(int keycode) {
    switch (keycode) {
    case Keys.DPAD_LEFT:
    case Keys.DPAD_RIGHT:
        movement.x = 0;
        break;
    case Keys.DPAD_UP:
    case Keys.DPAD_DOWN:
        movement.y = 0;
    }
    return true;
}

1 个答案:

答案 0 :(得分:1)

您的代码有效,问题是数字。

你身体的质量很大,20480公斤。重力因为你的要求而被颠倒了:

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

-1000,它低于0所以它被反转并且大数字(你的&#34;行星&#34;和地球上9.8)上的1000会导致对象不太可控。

更改值:

float spriteSpeed = 60000;
...
world = new World(new Vector2(0, 9.8F), true);
...
fixtureDef.density = 1;
fixtureDef2.density = 1;

你会注意到现在身体是可控的(它甚至可以飞)。玩这些数字并学习一些基本的物理规则,以了解你在做什么。

精灵和身体位置已同步,但未正确对齐。使用此代码进行第一步,使用body进行对齐纹理。

sprite.setCenter(body.getPosition().x, body.getPosition().y);
groundsprite.setCenter(ground.getPosition().x, ground.getPosition().y);

但轮换呢

groundsprite.setOriginCenter();
sprite.setOriginCenter();

groundsprite.setRotation(ground.getAngle() * MathUtils.radDeg);
sprite.setRotation(body.getAngle() * MathUtils.radDeg);

这很简单,因为你的对象是单个纹理,我们可以简单地将它对齐在中心,但随着对象变得更加复杂,需要更改代码。