box2d contact listener fixtures order

被刻印的时光 ゝ 提交于 2019-12-01 12:51:41

问题


I am trying to understand how does it work.

In my game I use box2d physics, to handle contacts I use contact listener, example:

ContactListener contactListener = new ContactListener()
{
    @Override
    public void beginContact(Contact contact)
    {
        final Fixture x1 = contact.getFixtureA();
        final Fixture x2 = contact.getFixtureB(); 

        if (x1.getBody().getUserData() != null && x2.getBody().getUserData() != null)
        {                   
            if (x1.getBody().getUserData().equals("player"))
            {                                               
                player.increaseFootContacts();
            }
        }
    }

And here`s question, is there any order in those fixtures? (x1 or x2) after 2 test, I found out that in this case, my player will be x1, and other objects x2, should I also check reverse order ? (if x2 is player and so on) ?


回答1:


No, there is no guarantee which fixture is which. You have to check for both possibilities, like this:

if ( (userdataA.equals("player") && userDataB.equals("ground")) ||
     (userdataA.equals("ground") && userDataB.equals("player")) )
         player.increaseFootContacts();


来源:https://stackoverflow.com/questions/14104813/box2d-contact-listener-fixtures-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!