coder_For_Life22 coder_For_Life22 - 7 days ago 7
Android Question

LibGDX Touch Box2D Body

I am using LibGDX to create a new project.

What i am trying to do is, i load bodies from a tmx file into levels which works fine. The bodies also has a sprite with them.

The problem is, is i would like to allow the user to touch certain bodies on the scene. When they touch the body they will be able to delete or remove it from the scene.

I am not that familiar with doing something like this in libgdx. Although i'm sure it is not that complicated.

Is there anyway i can do this in LibGDX?

EDIT:

Here is what i have so far.

QueryCallback callback = new QueryCallback() {


@Override
public boolean reportFixture(Fixture fixture) {
// if the hit fixture's body is the ground body
// we ignore it

// if the hit point is inside the fixture of the body
// we report it

hitBody = fixture.getBody();

return true;
}
};

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
hitBody = null;


return false;
}


Now i am just not sure how i remove the body that was clicked..

Answer

https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/Box2DTest.java use this link to use queryAABB to select the objects with touchpoint. this code also provide how can you move objects with mouse joint. If you want to delete the objects make sure you delete them after step cycle of world.

Edit:

....
public boolean touchDown (int x, int y, int pointer, int newParam) {
        testPoint.set(x, y, 0);
        camera.unproject(testPoint);
        hitBody = null;
        world.QueryAABB(callback, testPoint.x - 0.1f, testPoint.y - 0.1f, testPoint.x + 0.1f, testPoint.y + 0.1f);

        return false;
}
.....
Comments