简体   繁体   English

LibGDX Touch Box2D机身

[英]LibGDX Touch Box2D Body

I am using LibGDX to create a new project. 我正在使用LibGDX创建一个新项目。

What i am trying to do is, i load bodies from a tmx file into levels which works fine. 我想做的是,我将tmx文件中的主体加载到可以正常工作的级别中。 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. 我不太熟悉在libgdx中执行这样的操作。 Although i'm sure it is not that complicated. 尽管我确定它并不那么复杂。

Is there anyway i can do this in LibGDX? 无论如何,我可以在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.. 现在我只是不确定如何删除被单击的主体。

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. https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/Box2DTest.java使用此链接使用queryAABB选择带有接触点的对象。 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;
}
.....

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). 要删除身体并确保不会在步骤cyrcle中发生这种情况,请使用“列表”并向其中添加要删除的所有对象,然后在步骤cyrcle激活列表中的所有对象并调用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. 我没有真正尝试过该代码,但它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM