2
votes

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

2
you are assigning hitbody to be null. but here you for removing the body you should also write the code to remove body what you can edit here is in touchup use world.destroyBody(hitbody). then initialize hitbody = null. Aslo if this gives you JNI error then it means that body is removed in step cycle of world. You should prevent that.Vikalp Jain
Also here the hit body is the body that the user have clicked. So you can do anything with that body.Vikalp Jain
So if if hitbody is the body how to i call the QueryCallback callback method??coder_For_Life22
Could you edit my code above showing my how it should be done?coder_For_Life22
see my edit. You would find world.queryAABB(..) method which would give you a call back in reportFixture() telling which fixture is touched. The link that i provided earlier will also help you to move the body with mouseJoint if required.Vikalp Jain

2 Answers

5
votes

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;
}
.....
1
votes

To remove the body and make sure this will not happen in the step cyrcle use a List and add to it all the bodies you want to remove and after your step cyrcle itterate all of the bodies in the list and call world.destroyBody(body).

So the code should be like that:

Array<Body> bodies=new Array<Body>();
Vector3 temp=new Vector3();;
List<Body> bodiesToRemove=new ArrayList<Body>();
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    world.getBox2dWorld().getBodies(bodies);
    temp.set(screenX, screenY, 0);
    camera.unproject(temp);
    for(Body body:bodies){
        if(temp.dst(Vector.vector.set(body.getPosition().x, body.getPosition().y)</*width or height of the body*/){
            bodiesToRemove.add(body);
        }
    }
    return false;
}

public void update(){
    //The world.step(..) code here

    for(Body body:bodiesToRemove){
        world.destroyBody(body);
    }
}

I didn't really tried the code but it should work.