简体   繁体   English

使用物理身体编辑器的Libgdx Box2d碰撞检测

[英]Libgdx Box2d collision detection using physics body editor

Hy guys, 大家好

I am developing a game for android using libgdx. 我正在使用libgdx开发适用于android的游戏。 I am completely stuck at the part of detecting collision between two bodies. 我完全陷入了检测两个物体之间碰撞的部分。 I have a player which I create through the function below 我有一个通过以下功能创建的播放器

public Body createPlayer(String file_path, String fixture_name) {
    // 0. Create a loader for the file saved from the editor.
    BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal(file_path));

    // 1. Create a BodyDef, as usual.
    BodyDef bd = new BodyDef();
    bd.type = BodyDef.BodyType.DynamicBody;

    // 2. Create a FixtureDef, as usual.
    FixtureDef fd = new FixtureDef();
    fd.density = 1;
    fd.friction = 0.5f;
    fd.restitution = 0.3f;

    // 3. Create a Body, as usual.
    body= world.createBody(bd);
    //body.setBullet(true);

    // 4. Create the body fixture automatically by using the loader.
    loader.attachFixture(body, fixture_name, fd, 1);
    body.setUserData(this);

    return body;

}

and an enemy that I create with the same function of the player where I change only the file_path and the fixture_name. 和我创建的具有与播放器相同功能的敌人,我只更改file_path和Fixture_name。 The file_path points to a .json file that I created with box2d editor (site: http://www.aurelienribon.com/blog/projects/physics-body-editor/ ). file_path指向我使用box2d编辑器创建的.json文件(网站: http : //www.aurelienribon.com/blog/projects/physics-body-editor/ )。 After the creation of the body I draw the player and the enemy with two similar functions ( I only post one): 创建完身体后,我用两个相似的功能(和我只张贴一个)吸引玩家和敌人:

private void drawPlayer(){
    player_sprite = new Sprite(player_TR);
    player_sprite.setSize(player.getWidth(), player.getHeight());
    player_sprite.setPosition(player.getX(), player.getY());
    player_sprite.setOrigin(0, 0);
    player_sprite.draw(sb);
}

If I start the game everything is drawn where it should be. 如果我开始游戏,一切都会绘制在应有的位置。 Obviously if the player touches the enemy nothing happen. 显然,如果玩家触摸敌人,则什么也不会发生。 So i started trying to search how to make the two bodies collides but I don't really understand how to use ContactListener and beginContact. 因此,我开始尝试搜索如何使两个物体碰撞,但是我不太了解如何使用ContactListener和beginContact。 beginContact wants as input a Contact but what is a Contact? beginContact希望将Contact作为输入,但是什么是Contact? I have found this code online which appears to solve my problem but I don't know how to use it: 我在网上找到了这段代码,似乎可以解决我的问题,但是我不知道如何使用它:

worldbox.setContactListener(new ContactListener() {

        @Override
        public void beginContact(Contact contact) {

            if(contact.getFixtureA().getBody().getUserData()== "body1" &&
                    contact.getFixtureB().getBody().getUserData()== "body2")
                Colliding = true;
            System.out.println("Contact detected");
        }

Can you help me (if it is possible through some code) to solve my problem? 您是否可以帮助我(如果可以通过某些代码实现)来解决我的问题? Thanks in advance, Francesco 在此先感谢,弗朗切斯科

Update of my question Here is my render method: 我的问题的更新这是我的渲染方法:

public class GameRenderer{

private GameWorld myWorld;
private ShapeRenderer shapeRenderer;
private SpriteBatch sb;
private Camera camera;
private Constants constant;
private Rectangle viewport;

//dichiaro le variabili per caricare gli asset
private Player player;
private ScrollHandler scroller;
private Bordo frontBordoSX_1, backBordoSX_1, frontBordoSX_2, backBordoSX_2;
private Bordo frontBordoDX_1, backBordoDX_1, frontBordoDX_2, backBordoDX_2;

/*
private Ostacolo ob1_sx, ob2_sx, ob3_sx;
private Ostacolo ob4_dx, ob5_dx, ob6_dx;
*/

private TextureRegion player_TR;
private TextureRegion bordoSX_1, bordoSX_2;
private TextureRegion bordoDX_1, bordoDX_2;
private TextureRegion obstacleSX, obstacleSX_flip;
private TextureRegion obstacleDX, obstacleDX_flip;
private TextureRegion enemyS;
private TextureRegion blackBar;

//box2dpart
private World worldbox;
private Sprite fbDx_1,fbDx_2,fbSx_1,fbSx_2;
private Sprite player_sprite;
private Body player_body, bordo_destro;
private MyContactListener contactListener;

public GameRenderer(GameWorld world) {

    myWorld = world;
    constant = new Constants();
    camera = new OrthographicCamera(constant.getWidth(), constant.getHeight());
    shapeRenderer = new ShapeRenderer();
    shapeRenderer.setProjectionMatrix(camera.combined);
    sb = new SpriteBatch();
    sb.setProjectionMatrix(camera.combined);

    contactListener = new MyContactListener();
    worldbox= new World(new Vector2(0,-10),true);
    worldbox.setContactListener(contactListener);


    //initialize objects and assets
    initGameObjects();
    initAssets();

}

private void initGameObjects(){
    player = myWorld.getPlayer();
    scroller = myWorld.getScroller();
    frontBordoSX_1 = scroller.getFrontBordoSX_1();
    backBordoSX_1 = scroller.getBackBordoSX_1();
    frontBordoSX_2 = scroller.getFrontBordoSX_2();
    backBordoSX_2 = scroller.getBackBordoSX_2();
    frontBordoDX_1 = scroller.getFrontBordoDX_1();
    backBordoDX_1 = scroller.getBackBordoDX_1();
    frontBordoDX_2 = scroller.getFrontBordoDX_2();
    backBordoDX_2 = scroller.getBackBordoDX_2();

    /* other objects
    ob1_sx = scroller.getOb1_sx();
    ob2_sx = scroller.getOb2_sx();
    ob3_sx = scroller.getOb3_sx();
    ob4_dx = scroller.getOb4_dx();
    ob5_dx = scroller.getOb5_dx();
    ob6_dx = scroller.getOb6_dx();
    */
}

private void initAssets(){

    player_TR = AssetLoader.player;
    bordoSX_1 = AssetLoader.bordoSX;
    bordoSX_2 = AssetLoader.bordoSX;
    bordoDX_1 = AssetLoader.bordoDX;
    bordoDX_2 = AssetLoader.bordoDX;
    obstacleDX = AssetLoader.obstacleDX;
    obstacleSX = AssetLoader.obstacleSX;
    obstacleDX_flip = AssetLoader.obstacleDX_flip;
    obstacleSX_flip = AssetLoader.obstacleSX_flip;
    enemyS = AssetLoader.enemyS;
    blackBar = AssetLoader.blackBar;

    //box2d part

}

private void drawMargin(){

    //bordo SX

    /*
    sb.draw(bordoSX_1, frontBordoSX_2.getX(), frontBordoSX_2.getY(), frontBordoSX_2.getWidth(),
            frontBordoSX_2.getHeight());
    sb.draw(bordoSX_2, frontBordoSX_1.getX(), frontBordoSX_1.getY(), frontBordoSX_1.getWidth(),
            frontBordoSX_1.getHeight());
    */

    fbSx_1 = new Sprite(bordoSX_1);
    fbSx_1.setSize(frontBordoSX_1.getWidth(),frontBordoSX_1.getHeight());
    fbSx_1.setPosition(frontBordoSX_1.getX(), frontBordoSX_1.getY());
    fbSx_1.setOrigin(0, 0);
    fbSx_1.draw(sb);

    fbSx_2 = new Sprite(bordoSX_2);
    fbSx_2.setSize(frontBordoSX_2.getWidth(),frontBordoSX_2.getHeight());
    fbSx_2.setPosition(frontBordoSX_2.getX(), frontBordoSX_2.getY());
    fbSx_2.setOrigin(0, 0);
    fbSx_2.draw(sb);

    fbDx_1 = new Sprite(bordoDX_1);
    fbDx_1.setSize(frontBordoDX_1.getWidth(),frontBordoDX_1.getHeight());
    fbDx_1.setPosition(frontBordoDX_1.getX(), frontBordoDX_1.getY());
    fbDx_1.setOrigin(0, 0);
    fbDx_1.draw(sb);

    fbDx_2 = new Sprite(bordoDX_2);
    fbDx_2.setSize(frontBordoDX_2.getWidth(),frontBordoDX_2.getHeight());
    fbDx_2.setPosition(frontBordoDX_2.getX(), frontBordoDX_2.getY());
    fbDx_2.setOrigin(0, 0);
    fbDx_2.draw(sb);

    sb.draw(blackBar,-constant.getWidth()/2,-constant.getHeight()/2,
            (float) (0.573913)*(constant.getWidth()/6),constant.getHeight());
    sb.draw(blackBar,(float)(constant.getWidth()/2-(0.573913)*(constant.getWidth()/6)),-constant.getHeight()/2,
            (float)(0.573913)*(constant.getWidth()/6),constant.getHeight());


}

private void drawPlayer(){
    player_sprite = new Sprite(player_TR);
    player_sprite.setSize(player.getWidth(), player.getHeight());
    player_sprite.setPosition(player.getX(), player.getY());
    player_sprite.setOrigin(0, 0);
    player_sprite.draw(sb);
}

/*
private void drawOstacoli(){

    sb.draw(obstacleSX_flip,ob1_sx.getX(),ob1_sx.getY(),ob1_sx.getWidth(),ob1_sx.getHeight());
    sb.draw(obstacleSX,ob2_sx.getX(),ob2_sx.getY(),ob2_sx.getWidth(),ob2_sx.getHeight());
    sb.draw(obstacleSX_flip,ob3_sx.getX(),ob3_sx.getY(),ob3_sx.getWidth(),ob3_sx.getHeight());
    sb.draw(obstacleDX,ob4_dx.getX()+constant.getWidth()/2-ob4_dx.getWidth(),ob4_dx.getY(),ob4_dx.getWidth(),ob4_dx.getHeight());
    sb.draw(obstacleDX_flip,ob5_dx.getX()+constant.getWidth()/2-ob5_dx.getWidth(),ob5_dx.getY(),ob5_dx.getWidth(),ob5_dx.getHeight());
    sb.draw(obstacleDX,ob6_dx.getX()+constant.getWidth()/2-ob6_dx.getWidth(),ob6_dx.getY(),ob6_dx.getWidth(),ob6_dx.getHeight());

}
*/

public void render(float runTime) {



    Box2D.init();
    int width = constant.getWidth();
    int height = constant.getHeight();
    float ratio = constant.getRatio();

    //viewport
    float aspectRatio = (float) width / (float) height;
    float scale = 1f;
    Vector2 crop = new Vector2(0f, 0f);

    if(aspectRatio > ratio)
    {
        scale = (float)height/(float)height;
        crop.x = (width - width * scale) / 2f;
    } else if (aspectRatio < ratio) {
        scale = (float)width/(float)width;
        crop.y = (height - height*scale)/2f;
    }
    else
    {
        scale = (float) width / (float) width;
    }

    float w = (float) width * scale;
    float h = (float) height * scale;

    viewport = new Rectangle(crop.x, crop.y, w, h);


    // update camera
    camera.update();

    // clear previous frame
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);

    // Draw Background color
    shapeRenderer.setColor(255 / 255.0f, 255 / 255.0f, 250 / 255.0f, 1);
    shapeRenderer.rect(-constant.getWidth() / 2, -constant.getHeight() / 2, constant.getWidth(),
            constant.getHeight());

    // End ShapeRenderer
    shapeRenderer.end();


    // set viewport
    Gdx.gl.glViewport((int) viewport.x, (int) viewport.y,
            (int) viewport.width, (int) viewport.height);

    // Begin SpriteBatch
    sb.begin();

    // The player needs transparency, so we enable that again.
    sb.enableBlending();

    // Draw player at its coordinates.
    drawPlayer();

    //draw right and left side
    drawMargin();

    // End SpriteBatch
    sb.end();

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

}

} }

And if I start the program my view is my player in the middle and to margins ,one on the left and one on the right. 而且,如果我启动该程序,则我的观点是我的玩家在中间,在左边,一个在左边,一个在右边。 (unfortunatly I cannot post images of my view because I don't have enough rep). (不幸的是,由于没有足够的代表,我无法发布视图的图像)。

Everithing is fine. 一切都很好。 I move my player with the accelerometer and it works fine without any problem. 我用加速度计移动播放器,它工作正常,没有任何问题。 The only problem is that if I move the player near the margin the two entities overlap instead of colliding and I don't understand why. 唯一的问题是,如果我将玩家移动到边缘附近,则这两个实体会重叠而不是发生碰撞,而且我也不明白为什么。 I also fixed the line: 我还修正了这一行:

loader.attachFixture(body, fixture_name, fd, 1);

to loader.attachFixture(body, fixture_name, fd, player_width); 到loader.attachFixture(body,Fixture_name,fd,player_width);

but nothing changes. 但没有任何变化。

First, from here , the object Contact manages contact between two shapes, and from here , the listener ContactListener will be called when two fixtures begin to touch. 首先,从此处开始 ,对象Contact管理两个形状之间的接触,从此处开始,当两个灯具开始接触时,将调用侦听器ContactListener

So, to make your code work, you should set a custom object to your bodies with the method: setUserData(Object userData) . 因此,要使代码正常工作,应使用以下方法为您的主体设置一个自定义对象: setUserData(Object userData) Usually this method is used to link the sprite or the actor with the physic body, but for example purpose you could just send a simple ID (like a string). 通常,此方法用于将子画面或actor与物理主体链接,但是例如,您可以只发送一个简单的ID(如字符串)。

So in this part: 所以在这一部分:

// 3. Create a Body, as usual.
body= world.createBody(bd);
//body.setBullet(true);

You could add an identificator to your object like this: 您可以像这样向您的对象添加一个标识符:

body.setUserData("player");

to idenfity your object, and then, when the listener get fired, you could retrieve this value: 确定您的对象,然后在触发侦听器时,可以检索以下值:

    @Override
    public void beginContact(Contact contact) {
        String userDataA = contact.getFixtureA().getBody().getUserData().toString();
        String userDataB = contact.getFixtureB().getBody().getUserData().toString();

        if(userDataA.equals("player") && userDataB.equals("otherEntity")){
            colliding = true;
            //do stuffs when collision has started
        } else if(userDataB.equals("player") && userDataA.equals("otherEntity")){
            colliding = true;
            //do stuffs when collision has started
        }
        System.out.println("Contact detected");
    }

After that, you could be able to do whatever you want to do with this collision. 在那之后,您将能够做任何您想做的碰撞。

Hope you find this useful! 希望你觉得这个有用!

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

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