libGDX + box2d弹跳球触摸

时间:2015-02-28 18:33:56

标签: android libgdx box2d

我试图用libGDX和box2d触碰球, 它不会像太阳一样转身反弹, 任何人都可以帮助我吗?

@Override
public void create() {
    batch = new SpriteBatch();
    BallTexture = new Texture("data/ball.png");
    BallSprite = new Sprite(BallTexture);

    BallSprite.setPosition(-BallSprite.getWidth() / 2,
            -BallSprite.getHeight() / 2);
    world = new World(new Vector2(0, -10f), true);

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

    BallBody = world.createBody(BallDef);

    CircleShape BallShape = new CircleShape();
    BallShape.setRadius(3f);

    FixtureDef BallFixDeff = new FixtureDef();
    BallFixDeff.shape = BallShape;
    BallFixDeff.density = 2.5f;
    BallFixDeff.friction = 3f;
    BallFixDeff.restitution = .75f;

    BallBody.createFixture(BallFixDeff);
    BallShape.dispose();

    //floor
    BodyDef floorDef = new BodyDef();
    floorDef.type = BodyDef.BodyType.StaticBody;
    float w = Gdx.graphics.getWidth() / PIXELS_TO_METERS;
    float h = Gdx.graphics.getHeight() / PIXELS_TO_METERS - 50
            / PIXELS_TO_METERS;
    floorDef.position.set(0, 0);

    FixtureDef FloorFixDeff = new FixtureDef();
    EdgeShape FloorEdgeShape = new EdgeShape();
    FloorEdgeShape.set(-w / 2, -h / 2, w / 2, -h / 2);
    FloorFixDeff.shape = FloorEdgeShape;

    floorBody = world.createBody(floorDef);
    floorBody.createFixture(FloorFixDeff);
    FloorEdgeShape.dispose();

    Gdx.input.setInputProcessor(this);

    debugRenderer = new Box2DDebugRenderer();

    camera = new OrthographicCamera(Gdx.graphics.getWidth(),
            Gdx.graphics.getHeight());

}

@Override
public void render() {
    camera.update();

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


    BallBody.applyTorque(torque,true);

    BallSprite.setPosition(
            (BallBody.getPosition().x * PIXELS_TO_METERS) - BallSprite.getWidth()
                    / 2,
            (BallBody.getPosition().y * PIXELS_TO_METERS) - BallSprite.getHeight()
                    / 2);

    BallSprite.setRotation((float)Math.toDegrees(BallBody.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(drawBall)
        batch.draw(BallSprite, BallSprite.getX(), BallSprite.getY(), BallSprite.getOriginX(), BallSprite.getOriginY(), BallSprite.getWidth(), BallSprite.getHeight(), BallSprite.getScaleX(), BallSprite.getScaleY(), BallSprite.getRotation());

    batch.end();
    debugRenderer.render(world, debugMatrix);
}

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

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    // TODO Auto-generated method stub
    //BallBody.applyForce(0f,10f,screenX,screenY,true);
    BallBody.applyLinearImpulse(0f, 0.8f, screenX, screenY, true);
    return false;
}

}

2 个答案:

答案 0 :(得分:0)

你的问题是单位。 Box2D使用米

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    // TODO Auto-generated method stub
    //BallBody.applyForce(0f,10f,screenX,screenY,true);
    BallBody.applyLinearImpulse(0f, 0.8f, screenX, screenY, true);
    return false;
}

此代码导致问题。 请改用

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    Vector3 temp = new Vector3();
    camera.unproject(temp.set(screenX, screenY, 0));
    BallBody.applyLinearImpulse(0f, 0.8f, temp.x, temp.y, true);
    return false;
}

您应该先取消您的坐标。并且不要使用PIXELS_TO_METERS丢弃代码,学习如何正确使用相机。您的相机属性应以米为单位,而不是以像素为单位。

答案 1 :(得分:0)

抱歉,但没有上述帮助, 我添加了这个:

BallBody.applyLinearImpulse(new Vector2(-5, 450), BallBody.getLocalCenter(), true); 

并且它有效,但现在我必须检查与身体或精灵的ontouch碰撞,但我在网上发现的任何东西似乎都有效,

这是我的代码:

    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    // TODO Auto-generated method stub
    float centerX,centerY,radius,distance;
    centerX = BallSprite.getX() + BallSprite.getWidth()/2;
    centerY = BallSprite.getY() + BallSprite.getHeight()/2;

    radius = BallSprite.getHeight()/2;

    distance = (float)Math.sqrt(Math.pow((screenX-centerX),2)+Math.pow((screenY-centerY),2));

    if (distance <= radius)
        BallBody.applyLinearImpulse(new Vector2(-5, 450), BallBody.getLocalCenter(), true); 

我做错了什么?