coder_For_Life22 coder_For_Life22 - 1 year ago 224
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?


Here is what i have so far.

QueryCallback callback = new QueryCallback() {

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;

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 Source 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.


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

        return false;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download